(!*******************************************************
* 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 (i<NGOALS) do
i+=1
goal(i).hidden:=false ! Add (=unhide) the next goal
case goal(i).type of ! Add deviation variable(s)
CT_GEQ: do
deviation:= dev(2*i)
mindev += deviation
end-do
CT_LEQ: do
deviation:= -dev(2*i-1)
mindev += dev(2*i-1)
end-do
CT_EQ : do
deviation:= dev(2*i) - dev(2*i-1)
mindev += dev(2*i) + dev(2*i-1)
end-do
else writeln("Wrong constraint type")
break
end-case
goal(i)+= deviation
minimize(mindev) ! Solve the LP-problem
writeln(" Solution(", i,"): x: ", x.sol, ", y: ", y.sol)
goal(i)-= deviation ! Remove deviation variable(s) from goal
if(getobjval>0) 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
|
(!*******************************************************
* Mosel Example Problems *
* ====================== *
* *
* file goalobj.mos *
* ```````````````` *
* Example for the use of the Mosel language *
* (Archimedian and pre-emptive goal programming *
* using objective functions) *
* *
* (c) 2008 Fair Isaac Corporation *
* author: S. Heipcke, 2001 *
*******************************************************!)
model GoalObj ! 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
Type: array(1..NGOALS) of string ! Type of goal objectives
Sense: array(1..NGOALS) of string ! Sense of goal objectives
Weight: array(1..NGOALS) of real ! Weights of goals
Deviation: array(1..NGOALS) of real ! Max. deviation from goals
Target: array(1..NGOALS) of real ! Target (RHS) values for goals
wobj: linctr ! Objective function
goal: array(1..NGOALS) of linctr ! Goal constraints
end-declarations
limit:= 42*x + 13*y <= 100 ! Define a constraint
! Define the goal objectives
Weight::[100, 1, 0.1]
Type:: ["perc", "abs", "perc"]
Sense:: ["max", "min", "max"]
Deviation:: [10, 4, 20]
goal(1):= 5*x + 2*y - 20
goal(2):= -3*x + 15*y - 48
goal(3):= 1.5*x + 21*y - 3.8
archimedian ! Archimedian goal programming
preemptive ! Pre-emptive goal programming
!***********************************************************************
procedure archimedian
writeln("Archimedian:")
! Define the objective function as weighted sum of the goals
forall(g in 1..NGOALS)
if(Sense(g)="max") then
wobj-=Weight(g)*goal(g)
else
wobj+=Weight(g)*goal(g)
end-if
minimize(wobj) ! Solve the LP-problem
! Solution printout
writeln(" Solution: x: ", x.sol, ", y: ", y.sol)
writeln(" Goal", strfmt("Target",9), strfmt("Value",12))
forall(g in 1..NGOALS) do
writeln(strfmt(g,5), strfmt(Sense(g), 9),
strfmt(goal(g).act + goal(g).coeff , 12,6))
end-do
end-procedure
!***********************************************************************
procedure preemptive
writeln("\nPre-emptive:")
(!
Optimize successively the goals. After optimizing a goal turn it
into a constraint.
!)
i:=0
while (i<NGOALS) do
i+=1
case Sense(i) of
"max": do
maximize(goal(i)) ! Optimize the next goal
if(getprobstat<>XPRS_OPT) then
writeln("Cannot satisfy goal ",i)
break
else
Target(i):=getobjval
if (Type(i)="perc") then
Target(i)-= abs(Target(i))*Deviation(i)/100
else
Target(i)-= Deviation(i)
end-if
if(i<NGOALS) then
goal(i):= goal(i) >= Target(i) ! Turn goal into a constraint
else
goal(i).type:=CT_GEQ ! Only for printout
end-if
end-if
end-do
"min": do
minimize(goal(i)) ! Optimize the next goal
if(getprobstat<>XPRS_OPT) then
writeln("Cannot satisfy goal ",i)
break
else
Target(i):=getobjval
if (Type(i)="perc") then
Target(i)+= abs(Target(i))*Deviation(i)/100
else
Target(i)+= Deviation(i)
end-if
if(i<NGOALS) then
goal(i):= goal(i) <= Target(i) ! Turn goal into a constraint
else
goal(i).type:=CT_LEQ ! Only for printout
end-if
end-if
end-do
else writeln("Unknown objective sense")
break
end-case
writeln(" Solution(", i,"): x: ", x.sol, ", y: ", y.sol)
end-do
! Some declarations for a nice printout
declarations
STypes={CT_GEQ, CT_LEQ}
ATypes: array(STypes) of string
end-declarations
ATypes::([CT_GEQ, CT_LEQ])[">=", "<="]
! Solution printout
writeln(" Goal", strfmt("Target",15), strfmt("Value",12))
forall(g in 1..i) do
write(strfmt(g,5), strfmt(ATypes(goal(g).type), 3),
strfmt(Target(g), 12,6))
if(g=NGOALS) then
writeln(strfmt(getobjval, 12,6))
else
writeln(strfmt(goal(g).act+goal(g).coeff+Target(g) , 12,6))
end-if
end-do
end-procedure
end-model
|