Looping over optimization
We are going to modify the model foliodata.mos from the previous chapter in such a way that the problem is re-optimized repeatedly with different limits on the percentage of high-risk values.
In detail, the model will be transformed to implement the following algorithm:
- Definition of the part of the model that remains unchanged by the parameter changes.
- For every parameter value:
- Re-define the constraint limiting the percentage of high-risk values.
- Solve the resulting problem.
- If the problem is feasible: store the solution values.
- Draw the result graph.
To store the solution value and the total estimated deviation of the result after each optimization run, we declare the following two arrays:
declarations SOLRET: array(range) of real ! Solution values (total return) SOLDEV: array(range) of real ! Solution values (average deviation) end-declarations
The following code fragment introduces a loop around the definition of the constraint limiting the portion of high-risk shares and the solution procedure. To be able to override its previous definition at every iteration, we now give this constraint a name, Risk. If the constraint did not have a name, then each time the loop was executed, a new constraint would be added, and the existing constraint would not be replaced.
ct:=0 forall(r in 0..20) do ! Limit the percentage of high-risk values Risk:= sum(s in RISK) frac(s) <= r/20 maximize(Return) ! Solve the problem if (getprobstat = XPRS_OPT) then ! Save the optimal solution value ct+=1 SOLRET(ct):= getobjval SOLDEV(ct):= getsol(sum(s in SHARES) DEV(s)*frac(s)) else writeln("No solution for high-risk values <= ", 100*r/20, "%") end-if end-do
Above we have used the second form of the forall loop, namely forall/do. This form must be used when several statements are included in the loop. The loop is terminated by end-do.
Another new feature in this code extract is the if/then/else/end-if statement. We only want to save the values for a problem instance if the optimal solution has been found—the solution status is obtained with function getprobstat and tested whether it is `solved to optimality', represented by the constant XPRS_OPT.
The selection statement has two other forms, if/then/end-if and if/then/elif/then/ else/end-if where elif/then may be repeated several times.
For further examples and a complete description of all loops and selection statements available in Mosel the reader is refered to the `Mosel User Guide'.
© 2001-2019 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.