haorenhui
2023-10-30 6d6cc10d9e8e242661da7fd655dec155a09d676c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
Quintiq file version 2.0
#parent: #root
StaticMethod Create (LibOpt_Run owner, MathematicalProgram program) as LibOpt_SnapshotMP
{
  Description: 'Create a new MP snapshot.'
  TextBody:
  [*
    // Define CPLEX consts
    CPX_MULTIOBJ_BESTOBJVAL := 15;
    
    goals := program.Goals( relget );
    priorities := selectuniquevalues( goals, Elements, goal, true, goal.Priority() );
    priorities := priorities.Sort().Reverse();
    
    goal_values := RealVector::Construct( priorities.Size() );
    bounds := RealVector::Construct( priorities.Size() );
    
    if( goals.Size() = 1 and goals.First().Name() = '' ) // Non hierarchical goals
    {
      bound := program.GoalValue() + ifexpr( program.GoalSense() = MPGoalSense::Maximize(), 1, -1 ) * program.AbsoluteGap();
      bounds.Set( 0, bound );
      goal_values.Set( 0, program.GoalValue() );
    }
    else // Hierarchical goals
    {
      for( i := priorities.Size() - 1; i >= 0; i-- )
      {
        priority := priorities.Element( i );
        value := sum( goals, Elements, goal, goal.Priority() = priority, goal.Weight() * goal.OptimalValue() );
        goal_values.Set( i, value );
        
        bounds.Set( i, guard( program.Statistics().MultiObjGetRealInfo( i, CPX_MULTIOBJ_BESTOBJVAL ), value ) );
      }
    }
    
    // Calculate gap
    absolute_gaps := bounds.Minus( goal_values ).Abs();
    relative_gaps := null( RealVector );
    if( goal_values.Equals( 0.0 ).IsAllFalse() )
    {
      relative_gaps := absolute_gaps.Div( goal_values ).Abs();
    }
    else
    {
      // Solve division by zero error
      relative_gaps := RealVector::Construct( bounds.Size() );
      for( i := 0; i < relative_gaps.Size(); i++ )
      {
        goal := goal_values.Get( i );
        relative_gaps.Set( i, ifexpr( goal = 0.0,
                                      ifexpr( absolute_gaps.Get( i ) = 0,
                                              0.0,
                                              Real::MaxReal() ),
                                      abs( absolute_gaps.Get( i ) / goal ) ) );
      }
    }
    
    // Set whether the MIP is fixed
    fixed := program.IsFixedMixedIntegerProblem()
          or program.IsFixedMixedIntegerQuadraticProblem();
          
    // Set whether the problem is MIP
    mip := program.IsFixedMixedIntegerProblem()
        or program.IsFixedMixedIntegerQuadraticProblem()
        or program.IsMixedIntegerProblem()
        or program.IsMixedIntegerQuadraticallyConstrainedProblem()
        or program.IsMixedIntegerQuadraticProblem()
          
    // Set whether the problem is quadratically constrained
    qconstr := program.IsQuadraticallyConstrainedProblem()
            or program.IsMixedIntegerQuadraticallyConstrainedProblem()
    
    // Set whether the problem is quadratic
    quadr := program.IsQuadraticProblem()
          or program.IsMixedIntegerQuadraticProblem()
          or program.IsFixedMixedIntegerQuadraticProblem()
    
    snapshot := owner.Snapshot( relnew, LibOpt_SnapshotMP,
                                AbsoluteGap := program.AbsoluteGap(),
                                AbsoluteGaps := absolute_gaps.AsBinaryValue(),
                                RelativeGap := program.RelativeGap(),
                                RelativeGaps := relative_gaps.AsBinaryValue(),
                                GoalScore := goal_values.Get( 0 ),
                                GoalScores := goal_values.AsBinaryValue(),
                                Bound := bounds.Get( 0 ),
                                Bounds := bounds.AsBinaryValue(),
                                IsFeasible := LibOpt_SuboptimizerMP::CheckIsFeasible( program ),
                                IsOptimal := program.Optimal(),
                                NumberOfSolutions := program.NumberOfSolutions(),
                                NrVariables := program.Variables( relsize ),
                                NrConstraints := program.Constraints( relsize ),
                                NrGoalLevels := priorities.Size(),
                                IsFixed := fixed,
                                IsMIP := mip,
                                IsQuadraticallyConstrained := qconstr,
                                IsQuadratic := quadr,
                                Kappa := program.Statistics().Kappa() );
    snapshot.Capture( program );
    
    return snapshot;
  *]
}