Skip to content

Commit

Permalink
stock, sheet and scrap inventories #7
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesBremner committed Jun 26, 2020
1 parent 1933555 commit 4f40496
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 69 deletions.
11 changes: 10 additions & 1 deletion build/codeblocks/timberAllocation.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
<Build>
<Target title="Debug">
<Option output="../../bin/timberAllocation" prefix_auto="1" extension_auto="1" />
<Option working_dir="../../bin" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc_v83" />
<Option parameters="../data/t1.txt" />
<Compiler>
<Add option="-g" />
</Compiler>
Expand All @@ -32,7 +34,14 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="../../../src/TimberAllocation.cpp" />
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
</Linker>
<Unit filename="../../src/TimberAllocation.cpp" />
<Unit filename="../../src/TimberAllocation.h" />
<Unit filename="../../src/taSpace.cpp" />
<Extensions>
<code_completion />
<envvars />
Expand Down
149 changes: 81 additions & 68 deletions src/TimberAllocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,14 @@
#include <sstream>
#include <memory>
#include <vector>
#include <algorithm>
#include "TimberAllocation.h"

using namespace std;

namespace ta
{

class cTimber;
typedef std::shared_ptr< cTimber > timber_t;
typedef std::vector< timber_t > timberv_t;


/// A 3D object with dimension and location
class cSpace
{
public:
int myLength, myWidth, myHeight; // dimensions
int myLocL, myLocW, myLocH;


cSpace( int L, int W, int H )
: myLength( L ), myWidth( W ), myHeight{ H }
{

}
bool fit( const cSpace& squeeze ) const
{
return ( squeeze.myLength <= myLength &&
squeeze.myLength <= myLength &&
squeeze.myHeight <= myHeight );
}

int size_horiz()
{
return myLength * myWidth;
}
};

class cTimber : public cSpace
{
public:
cTimber()
: cSpace( 0, 0, 0 )
{

}
cTimber( int L, int W, int H )
: cSpace( L, W, H )
{

}
int ParseSpaceDelimited(
const std::string& l );
private:
std::string myUserID;
int myDemand;

};
class cInstance
{
public:
void read( const std::string& fname );

private:
timberv_t myInventory;
timberv_t myOrder;
std::vector< int > ParseSpaceDelimited(
const std::string& l );
void add( std::vector< int > );
};

int cTimber::ParseSpaceDelimited(
const std::string& l )
{
Expand Down Expand Up @@ -106,6 +44,7 @@ void cInstance::read(
myInventory.clear();
myOrder.clear();

// loop over lines in file
std::string line;
while( std::getline( f, line ) )
{
Expand All @@ -121,22 +60,96 @@ void cInstance::read(
break;
}
}
expandDemand( myInventory );
expandDemand( myOrder );
}

void cInstance::expandDemand( timberv_t& tv )
{
timberv_t ex;
for( auto& t : tv )
{
if( t->myDemand <= 0 )
throw std::runtime_error("Bad demand for " + t->myUserID );
for( int k = 0; k < t->myDemand-1; k++ )
ex.push_back( timber_t( new cTimber( *t.get() )));
}
tv.insert(
tv.end(),
ex.begin(), ex.end() );
}

std::string cInstance::text()
{
std::stringstream ss;
ss << "Stock contains " << myInventory.size() << " timbers\n"
<< "Sheet inventory " << mySheet.size() << "\n"
<< "Scrap inventory " << myScrap.size() << "\n"
<< "Order demands " << myOrder.size() << " timbers\n";
return ss.str();
}

void cInstance::sortInventory( int sheetHeight, int scrapWidth )
{
// rotate, if neccesary, so L > W > H
for( auto t : myInventory )
{
t->rotateLWH();
}

// find sheets
for( auto t : myInventory )
if( t->myHeight < sheetHeight )
mySheet.push_back( t );
myInventory.erase(
remove_if(
myInventory.begin(),
myInventory.end(),
[ sheetHeight ] ( timber_t t )
{
return( t->myHeight < sheetHeight );
} ),
myInventory.end() );


// find scraps
for( auto t : mySheet )
if( t->myWidth < scrapWidth )
myScrap.push_back( t );
myInventory.erase(
remove_if(
mySheet.begin(),
mySheet.end(),
[ scrapWidth ] ( timber_t t )
{
return( t->myWidth < scrapWidth );
} ),
mySheet.end() );
}

}

int main( int argc, char* argv[] )
{
cout << "TimberAllocation" << endl;
cout << "TimberAllocation1" << endl;

if( argc != 2 )
{
std::cout << "Usage: TimberAllocation <path to instance file>\n";
exit(1);
}

ta::cInstance I;
I.read( argv[1] );

try
{
ta::cInstance I;
I.read( argv[1] );
std::cout << I.text();
I.sortInventory( 1000, 100 );
std::cout << I.text();
}
catch( std::runtime_error& e )
{
std::cout << "exception: " << e.what() << "\n";
}
return 0;
}
77 changes: 77 additions & 0 deletions src/TimberAllocation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace ta
{

class cTimber;
typedef std::shared_ptr< cTimber > timber_t;
typedef std::vector< timber_t > timberv_t;


/// A 3D object with dimension and location
class cSpace
{
public:
int myLength, myWidth, myHeight; // dimensions
int myLocL, myLocW, myLocH;


cSpace( int L, int W, int H )
: myLength( L ), myWidth( W ), myHeight{ H }
{

}
bool fit( const cSpace& squeeze ) const
{
return ( squeeze.myLength <= myLength &&
squeeze.myLength <= myLength &&
squeeze.myHeight <= myHeight );
}

int size_horiz()
{
return myLength * myWidth;
}
/// rotate so that L, W, H are in decending size order
void rotateLWH();
};

class cTimber : public cSpace
{
public:
cTimber()
: cSpace( 0, 0, 0 )
{

}
cTimber( int L, int W, int H )
: cSpace( L, W, H )
{

}
int ParseSpaceDelimited(
const std::string& l );


std::string myUserID;
int myDemand;

};
class cInstance
{
public:
void read( const std::string& fname );
std::string text();
void sortInventory( int sheetHeight, int scrapWidth );


private:
timberv_t myInventory;
timberv_t myOrder;
timberv_t myScrap;
timberv_t mySheet;
std::vector< int > ParseSpaceDelimited(
const std::string& l );
void add( std::vector< int > );
void expandDemand( timberv_t& tv );
};
}

29 changes: 29 additions & 0 deletions src/taSpace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <memory>
#include <vector>
#include <algorithm>
#include "TimberAllocation.h"

using namespace std;

namespace ta
{
void cSpace::rotateLWH()
{
std::vector<int> vLWH { myLength, myWidth, myHeight };
std::sort( vLWH.begin(), vLWH.end(),
[]( int& a, int& b)
{
return a > b;
});
myLength = vLWH[0];
myWidth = vLWH[1];
myHeight = vLWH[2];

// for( int i : vLWH )
// std::cout << i <<" ";
// std::cout << "\n";
}
}

0 comments on commit 4f40496

Please sign in to comment.