Print the dual model via docplex - docplex

I have developed a linear program and implemented it in Python via docplex. I would like to know how one can print the dual model using docplex? I have seen similar posts for other programming languages, but I was unable to find relevant discussions for docplex.
How can I export dual model from Cplex using java?

I would use cplex command line from python through an external call.
If I use the zoo example
import os
from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.continuous_var(name='nbBus40')
nbbus30 = mdl.continuous_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.solve(log_output=True,)
mdl.export("buses.lp")
for v in mdl.iter_continuous_vars():
print(v," = ",v.solution_value)
os.system("cplex -c \"read buses.lp\" \"write buses.dua\"")
then in buses.dua I get
NAME buses.lp.dual
ROWS
N rhs
G nbBus40
G nbBus30
COLUMNS
kids rhs -300
kids nbBus40 -40
kids nbBus30 -30
RHS
obj nbBus40 -500
obj nbBus30 -400
ENDATA
which gives in lp format
Minimize
rhs: - 300 kids
Subject To
nbBus40: - 40 kids >= -500
nbBus30: - 30 kids >= -400
End

Related

How to express exponential of a variable in docplex?

I need to define a constraint as follows:
mdl.add_constraints(p_pg[plan, segment] == np.exp(u_pg[plan, segment] for plan in range(1, p+1) for segment in range(1, g+1))
In this constraint both p_pg and u_pg are variable and are defined as mdl.continuous_var_dict. However I get the following error:
loop of ufunc does not support argument 0 of type Var which has no callable exp method
Can anyone help how to define this constraint?
exp is not linear so you could either try to do a piecewise linear approximation or use Constraint Programming within CPLEX.
See this example in Easy optimization with python
from docplex.cp.model import CpoModel
mdl = CpoModel(name='buses')
nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
mdl.add(nbbus40*40 + nbbus30*30 >= 300)
#non linear objective
mdl.minimize(mdl.exponent(nbbus40)*500 + mdl.exponent(nbbus30)*400)
msol=mdl.solve()
print(msol[nbbus40]," buses 40 seats")
print(msol[nbbus30]," buses 30 seats")

Shadow price - XPRESS - Pulp

I am trying to obtain the shadow price of a LP problem using Pulp and the XPRESS solver.
With the CBC solver, the .pi command works fine :
import pulp
my_lp_problem = pulp.LpProblem("My LP Problem", pulp.LpMinimize)
a = pulp.LpVariable("L",lowBound=-10, upBound=10, cat='Continuous')
my_lp_problem += a<= 2
my_lp_problem += a >= -5
my_lp_problem += a
my_lp_problem.solve(pulp.PULP_CBC_CMD())
for name, c in list(my_lp_problem.constraints.items()):
print(c.pi)
gives
0.0
1.0
However, using XPRESS :
import pulp
my_lp_problem = pulp.LpProblem("My LP Problem", pulp.LpMinimize)
a = pulp.LpVariable("L",lowBound=-10, upBound=10, cat='Continuous')
my_lp_problem += a<= 2
my_lp_problem += a >= -5
my_lp_problem += a
my_lp_problem.solve(pulp.XPRESS())
for name, c in list(my_lp_problem.constraints.items()):
print(c.pi)
gives
None
None
Does any one know how to solve this issue?
Thank you!
Currently, the XPRESS api in PuLP does not suppport getting the shadow prices as far as I see. Feel free to open an issue in the project's site: https://github.com/coin-or/pulp/issues

Optimizing a Summation function - GEKKO

I just started learning optimization and I have some issues finding the optimal value for the problem below.
Note: This is just a random problem that came to my mind and has no real application.
Problem:
where x can be any value from the list ([2,4,6]) and y is between 1 and 3.
My attempt:
from gekko import GEKKO
import numpy as np
import math
def prob(x,y,sel):
z = np.sum(np.array(x)*np.array(sel))
cst = 0
i=0
while i <= y.VALUE:
fact = 1
for num in range(2, i + 1): # find the factorial value
fact *= num
cst += (z**i)/fact
i+=1
return cst
m = GEKKO(remote=False)
sel = [2,4,6] # list of possible x values
x = m.Array(m.Var, 3, **{'value':1,'lb':0,'ub':1, 'integer': True})
y = m.Var(value=1,lb=1,ub=3,integer=True)
# switch to APOPT
m.options.SOLVER = 1
m.Equation(m.sum(x) == 1) # restrict choice to one selection
m.Maximize(prob(x,y,sel))
m.solve(disp=True)
print('Results:')
print(f'x: {x}')
print(f'y : {y.value}')
print('Objective value: ' + str(m.options.objfcnval))
Results:
----------------------------------------------------------------
APMonitor, Version 0.9.2
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 4
Intermediates: 0
Connections : 0
Equations : 2
Residuals : 2
Number of state variables: 4
Number of total equations: - 1
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 3
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: -0.00 NLPi: 2 Dpth: 0 Lvs: 0 Obj: -7.00E+00 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 0.024000000000000004 sec
Objective : -7.
Successful solution
---------------------------------------------------
Results:
x: [[0.0] [0.0] [1.0]]
y : [1.0]
Objective value: -7.0
x should be [0,0,1] (i.e. 6) and y should be 3 to get the maximum value (61). x value I get is correct but for some reason the y value I get is wrong. What is causing this issue ? Is there something wrong with my formulation ? Also it would be very helpful if you could kindly point me towards more information about the various notations (like Tm, NLPi, etc) in APOPT solver output.
Here is a solution in gekko:
x=6.0
y=3.0
You'll need to use the gekko functions to build the functions and pose the problem in a way so that the equations don't change as the variable values change.
from gekko import GEKKO
import numpy as np
from scipy.special import factorial
m = GEKKO(remote=False)
x = m.sos1([2,4,6])
yb = m.Array(m.Var,3,lb=0,ub=1,integer=True)
m.Equation(m.sum(yb)==1)
y = m.sum([yb[i]*(i+1) for i in range(3)])
yf = factorial(np.linspace(0,3,4))
obj = x**0/yf[0]
for j in range(1,4):
obj += x**j/yf[j]
m.Maximize(yb[j-1]*obj)
m.solve()
print('x='+str(x.value[0]))
print('y='+str(y.value[0]))
print('Objective='+str(-m.options.objfcnval))
For your problem, I used a Special Ordered Set (type 1) to get the options of 2, 4, or 6. To select y as 1, 2, or 3 I calculated all possible values and then used a binary selector yb to choose one. There is a constraint that only one of them can be used with m.sum(yb)==1. There are gekko examples, documentation, and a short course available if you need additional resources.
Here is the solver output:
----------------------------------------------------------------
APMonitor, Version 0.9.2
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 1
Constants : 0
Variables : 11
Intermediates: 1
Connections : 4
Equations : 10
Residuals : 9
Number of state variables: 11
Number of total equations: - 7
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 4
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 6 Dpth: 0 Lvs: 0 Obj: -6.10E+01 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 0.047799999999999995 sec
Objective : -61.
Successful solution
---------------------------------------------------
x=6.0
y=3.0
Objective=61.0
Here is more information on the solver APOPT options. The iteration summary describes the branch and bound progress. It is Iter=iteration number, Tm=time to solve the NLP, NLPi=NLP iterations, Dpth=depth in the branching tree, Lvs=number of candidates leaves, Obj=NLP solution objective, and Gap=gap between integer solution and best non-integer solution.
equation to be minimzedhey how to solve these type of prolems
problem:
Minimization
summation(xij*yij)
i=from 0 to 4000
j= from 0 to 100
y is coast matrix given
m = GEKKO(remote=False)
dem_var = m.Array(m.Var,(4096,100),lb=0)
for i,j in s_d:
m.Minimize(sum([dem_var[i][j]*coast_new[i][j]]))
here y=coast_new
x= dem_var

Using numpy.exp to calculate object life length

I can't find any example anywhere on the internet .
I would like to learn using the exponential law to calculate a probability.
This my exponential lambda : 0.0035
What is the probability that my object becomes defectuous before 100 hours of work ? P(X < 100)
How could I write this with numpy or sci kit ? Thanks !
Edit : this is the math :
P(X < 100) = 1 - e ** -0.0035 * 100 = 0.3 = 30%
Edit 2 :
Hey guys, I maybe have found something there, hi hi :
http://web.stanford.edu/class/archive/cs/cs109/cs109.1192/handouts/pythonForProbability.html
Edit 3 :
This is my attempt with scipy :
from scipy import stats
B = stats.expon(0.0035) # Declare B to be a normal random variable
print(B.pdf(1)) # f(1), the probability density at 1
print(B.cdf(100)) # F(2) which is also P(B < 100)
print(B.rvs()) # Get a random sample from B
but B.cdf is wrong : it prints 1, while it should print 0.30, please help !
B.pdf prints 0.369 : What is this ?
Edit 4 : I've done it with the python math lib like this :
lambdaCalcul = - 0.0035 * 100
MyExponentialProbability = 1 - math.exp(lambdaCalcul)
print("My probability is",MyExponentialProbability * 100 , "%");
Any other solution with numpy os scipy is appreciated, thank you
The expon(..) function takes as parameters loc and scale (which correspond to the mean and the standard deviation. Since the standard deviation is the inverse of the variance, we thus can construct such distribution with:
B = stats.expon(scale=1/0.0035)
Then the cummulative distribution function says for P(X < 100):
>>> print(B.cdf(100))
0.2953119102812866

GAMS - Economic Dispatch - QCP and NLP

I have this code:
If I use NLP i get the results, but using QCP as it was asked to me, I can not get results
anyone can help me finding the reason?
code:
sets g generators / P1*P5 /
properties generator properties / a,b,c,max,min /
cc(properties) cost categories / a,b,c /
table data(g,properties) generator cost characteristics and limits
a b c max min
P1 0.19 58.3 1800 155 35
P2 0.13 39.3 3250 195 60
P3 0.08 11.5 4600 165 95
P4 0.07 42.6 5100 305 170
P5 0.14 8.9 3850 280 130
parameter exp(cc) exponent for cost function / a 2, b 1, c 0 /;
scalar demand total power demand in MW / 730 / ;
variables
p(g) power generation level in MW
cost total generation cost - the objective function ;
positive variables p;
p.up(g) = data(g,"max") ;
p.lo(g) = data(g,"min") ;
equations
Q_Eq1 total cost calculation
Q_Eq2 constraint - total generation must equal demand ;
Q_Eq1 .. cost =e= sum((g,cc), data(g,cc)*power(p(g),exp(cc)));
Q_Eq2 .. sum(g,p(g)) =g= demand ;
model problem /all/ ;
solve problem using QCP minimizing cost ;
Seems as if the function "power" is treated as nonlinear in general without analyzing the value of "exp", so that it is not allowed for a QCP. You could reformulate Q_Eq1 like this to make it work:
Q_Eq1 .. cost =e= sum((g,cc), data(g,cc)*(1 $(exp(cc)=0) +
p(g) $(exp(cc)=1) +
sqr(p(g))$(exp(cc)=2)));
Best,
Lutz