Miscellaneous functions¶
There are some msscellaneous functions provided in PyMathProg for less common tasks. Here we will talk about:
Deleting model elements¶
Deleting variables and/or constraints from a model is done by invoking the delete() method on a variable or constraint.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from pymprog import *
begin('trader')
x = var('x', 3)
c = par('c', [100, 300, 50])
b = par('b', [93000, 101, 201])
maximize(sum(c[i]*x[i] for i in range(3)), 'Profit')
300*x[0] + 1200*x[1] + 120*x[2] <= b[0]
0.5*x[0] + x[1] + 0.5*x[2] <= b[1]
r = x[0] + x[1] + x[2] <= b[2]
solve()
sensitivity()
r.delete()
# deleting a basic varriable destroys the basis
x[1].delete()
# restore the standard basis
std_basis()
solve()
sensitivity()
end()
|
The output is as follows:
GLPK Simplex Optimizer, v4.60
3 rows, 3 columns, 9 non-zeros
* 0: obj = -0.000000000e+00 inf = 0.000e+00 (3)
* 2: obj = 2.560000000e+04 inf = 0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
PyMathProg 1.0 Sensitivity Report Created: 2016/12/11 Sun 09:05AM
================================================================================
Variable Activity Dual.Value Obj.Coef Range.From Range.Till
--------------------------------------------------------------------------------
*x[0] 94 0 100 87.5 150
*x[1] 54 0 300 200 366.667
x[2] 0 -20 50 -inf 70
================================================================================
================================================================================
Constraint Activity Dual.Value Lower.Bnd Upper.Bnd RangeLower RangeUpper
--------------------------------------------------------------------------------
R1 93000 0.166667 -inf 93000 61200 121200
R2 101 100 -inf 101 77.5 118.667
*R3 148 0 -inf 201 148 148
================================================================================
GLPK Simplex Optimizer, v4.60
2 rows, 2 columns, 4 non-zeros
* 3: obj = 2.020000000e+04 inf = 0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
PyMathProg 1.0 Sensitivity Report Created: 2016/12/11 Sun 09:05AM
================================================================================
Variable Activity Dual.Value Obj.Coef Range.From Range.Till
--------------------------------------------------------------------------------
*x[0] 202 0 100 50 1.79769e+308
x[2] 0 -50 50 -inf 100
================================================================================
================================================================================
Constraint Activity Dual.Value Lower.Bnd Upper.Bnd RangeLower RangeUpper
--------------------------------------------------------------------------------
*R1 60600 0 -inf 93000 60600 60600
R2 101 200 -inf 101 0 155
================================================================================
Saving model and solution¶
It is possible to save the model and/or the solution to a text file. The example below shows how to do that through the global function save(...).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from pymprog import *
begin('save')
x = var('x', 3)
x[2].kind = int
c = par('c', [100, 300, 50])
b = par('b', [93000, 101, 201])
maximize(sum(c[i]*x[i] for i in range(3)), 'Profit')
300*x[0] + 1200*x[1] + 120*x[2] <= b[0]
0.5*x[0] + x[1] + 0.5*x[2] <= b[1]
r = x[0] + x[1] + x[2] <= b[2]
solve()
save(mps='_save.mps', sol='_save.sol',
clp='_save.clp', glp='_save.glp',
sen='_save.sen', ipt='_save.ipt',
mip='_save.mip')
end()
|
Note that the sensitivity report just saved is produced by GLPK. The format is not the same as the report produced by the global function sensitivity() in PyMathProg.
Karush-Kuhn-Tucker conditions¶
The KKT condition tells how much error are there in terms of satisfying the constraints. Errors may be measured both absolutely or relatively. To produce KKT conditions, just call the routine KKT() after solving a model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from pymprog import *
c = (10, 6, 4)
A = [ ( 1, 1, 1),
( 9, 4, 5),
( 2, 2, 6) ]
b = (10, 60, 30)
begin('basic') # begin modelling
verbose(True) # be verbose
x = var('x', 3) #create 3 variables
maximize(sum(c[i]*x[i] for i in range(3)))
for i in range(3):
sum(A[i][j]*x[j] for j in range(3)) <= b[i]
solve() # solve the model
print(KKT())
end() #Good habit: do away with the model
|
The produced output is as follows:
Max : 10 * x[0] + 6 * x[1] + 4 * x[2]
R1: x[0] + x[1] + x[2] <= 10
R2: 9 * x[0] + 4 * x[1] + 5 * x[2] <= 60
R3: 2 * x[0] + 2 * x[1] + 6 * x[2] <= 30
GLPK Simplex Optimizer, v4.60
3 rows, 3 columns, 9 non-zeros
* 0: obj = -0.000000000e+00 inf = 0.000e+00 (3)
* 2: obj = 7.600000000e+01 inf = 0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
Karush-Kuhn-Tucker optimality conditions:
=========================================
Solver used for this solution: simplex
1) Error for Primal Equality Constraints:
----------------------------------------
Largest absolute error: 0.000000 (row id: 0)
Largest relative error: 0.000000 (row id: 0)
2) Error for Primal Inequality Constraints:
-------------------------------------------
Largest absolute error: 0.000000 (row id: 0)
Largest relative error: 0.000000 (row id: 0)
1) Error for Dual Equality Constraints:
----------------------------------------
Largest absolute error: 0.000000 (var id: 0)
Largest relative error: 0.000000 (var id: 0)
2) Error for Dual Inequality Constraints:
-------------------------------------------
Largest absolute error: 0.000000 (var id: 0)
Largest relative error: 0.000000 (var id: 0)
__del__ is deleting problem: basic