# Implementing NLP as QP on docplex

I wanna learn how to solve non-linear programs using the docplex library. according to this link I should be able to run the model as a QP. But when running the model I got the error: DOcplexException: Model<portfolio_miqp> has not been solved yet. So I'm wondering if it is because of some theory that I'm not aware of (I'm just starting to study optimization) or if it is because I'm making a syntax error. Below the problem I'm trying to solve:

• A retailer buys an item from a supplier for $$c=\\\500$$ and sells it to its customers for $$p=\\\700$$.
• The retailer experiences a constant daily demand of $$d=20$$ units per day for the item.
• If the retailers spend $$\\\x$$ dollars on advertisement daily, it will increase daily demand by $$\sqrt{x}/10$$ units.
• Lead time for procuring the item from the supplier is two days.
• There is a fixed cost of $$f=\\\2,000$$ for placing an order. This cost does not depend on the number of units ordered.
• There is a variable cost of $$h=\\\5$$ per day associated with holding an item in the retailer’s inventory.
• Customers' demands must be met without backorders.
• The retailer wishes to maximize profit.
Assumptions
• Lead time can be assumed to be zero.
• Retailer orders only when inventory drops to zero.
• The optimal order quantity does not change with time.
Variables
• Optimum quantity of items to order $$Q$$.
• Period between order (days) $$T$$.
• Advertisement investment $$x$$.
Constraints
• Demand must be equal to the order $$(d+\alpha{(x)})T=Q$$.
Objective
• Maximize profits: $$(p-c)(d+\alpha{(x)})-\frac{hQ}{2}-\frac{f}{T}-x$$ Then the algebraic model can be written as follows:
The Algebraic Model

\begin{aligned} \text{Maximize:} \quad & \; (p-c)(d+\alpha{(x)})-\frac{hQ}{2}-\frac{f}{T}-x \\ \text{Subject to:} \quad & (d+\alpha{(x)})T=Q\\ \quad & Q >0\\ \quad & x\geq 0 \end{aligned} Note that the objective function was derivate from the problem. I didn't include how the objective function was developed. Because I'm extracting the problem from an example solved in class which was solved using the GRG Nonlinear method that comes with the solver extension in excel.

The code I'm running is:

from docplex.mp.model import Model
import math

mdl = Model(name='portfolio_miqp')

Q = mdl.continuous_var(name='Quantity of Items')
T = mdl.continuous_var(name='Period in days')

alpha = 0.1
c = 500
p = 700
d = 20
f = 2000
h = 5

profit =  (p-c)*(d + alpha*math.sqrt(x)) - (h*Q)/2 - f/T-x

mdl.maximize(profit)
mdl.solve()

• Check your algebraic model, as well as your transcript as a DOcplex model as well. They both contain many errors. – LocalSolver Apr 22 at 21:23
• Thanks for the corrections. Now, it is clearer and that works. Here is the log obtained using LocalSolver 10.0: 8979 iterations performed in 0 seconds Optimal solution: obj = 3452.52 gap = < 0.01% bounds = 3452.84 Run output... Q = 129.376369564468 T = 6.18354476687214 x = 85.1350796591106 – LocalSolver Apr 23 at 7:49
• Thanks for introducing me to local_solver I was not familiar with it. overview – AGH Apr 23 at 15:04

Within docplex I would use cpoptimizer and write

mdl = CpoModel(name='portfolio_miqp')

scale=10000

scaleQ = mdl.integer_var(0,200*scale,name='scaleQuantity of Items')
scaleT = mdl.integer_var(0,100*scale,name='scalePeriod in days')

Q=scaleQ/scale
T=scaleT/scale
x=scalex/scale

alpha = 0.1
c = 500
p = 700
d = 20
f = 2000
h = 5

profit =  (p-c)*(d + alpha*mdl.power(x,0.5)) - (h*Q)/2 - f/T-x

mdl.maximize(profit)

msol=mdl.solve()

print("Q=",msol[scaleQ]/scale)
print("T=",msol[scaleT]/scale)
print("x=",msol[scalex]/scale)
print("profit :",msol.get_objective_values()[0])


which gives

Q= 127.1403
T= 6.076
x= 85.5625
profit : (3452.42, 3452.42)

• Yes, that's the answer I'm getting from the solver in excel. I see that this is an implementation of Constraint Programming (CP) from the same python library link. I'm wondering if there is a way to solved as a QP? or If this is not possible, why? From the example we know this is a convex so QP should work. – AGH Apr 23 at 15:00

The error message suggests that you tried to access the solution before solving the model. At the point that mdl.maximize(profit) is executed, you have constructed your model, but you have not solved it. Trying invoking mdl.solve() next.