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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Procedure SaveCase {
    Arguments: (sp_CaseName);
    Body: {
        OptionGetString("Data Management Style", sp_dms);
        if sp_dms = "Disk Files and Folders" then

            ! Save the case in the folder "data".
            if not DirectoryExists( "data" ) then
                DirectoryCreate("data");
            endif ;
            CaseFileSave( "data\\" + sp_CaseName, AllIdentifiers );

        else
            ! First try to find a case with the name indicated by CaseName.
            ! If AIMMS can find this, it will store a reference to this case
            ! in the element parameter ep_CaseReference
            if ( not CaseFind( sp_CaseName, ep_CaseReference ) ) then

                ! If no case with the name indicated by CaseName could be found, then
                ! we try to create a case with this name. After creating the case, AIMMS
                ! will store a reference in the ep_CaseReference element parameter to the
                ! newly created case
                if ( not CaseCreate( sp_CaseName, ep_CaseReference ) ) then

                    ! If there was an error while creating the case, notify the developer
                    ! by raising an error. If the raised error is not caught, AIMMS will
                    ! display it in the error window.
                    raise error "Could not create case with name " + sp_CaseName ;

                endif;

            endif;

            ! If we got here, it means either a case with the indicated case name could be
            ! found, or it was created.
            ! Now instruct AIMMS to set this case to be the current case
            CaseSetCurrent( ep_CaseReference );

            ! And then instruct AIMMS to save the case
            CaseSave( 0 );

        endif;
    }
    StringParameter sp_CaseName {
        Property: Input;
    }
    ElementParameter ep_CaseReference {
        Range: AllCases;
    }
    StringParameter sp_dms;
}

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:

1
SaveCase("Case 1") ;

AIMMS project download