Mathematical Modeling with LPL : Explain and run a Model
Problem Statement
Modeling Description
||
Back to MatMod
model jug "The 3-Jug Problem"; set k "Set of jugs"; i,j "The set of states"; e{i,j} "Transition links"; p{i,j} "shortest path"; parameter N "Number of jugs"; T "Total liquid"; C{k} "capacities of the jugs"; x{k,i} "the states"; string S{i} "State name"; InitState "Starting state"; GoalState "Arrivng State"; ;EnumerateStates; S{i}:='('&x[1,i]&','&x[2,i]&','&x[3,i]&')'; e{i,j}:= or{k} ( x[k,i]=x[k,j] and (x[k%N+1,j]=C[k%N+1] or x[(k+1)%N+1,j]=0) or x[k,i]=x[k,j] and (x[k%N+1,j]=0 or x[(k+1)%N+1,j]=C[(k+1)%N+1])); parameter s:=argmax{i} (S=InitState) "from"; t:=argmax{i} (S=GoalState) "to"; if Graph.SPath(e,p,s,t)>=99999999 then Write('No path exists\n'); end; --DrawGraph; DrawJug(8,0,0, 0,0); DrawJug(4,4,0, 25,0); Draw.Arrow(16,4,23,4,-2,0,3); Draw.Text('Abstraction',15,-3.5,16,0,3,1.3); parameter SPdraw{i}:=[16,7,6,13,12,3,2,9]; n; parameter SPdraw1{i}:=[16,9]; {i|SPdraw1} (n:=n+1, Draw.Ellipse(S[SPdraw1],if(n>4,(n-4)*10-5,n*25-17),if(n>4,-10,-6),3,1,1,0)); Draw.Arrow(16,-6,23,-6,-2,0,3); model data; T:=8; InitState:='(8,0,0)'; //sum must be T GoalState:='(4,4,0)'; //sum must be T N:=3; k:=1..N; C{k}:=[8,5,3]; end; model EnumerateStates; {a in 0..C[1],b in 0..C[2], c in 0..C[3]} if(a+b+c=T and (a=0 or a=C[1] or b=0 or b=C[2] or c=0 or c=C[3]), (Addm(i,#i+1), x[1,#i]:=a,x[2,#i]:=b,x[3,#i]:=c) ); end model DrawGraph; parameter PI:=3.14159; xa{i}:=15*Sin(PI/#i+2*PI*(i-1)/#i); ya{i}:=15*Cos(PI/#i+2*PI*(i-1)/#i); Draw.Scale(15,15); Draw.DefFont('Verdana',10); {e[i,j]} Draw.Arrow(xa[i],ya[i],xa[j],ya[j],2); --{p[i,j]} Draw.Arrow(xa[j],ya[j],xa[i],ya[i],2,3,3); {i}Draw.Ellipse(S,xa,ya,2,1,1,0); end model DrawJug(integer a;b;c; x;y); Draw.Scale(10,-15); Draw.DefFont('Verdana',12); model oneJug(integer h;a; x;y); Draw.Ellipse(x+2,y,2,.5,if(a>0,5,1),0); Draw.Rect(x,y,4,1,1,1); if a>0 then Draw.Rect(x,y,4,y+a,5,5); Draw.Ellipse(x+2,a,2,.5,5,1); end; Draw.Ellipse(x+2,h,2,.5,1,0)); Draw.Line(x,h,x,0); Draw.Line(x+4,h,x+4,0); Draw.Text(h&'L',x+1.5,-1.3); Draw.Text(a&'L',x+1.5,1); end; oneJug(8,a,x,y); oneJug(5,b,x+5,y); oneJug(3,c,x+10,y); end end
Problem Statement
Modeling Description
Back to model
The solution is as follows:
The LOG-File output of LPL is as follows: