Selections
Mosel provides several statements to express a selection between different actions to be taken in a program. The simplest form of a selection is the if-then statement:
- if-then: `If a condition holds do something'. For example:
if A >= 20 then x <= 7 end-if
For an integer number A and a variable x of type mpvar, x is constrained to be less or equal to 7 if A is greater or equal 20.
Note that there may be any number of expressions between then and end-if, not just a single one.
In other cases, it may be necessary to express choices with alternatives.
- if-then-else: `If a condition holds, do this, otherwise do something else'. For example:
if A >= 20 then x <= 7 else x >= 35 end-if
Here the upper bound 7 is applied to the variable x if the value of A is greater or equal 20, otherwise the lower bound 35 is applied to it. - if-then-elif-then-else: `If a condition holds do this, otherwise, if a second condition holds do something else etc.'
if A >= 20 then x <= 7 elif A <= 10 then x >= 35 else x = 0 end-if
Here the upper bound 7 is applied to the variable x if the value of A is greater or equal 20, and if the value of A is less or equal 10 then the lower bound 35 is applied to x. In all other cases (that is, A is greater than 10 and smaller than 20), x is fixed to 0.
Note that this could also be written using two separate if-then statements but it is more efficient to use if-then-elif-then[-else] if the cases that are tested are mutually exclusive. - case: `Depending on the value of an expression do something'.
case A of -MAX_INT..10 : x >= 35 20..MAX_INT : x <= 7 12, 15 : x = 1 else x = 0 end-case
Here the upper bound 7 is applied to the variable x if the value of A is greater or equal 20, and the lower bound 35 is applied if the value of A is less or equal 10. In addition, x is fixed to 1 if A has value 12 or 15, and fixed to 0 for all remaining values.
An example for the use of the case statement is given in Section Goal Programming.
The following example (model minmax.mos) uses the if-then-elif-then statement to compute the minimum and the maximum of a set of randomly generated numbers:
model Minmax declarations SNumbers: set of integer LB=-1000 ! Elements of SNumbers must be between LB UB=1000 ! and UB end-declarations ! Generate a set of 50 randomly chosen numbers forall(i in 1..50) SNumbers += {round(random*200)-100} writeln("Set: ", SNumbers, " (size: ", getsize(SNumbers), ")") minval:=UB maxval:=LB forall(p in SNumbers) if p<minval then minval:=p elif p>maxval then maxval:=p end-if writeln("Min: ", minval, ", Max: ", maxval) end-model
Instead of writing the loop above, it would of course be possible to use the corresponding operators min and max provided by Mosel:
writeln("Min: ", min(p in SNumbers) p, ", Max: ", max(p in SNumbers) p)
It is good programming practice to indent the block of statements in loops or selections as in the preceding example so that it becomes easy to get an overview where the loop or the selection ends. — At the same time this may serve as a control whether the loop or selection has been terminated correctly (i.e. no end-if or similar key words terminating loops have been left out).
© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.