Skip to content

Commit

Permalink
Add DP3UKInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesBremner committed Jun 24, 2020
1 parent 429cab2 commit 9d30fb3
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 15 deletions.
1 change: 1 addition & 0 deletions build/codeblocks/DP3UK.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Unit filename="../../src/DP3UKTest.cpp" />
<Unit filename="../../src/RRP.cpp" />
<Unit filename="../../src/knapsack.h" />
<Unit filename="../../src/sInstance.cpp" />
<Extensions>
<code_completion />
<envvars />
Expand Down
55 changes: 55 additions & 0 deletions build/codeblocks/DP3UKInstance.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="DP3UKInstance" />
<Option pch_mode="2" />
<Option compiler="gcc_v83" />
<Build>
<Target title="Debug">
<Option output="../../bin/DP3UKInstance" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc_v83" />
<Option parameters="C:\Users\James\code\knapsack\data\gcut\item4cube_bin13cube.txt" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="../../bin/DP3UKInstance" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc_v83" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add option="-std=c++17" />
</Compiler>
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
</Linker>
<Unit filename="../../src/DDP.cpp" />
<Unit filename="../../src/DP3UK.cpp" />
<Unit filename="../../src/DP3UKInstance.cpp" />
<Unit filename="../../src/RRP.cpp" />
<Unit filename="../../src/knapsack.h" />
<Unit filename="../../src/sInstance.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
4 changes: 4 additions & 0 deletions data/gcut/item4cube_bin13cube.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
13 13 13
4 4 4
item4cube_bin13cube
2 changes: 1 addition & 1 deletion src/DDP.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <vector>

#include <iostream>

std::vector<int> DDP(
int D,
Expand Down
6 changes: 3 additions & 3 deletions src/DP3UK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#include "knapsack.h"

sPattern DP3UK (
std::vector<int>& l,
std::vector<int>& w,
std::vector<int>& h,
sInstance& problem )
{
// populate variables used by pseudo code
int L = problem.bin[0];
int W = problem.bin[1];
int H = problem.bin[2];
std::vector<int> l = problem.l;
std::vector<int> w = problem.w;
std::vector<int> h = problem.h;
std::vector<int> v = problem.item_values;

// Calulate raster points
Expand Down
37 changes: 37 additions & 0 deletions src/DP3UKInstance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** Application to read THREE-DIMENSIONAL UNBOUNDED KNAPSACK PROBLEM - INSTANCES
from http://www.loco.ic.unicamp.br/files/instances/3duk/
apply code Implemented from the psuedo code in
Cintra GF, Miyazawa FK, Wakabayashi Y, Xavier EC. Algorithms for twodimensional cutting stock and strip packing problems using dynamic programming and column generation. European Journal of Operational Research
2008;191:59–83
algorithm 1: DP3UK
*/


#include <iostream>
#include <vector>
#include "knapsack.h"


int main( int argc, char* argv[] )
{
std::cout << "DP3UK\n";

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

// read the instance file
sInstance problem;
problem.read( argv[1] );

// run the algorithm
auto P = DP3UK( problem );

// display the solution
std::cout << P.text() << "\n";

return 0;
}

8 changes: 4 additions & 4 deletions src/DP3UKTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
void testRRP()
{
std::cout << "DDP\n";
auto r = DDP( 10, { 5, 7 } );
auto r = DDP( 13, { 4 } );
for( int x : r )
std::cout << x<< " ";
std::cout << "\n";

std::cout << "RRP\n";
r = RRP( 10, { 5, 7 } );
r = RRP( 13, { 4 } );
for( int x : r )
std::cout << x<< " ";
std::cout << "\n";
Expand Down Expand Up @@ -48,7 +48,7 @@ void test2()
std::vector<int> h = { 4 };

// 10 unit cube bin
problem.bin = std::vector<int>(3,12);
problem.bin = std::vector<int>(3,13);

problem.item_values = { 5 };

Expand Down Expand Up @@ -84,7 +84,7 @@ int main()
{
std::cout << "DP3UK\n";

//testRRP();
testRRP();
// test1();
test2();
//test3();
Expand Down
3 changes: 2 additions & 1 deletion src/RRP.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include "knapsack.h"
Expand Down Expand Up @@ -35,8 +36,8 @@ std::vector<int> RRP(
int r = LargestInVSmallerThanX( S, D - p );
if( r >= 0 )
reduced_raster_points.push_back( r );

}

// arrange in ascending size
std::reverse(reduced_raster_points.begin(),reduced_raster_points.end());

Expand Down
21 changes: 15 additions & 6 deletions src/knapsack.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// knapsack.h contains declarations for functions implementing knapsack algorithms

#include <string>
#include <vector>

/** discretization points ( (positions where guillotine cutting can be performed)
@param[in] D knapsack capacity
Expand Down Expand Up @@ -33,7 +34,21 @@ std::vector<int> RRP(
struct sInstance
{
std::vector<int> bin;
std::vector<int> l;
std::vector<int> w;
std::vector<int> h;
std::vector<int> item_values;
std::string myName;

/* read problem instance
#param[in] fname path to file
The file format is described
http://www.loco.ic.unicamp.br/files/instances/3duk/
*/
void read( const std::string& fname );

std::string text();
};

/// A solution pattern
Expand All @@ -51,13 +66,7 @@ struct sPattern
};

/** dynamic programming for the three-dimensional unbounded knapsack
@param[in] l lengths of items
@param[in] w width of items
@param[in] h heights of items
@param[in] problem instance
*/
sPattern DP3UK (
std::vector<int>& l,
std::vector<int>& w,
std::vector<int>& h,
sInstance& problem );
67 changes: 67 additions & 0 deletions src/sInstance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <fstream>
#include <sstream>
#include <iostream>
#include "knapsack.h"

std::vector< int >
ParseSpaceDelimited(
const std::string& l )
{
std::vector< int > output;
std::stringstream sst(l);
std::string a;
while( getline( sst, a, ' ' ) )
output.push_back( atoi(a.c_str()));
return output;
}

void sInstance::read( const std::string& fname )
{
std::ifstream f( fname );
if( ! f.is_open() )
throw std::runtime_error("Cannot read instance file " + fname );

l.clear();
w.clear();
h.clear();

int box_type_count;
int lcount = 0;
std::string line;
while( std::getline( f, line ) )
{
//std::cout << lcount << " " << line << "\n";
if( lcount == 0 )
{
box_type_count = atoi( line.c_str() );
lcount++;
continue;
}
if( lcount == 1 )
{
bin = ParseSpaceDelimited( line );
lcount++;
continue;
}
if( lcount < 2 + box_type_count )
{
auto lv = ParseSpaceDelimited( line );
l.push_back( lv[0] );
w.push_back( lv[1] );
h.push_back( lv[2] );
item_values.push_back( lv[0]*lv[1]*lv[2] );
lcount++;
continue;
}
myName = line;
}
std::cout << "read problem " << myName << "\n";
}

std::string sInstance::text()
{
std::stringstream ss;
for( int k = 0; k < (int)l.size(); k++ )
ss << l[k] <<" "<< w[k] <<" " << h[k] << "\n";
return ss.str();
}

0 comments on commit 9d30fb3

Please sign in to comment.