Check if Variable Values Satisfy Constraints
This article explains how to check whether a given combination of value assignments for the variables in your model satisfy all the constraints to produce a feasible solution.
You could add an assignment constraint for each of your variables that fixes the variable to the given value and then solve the model again. If the solver returns with the status infeasible, you know that these variable values do not satisfy all constraints.
Or, better yet, use the GMP function GMP::Solution::Check
.
Suppose we have the following simple model:
\({min}x\) | \(x + y \ge 5\) \(x,y \ge 0\)
and we want to check whether the assignment \(x=1\ ,\ y=1\) is actually a feasible solution.
First, add identifiers for the required variables (x,y,obj)
and constraint to an AIMMS project.
Then, add a math program identifier that links these variables and constraints.
Finally, use the following code to check whether the assignment \(x=1\ ,\ y=1\) is a feasible solution:
1!We want to check if x=1, y=1 is a feasible solution for our MathProgram
2
3!First we must generate the GMP for our MathProgram
4generatedMP := GMP::Instance::Generate(
5 MP : MathProgram ) ;
6
7!Then we must assign the values we want to check to the variables
8x := 1 ;
9y := 1 ;
10
11!Now retrieve the values from the variables in the model to
12!solution number 1 of this generated math program.
13
14GMP::Solution::RetrieveFromModel(
15 GMP : generatedMP ,
16 solution : 1 ) ;
17
18
19!We can now use the GMP::Solution::Check to check solution
20!number 1.
21GMP::Solution::Check(
22 GMP : generatedMP , !The Math program we want to use
23 solution : 1 , !The solution number we want to check
24 numInfeas : numberOfInfeasibilities , !store # of infeasibilities
25 sumInfeas : sumOfInfeasibilities , !store sum infeasibilities
26 skipObj : 1 ) ; !Objective can be skipped
27
28!Now we can check the number of infeasibilities. If there are no
29!infeasibilities, it means the variables values satisfy all constraints
30if numberOfInfeasibilities then
31 message := "Variable values do not satisfy all constraints:\n" ;
32 message += "Number of infeasibilities = " + numberOfInfeasibilities + "\n" ;
33 message += "Sum of infeasibilities = " + sumOfInfeasibilities ;
34 dialogMessage( message ) ;
35else
36 dialogMessage("Variable values are feasible for all constraints") ;
37endif ;
After running the above code, you will get the message that assignment \(x=1\ ,\ y=1\) does not satisfy all the constraints.
The example code above is available in a complete AIMMS project, attached below.