(!******************************************************* * Mosel Example Problems * * ====================== * * * * file goalctr.mos * * ```````````````` * * Example for the use of the Mosel language * * (Pre-emptive and Archimedian goal programming * * using constraints) * * * * (c) 2008 Fair Isaac Corporation * * author: S. Heipcke, 2001, rev. Mar. 2006 * *******************************************************!) model GoalCtr ! Start a new model uses "mmxprs" ! Load the optimizer library forward procedure preemptive ! Declare some procedures that are forward procedure archimedian ! defined later declarations NGOALS=3 ! Number of goals x,y: mpvar ! Variables dev: array(1..2*NGOALS) of mpvar ! Deviation from goals mindev: linctr ! Objective function goal: array(1..NGOALS) of linctr ! Goal constraints end-declarations limit:= 100*x + 60*y <= 600 ! Define a constraint ! Define the goal constraints goal(1):= 7*x + 3*y >= 40 goal(2):= 10*x + 5*y = 60 goal(3):= 5*x + 4*y >= 35 ! Some declarations for a nice printout declarations STypes={CT_GEQ, CT_LEQ, CT_EQ} ATypes: array(STypes) of string end-declarations ATypes(CT_GEQ):= ">="; ATypes(CT_LEQ):= "<="; ATypes(CT_EQ):= "=" preemptive ! Pre-emptive goal programming archimedian ! Archimedian goal programming !*********************************************************************** procedure printsol forall(g in 1..NGOALS) if(dev(2*g).sol>0) then writeln(" Goal(",g,") deviation from target: -", dev(2*g).sol) elif(dev(2*g-1).sol>0) then writeln(" Goal(",g,") deviation from target: +", dev(2*g-1).sol) end-if end-procedure !*********************************************************************** procedure preemptive writeln("Pre-emptive:") (! Add successively the goals to the problem and solve it, until all goals have been added or a goal cannot be satisfied. This assumes that the goals are given ordered by priority. !) ! Remove (=hide) goal constraint from the problem forall(i in 1..NGOALS) goal(i).hidden:=true i:=0 while (i0) then writeln("Cannot satisfy goal ",i) break end-if end-do ! Solution printout writeln(" Goal", strfmt("Target",12), strfmt("Value",9)) forall(g in 1..i) writeln(strfmt(g,5), strfmt(ATypes(goal(g).type),3), strfmt(-goal(g).coeff,9), strfmt( goal(g).act-dev(2*g).sol+dev(2*g-1).sol ,9)) printsol end-procedure !*********************************************************************** procedure archimedian writeln("\nArchimedian:") declarations Penalty: array(1..NGOALS) of real ! Penalties for goal constraints end-declarations Penalty:: [8, 3, 1] mindev:=0 ! Re-initialize objective function ! Define the objective as weighted sum of the deviation variables forall(g in 1..NGOALS) do case goal(g).type of ! Add deviation variable(s) CT_GEQ: do deviation:= dev(2*g) mindev += Penalty(g)*deviation end-do CT_LEQ: do deviation:= -dev(2*g-1) mindev += Penalty(g)*dev(2*g-1) end-do CT_EQ : do deviation:= dev(2*g) - dev(2*g-1) mindev += Penalty(g)*(dev(2*g) + dev(2*g-1)) end-do else writeln("Wrong constraint type") break end-case goal(g)+= deviation end-do minimize(mindev) ! Solve the LP-problem writeln(" Solution: x: ", x.sol, ", y: ", y.sol) ! Solution printout writeln(" Goal", strfmt("Target",12), strfmt("Value",9), strfmt("Penalty",9)) forall(g in 1..NGOALS) writeln(strfmt(g,5), strfmt(ATypes(goal(g).type),3), strfmt(-goal(g).coeff,9), strfmt( goal(g).act-dev(2*g).sol+dev(2*g-1).sol ,9), strfmt(Penalty(g),9)) printsol end-procedure end-model