Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cutting_Stock.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="inheritedJdk" />
</component>
</module>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ And I want pieces example

700(mm) * 4 pieces
500(mm) * 3 pieces
250(mm) * 4 pieces
250(mm) * 6 pieces
320(mm) * 5 pieces
To cut this efficiently by minimizing the waste, here is the code.

Expand Down
Binary file modified bin/org/optimization/CuttingStock.class
Binary file not shown.
Binary file modified bin/org/optimization/InvalidLegthException.class
Binary file not shown.
Binary file modified bin/org/optimization/InvalidParameterException.class
Binary file not shown.
Binary file modified bin/org/optimization/MainClass.class
Binary file not shown.
102 changes: 35 additions & 67 deletions src/org/optimization/CuttingStock.java
Original file line number Diff line number Diff line change
@@ -1,83 +1,54 @@
package org.optimization;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

public class CuttingStock {

private int block[],qty[],comb[],tempcomb[],limit[];
@SuppressWarnings("unused")
private int max,total,counter=0,waste=0;
private List<Map<Integer,Integer>> mapList=new ArrayList<Map<Integer, Integer>>();
private List<Integer> store=new ArrayList<Integer>();
private ArrayList<Map<Integer,Integer>> mapList=new ArrayList<>();
private List<Integer> store;
private int count=0;

public boolean hasMoreCombinations()
{
if(count<counter)
return true;
else
return false;
return count<counter;
}
public synchronized Map<Integer, Integer> nextCombination()
{
Map<Integer, Integer> map=mapList.get(count);
count++;
return map;
Map<Integer, Integer> map = mapList.get(count);
count++;
return map;
}

public CuttingStock(int max,int block[],int quantity[]) throws InvalidLegthException,InvalidParameterException
{
for(int i=0;i<block.length;i++)
{
if(block[i]>max)
{
throw new InvalidLegthException();
}
}
{
Arrays.stream(block).forEach((p) ->{
if (p > max)
throw new InvalidLegthException();
});

if(block.length!=quantity.length)
{
throw new InvalidLegthException();
}

store=new ArrayList<>();
this.total=block.length;
this.max=max;
this.block=block;
this.qty=quantity;
this.doIt();
this.initialize();
}
private void doIt()
{
this.initialize();
/*for(int i=0;i<stock.size();i++)
{
for(int j=0;j<stock.get(i).comb.length;j++)
{
if(stock.get(i).comb[j]>0)
System.out.println(block[j]+" * "+this.stock.get(i).comb[j]);
}
}*/
}

private void initialize()
{
store=new ArrayList<Integer>();
waste=0;
counter=0;
this.sort();
this.calculate(store);
/*wast_array=store.toArray();
if(wast_array.length>0)
{
System.out.println("Consider reusing the following remains");
for(int i=wast_array.length-1;i>=0;i--)
{
System.out.println((this.counter+i-wast_array.length+1)+" "+wast_array[i]);
}
//out.println("</table><br><br>");
}
System.out.println("No of pieces req = "+this.counter);
System.out.println("Waste = "+this.waste);*/
Object[] wast_array = store.toArray();
System.out.println("Consider reusing the following remains");
Arrays.stream(wast_array).forEach((p) ->{ System.out.println("-> "+ p); });
System.out.println("No of pieces req = "+this.counter +"\n" + "Waste = "+this.waste);
}
private void sort()
{
Expand All @@ -102,6 +73,7 @@ private void sort()
}
}while(swap);
}

private void calculate(List<Integer> store)
{
initLimit();
Expand Down Expand Up @@ -178,48 +150,40 @@ private void calculate(List<Integer> store)
else if(i==total-1 && qty[i]==0)
start=false;
break;
}/*//out.println("After start loop"); // DELETE IT
for(int i=0;i<total;i++) ////////
//out.print(qty[i]); /////////
//out.println(); */ //////////
}
}
}

private void showComb(int a, List<Integer>store )
{
counter++;

boolean flag=false;
//out.println("<font color=\"brown\">=====================================</font><br>Piece no "+counter+"<br>----------<br>");
if(a==0)
{
Map<Integer, Integer> tempMap = new HashMap<Integer, Integer>();
for(int i=0;i<total;i++)
if(comb[i]!=0)
{
tempMap.put(new Integer(block[i]), new Integer(comb[i]));
//System.out.println(block[i]+" * "+comb[i]);
tempMap.put(block[i], comb[i]);
qty[i]=qty[i]-comb[i]; // deduct samples from stock(qty) which are already printed
if((qty[i]-comb[i])<0)
{
flag=true; // to return and not recursively call.
}
}
flag=true;

if(flag)
{
mapList.add(tempMap);
return;
}
showComb(0,store);
//showComb(0,store);
}
else
{
Map<Integer, Integer> tempMap = new HashMap<Integer, Integer>();
for(int i=0;i<total;i++)
if(tempcomb[i]!=0)
{
tempMap.put(new Integer(block[i]), new Integer(tempcomb[i]));
tempMap.put(block[i], tempcomb[i]);
//System.out.println(block[i]+" ggg "+tempcomb[i]);
}
mapList.add(tempMap);
Expand All @@ -240,6 +204,7 @@ private void showComb(int a, List<Integer>store )
showComb(a,store);
}
}

private void combinations()
{
for(int i=total-1;;)
Expand All @@ -261,6 +226,7 @@ private void combinations()
}
}
}

private void initLimit()
{
int div;
Expand All @@ -274,6 +240,7 @@ private void initLimit()
limit[i]=qty[i];
}
}

private void updateLimit()
{
for(int i=0;i<total;i++)
Expand All @@ -282,9 +249,10 @@ private void updateLimit()
limit[i]=qty[i];
}
}

private void resetComb()
{
for(int i=0;i<total;i++) // reset comb[]
for(int i=0;i<total;i++)
comb[i]=0;
}

Expand Down
6 changes: 3 additions & 3 deletions src/org/optimization/MainClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public class MainClass {

public static void main(String[] args) {

int blocks[]={700,500,250,380};
int quantities[]={4,3,6,5};
int i=0,max_size=2000;
int blocks[]={225,175, 20,60, 25, 420, 50, 950};
int quantities[]={2,2,2,2, 4, 2, 4, 2};
int i=0,max_size=1000;
Map<Integer, Integer> map;
CuttingStock cuttingStock= new CuttingStock(max_size,blocks,quantities);
while(cuttingStock.hasMoreCombinations())
Expand Down