Tuesday, 15 February 2011

java - Memoization:Rememo -



java - Memoization:Rememo -

the array memo[][] returning cloth is in memo[x][y]. have tested class , seems stores first cloth fits in memo[x][y], want homecoming valuable cloth fits in memo[x][y]. how can this?

class clothcutter { static arraylist <pattern> patterns; //array of patterns seek static cloth maxcloth; //the maximum cloth static cloth memo[][]; // memo maximum cloth (x,y), memo[x][y] int width; int height; //constructor public clothcutter(int w, int h,arraylist<pattern> p) { width = w; height = h; patterns = p; maxcloth = new cloth(w,h); memo = new cloth [w+1][h+1]; (int = 0; i<patterns.size();i++) { pattern z = patterns.get(i); system.out.println(z.name); cloth m = new cloth(z.width,z.height); m.add(z); memo[z.width][z.height]=m; system.out.println(memo[z.width][z.height].value); } } public cloth optimize() { homecoming optimize(maxcloth); } public cloth optimize(cloth c){ cloth temp1 = new cloth(); cloth temp2 = new cloth(); cloth max = new cloth(c.width,c.height);//temporary max if (memo[c.width][c.height]!=null) // homecoming memo if there 1 homecoming memo[c.width][c.height]; if (c.width==0||c.height==0) //if (x||y ==0) memo[c.width][c.height]=max; homecoming max; } (int i=0;i<patterns.size();i++) { //for each pattern pattern p = patterns.get(i); if (p.width<=c.width && p.height<=c.height) { if (p.width<c.width) { //if pattern's width less cloth's width cloth = new cloth(c.width-p.width,c.height);//cut vertically a.pattern = p; cloth b = new cloth(p.width,c.height);//remainder after vertical cutting b.pattern = p; temp2=optimize(b);//recurse temp1=optimize(a);//recurse } if (c.width==p.width) { //if cloth's width equal patterns start cutting horizontally cloth = new cloth(c.width,c.height-p.height);//horizontal cutting a.pattern=p; cloth b = new cloth(c.width,p.height);//remainder after horizontal cutting b.pattern = p; temp2=optimize(b);//recurse temp1=optimize(a);//recurse } if (temp1.value+temp2.value>max.value) { //if value of optimal cloths greater value of max max.add(temp1,temp2);//add 2 cloths max max.pattern = p; if (max.width == maxcloth.width && max.height == maxcloth.height && maxcloth.value < max.value) // if max dimentions equal maxcloth dimetions , value of maxcloth less max maxcloth=max;//set maxcloth max } } } if (memo[max.width][max.height]==null) //if memo equal null memo[max.width][max.height]=max;// create memo homecoming max; }

your code little confusing me figure out problem wrote psuedocode friend , got right optimal value when ran mine. perhaps can compare!

optimize() { rectangle memo[width][height] optimize(0,0,totalwidth, totalheight, memo) } optimize(x, y, width, height, memo) { if memo[width][height] != null homecoming memo[width][height] rect = new rectangle(width, height, value = 0) each pattern { //find vertical cutting solution leftverticalrect = optimize (x, y + pattern.height, pattern.width, height-pattern.height,memo) rightverticalrect = optimize(x + pattern.width, y, width-pattern.width, height) verticalcut = new cut(x + pattern.width, y, x + pattern.width, y + height) //find horizontal cutting solution tophorizontalrect = optimize ( --parameters-- ) bottomhortizonalrect = optimize( --parameters--) horizontalcut = new cut( --parameters--) //see solution more optimal if (leftverticalrect.val + rightverticalrect.val > tophorizontalrect.val + bottomhorizontalrect.val) subprobsolution = vertical cutting solution else subprobsolution = horizontal cutting solution //see if solution found greater previous solutions subproblem if (subprobsolution.value + pattern.value > rect.value) { rect.subrect1 = subprobsolutionrect1 rect.subrect2 = subprobsolutionrect2 rect.pattern = pattern rect.cut = subprobsolutioncut rect.value = rect.subrect1.value + rect.subrect2.value + rect.pattern.value } } memo[width][height] = rect homecoming rect }

java optimization memoization knapsack-problem

No comments:

Post a Comment