Save a Case from an AIMMS Procedure
It can be convenient to save a case from within a procedure.
For example, let’s say you want to run multiple scenarios and store the result of each one to a separate case. One possible solution is to manually save a case after running each scenario. However, for several long-running scenarios, this is very tedious.
Instead, you can save cases automatically with a procedure. This way you can run a procedure that solves all your scenarios and saves cases after each solve, and let it run unattended.
Writing the procedure
You can save a case in AIMMS using predefined case related functions.
To make it easier to save a case with any given name,
you can introduce a new procedure, say SaveCase
, with a string parameter
CaseName
as an input argument.
This procedure requires a local element parameter named CaseReference
with the
range AllCases
.
The body argument of the procedure should contain the following code:
1Procedure SaveCase {
2 Arguments: (sp_CaseName);
3 Body: {
4 OptionGetString("Data Management Style", sp_dms);
5 if sp_dms = "Disk Files and Folders" then
6
7 ! Save the case in the folder "data".
8 if not DirectoryExists( "data" ) then
9 DirectoryCreate("data");
10 endif ;
11 CaseFileSave( "data\\" + sp_CaseName, AllIdentifiers );
12
13 else
14 ! First try to find a case with the name indicated by CaseName.
15 ! If AIMMS can find this, it will store a reference to this case
16 ! in the element parameter ep_CaseReference
17 if ( not CaseFind( sp_CaseName, ep_CaseReference ) ) then
18
19 ! If no case with the name indicated by CaseName could be found, then
20 ! we try to create a case with this name. After creating the case, AIMMS
21 ! will store a reference in the ep_CaseReference element parameter to the
22 ! newly created case
23 if ( not CaseCreate( sp_CaseName, ep_CaseReference ) ) then
24
25 ! If there was an error while creating the case, notify the developer
26 ! by raising an error. If the raised error is not caught, AIMMS will
27 ! display it in the error window.
28 raise error "Could not create case with name " + sp_CaseName ;
29
30 endif;
31
32 endif;
33
34 ! If we got here, it means either a case with the indicated case name could be
35 ! found, or it was created.
36 ! Now instruct AIMMS to set this case to be the current case
37 CaseSetCurrent( ep_CaseReference );
38
39 ! And then instruct AIMMS to save the case
40 CaseSave( 0 );
41
42 endif;
43 }
44 StringParameter sp_CaseName {
45 Property: Input;
46 }
47 ElementParameter ep_CaseReference {
48 Range: AllCases;
49 }
50 StringParameter sp_dms;
51}
Calling the procedure
To save a case with the name “Case 1” from within any of your procedures, you can call your procedure as follows:
1SaveCase("Case 1") ;