Identify Differences

In this article we’ll identify the cause of data differences in a model after a refactorization.

The process we’re following can be summarized like this:

  1. You’ll want to separate out each element, so it will be easy to break down the differences.

  2. Then send each identifier to a separate output file in an output folder.

  3. Do that for each version of the model, so you’ll have two output folders each containing output files to be compared.

  4. Finally you’ll compare the two output folders with a diff tool to find the differences between the individual files.

See the procedure below for an example:

 1Procedure pr_TraceValues {
 2    Arguments: (s_someIds,sp_outputFoldername);
 3    Body: {
 4        block where
 5            single_column_display := 1, ! Each element on a single line; it is easy to identify single element differences.
 6            listing_number_precision := 6 ; ! Increase this if you want to identify potential numerical causes.
 7
 8            ! Ensure the output folder exists.
 9            if not DirectoryExists( sp_outputFolderName ) then
10                DirectoryCreate( sp_outputFolderName );
11            endif ;
12
13            ! Write each identifier to a separate output file.
14            !
15            ! To do so, we need to put each identifier in consecutively in a singleton set
16            ! and then we can use the write statement.
17            !
18            ! :: cannot be part of a filename, so replace it with __.
19            for i_sid do
20
21                ! Fill the single ton set with the identifier at hand.
22                s_oneId := {};
23                s_oneId := i_sid ;
24
25                ! Construct the name of the output file.
26                sp_outputBase := formatString("%e",i_sid);
27                sp_outputBase := FindReplaceStrings( sp_outputBase, ":", "_" );
28                sp_outputFilename := formatString("%s\\%s", sp_outputFolderName, sp_outputBase);
29
30                ! Actually write the identifier
31                write s_oneId to file sp_outputFilename ;
32
33            endfor ;
34
35        endblock ;
36    }
37    Set s_someIds {
38        SubsetOf: AllIdentifiers;
39        Index: i_sid;
40        Property: Input;
41    }
42    Set s_oneId {
43        SubsetOf: AllIdentifiers;
44    }
45    StringParameter sp_outputFoldername {
46        Property: Input;
47    }
48    StringParameter sp_outputBase;
49    StringParameter sp_outputFilename;
50}

You can call the procedure with the code below:

1Procedure pr_ExampleTraceValues {
2    Body: {
3        s_outputIds := AllUpdatableIdentifiers * ( mod1 + mod2 );
4        pr_TraceValues( s_outputIds, "myModuleData" );
5    }
6    Set s_outputIds {
7        SubsetOf: AllIdentifiers;
8    }
9}

To compare, call the procedure on both versions of the project, and then compare the output folders using a diff tool such as WinMerge.

Now it’s time to use that diff to figure out what happened.

When definitions of sets and parameters haven’t changed, output a subset of AllUpdatableIdentifiers; these are the sets and parameters without definition.

Let’s say we’re interested in the sets and parameters related to a particular mathematical program, such as the one below:

 1Module Mod3 {
 2    Prefix: m3;
 3    Set s_myVars {
 4        SubsetOf: AllVariables;
 5        Definition: AllVariables * Mod3;
 6    }
 7    Set s_myCons {
 8        SubsetOf: AllConstraints;
 9        Definition: AllConstraints * Mod3;
10    }
11    Variable v_obj {
12        Range: free;
13    }
14    MathematicalProgram mp_Mine {
15        Objective: v_obj;
16        Direction: minimize;
17        Constraints: s_myCons;
18        Variables: s_myVars;
19        Type: Automatic;
20    }
21}

Then we can use the following to output all identifiers making up the mathematical program like this:

1s_outputIds := data { v_obj } + m3::s_myVars + m3::s_myCons ;
2s_outputIds += ReferencedIdentifiers( s_outputIds, AllAttributeNames, 1 );
3pr_TraceValues( s_outputIds, "myMPData" );

In the above code, we include the variables because the bound information is essential to the mathematical program. We also include the constraints; this may be obsolete unless you are interested in the shadow prices as well.

Example Download

You can download the section (.ams file) that contains this procedure and sample below: * TracingValues.ams

To use it, you’ll need to Import a section to your project.