diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..e0783ec --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Cutting_Stock \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ba93b15 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2fe4f54 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cutting_Stock.iml b/Cutting_Stock.iml new file mode 100644 index 0000000..26db12c --- /dev/null +++ b/Cutting_Stock.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index e86a2c0..f6ec528 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/bin/org/optimization/CuttingStock.class b/bin/org/optimization/CuttingStock.class index 042ab0c..2d6ca4f 100644 Binary files a/bin/org/optimization/CuttingStock.class and b/bin/org/optimization/CuttingStock.class differ diff --git a/bin/org/optimization/InvalidLegthException.class b/bin/org/optimization/InvalidLegthException.class index 193e658..b1cc80b 100644 Binary files a/bin/org/optimization/InvalidLegthException.class and b/bin/org/optimization/InvalidLegthException.class differ diff --git a/bin/org/optimization/InvalidParameterException.class b/bin/org/optimization/InvalidParameterException.class index ef71a2d..be4f5dc 100644 Binary files a/bin/org/optimization/InvalidParameterException.class and b/bin/org/optimization/InvalidParameterException.class differ diff --git a/bin/org/optimization/MainClass.class b/bin/org/optimization/MainClass.class index e3afa05..4904e7d 100644 Binary files a/bin/org/optimization/MainClass.class and b/bin/org/optimization/MainClass.class differ diff --git a/src/org/optimization/CuttingStock.java b/src/org/optimization/CuttingStock.java index f10abee..4d5cb82 100644 --- a/src/org/optimization/CuttingStock.java +++ b/src/org/optimization/CuttingStock.java @@ -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> mapList=new ArrayList>(); - private List store=new ArrayList(); + private ArrayList> mapList=new ArrayList<>(); + private List store; private int count=0; public boolean hasMoreCombinations() { - if(count nextCombination() { - Map map=mapList.get(count); - count++; - return map; + Map map = mapList.get(count); + count++; + return map; } public CuttingStock(int max,int block[],int quantity[]) throws InvalidLegthException,InvalidParameterException - { - for(int i=0;imax) - { - 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;i0) - System.out.println(block[j]+" * "+this.stock.get(i).comb[j]); - } - }*/ - } + private void initialize() { - store=new ArrayList(); - 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("

"); - } - 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() { @@ -102,6 +73,7 @@ private void sort() } }while(swap); } + private void calculate(List store) { initLimit(); @@ -178,40 +150,32 @@ private void calculate(List store) else if(i==total-1 && qty[i]==0) start=false; break; - }/*//out.println("After start loop"); // DELETE IT - for(int i=0;istore ) { counter++; boolean flag=false; - //out.println("=====================================
Piece no "+counter+"
----------
"); if(a==0) { Map tempMap = new HashMap(); for(int i=0;istore ) for(int i=0;istore ) showComb(a,store); } } + private void combinations() { for(int i=total-1;;) @@ -261,6 +226,7 @@ private void combinations() } } } + private void initLimit() { int div; @@ -274,6 +240,7 @@ private void initLimit() limit[i]=qty[i]; } } + private void updateLimit() { for(int i=0;i map; CuttingStock cuttingStock= new CuttingStock(max_size,blocks,quantities); while(cuttingStock.hasMoreCombinations())