Anas
Anas

Reputation: 29

Why doesn't my CPLEX API solve the problem?

I have written my quadratic program in the CPLEX API but when I try to solve it the model doesn't do anything. The following is the code:

import numpy as np
from docplex.mp.model import Model
    
   
# Model Design
COVID_19_QP_CPLEX_MODEL = Model() # Name of the model ('...Name... ') Optional

# Input Data

Days = 80


τ_min = 11 
τ_max = 27

ζ_min = 10
ζ_max = 40
ζ_mode = 28

Ĉ = [7,16,23,33,42,49,55,59,63,68,72,77,81,87,90,95,100,106,112,115,119,123,127,129,132,136,140,143,145,149,152,156,160,163,166,170,174,179,183,187,192,197,201,206,210,215,218,222,227,231,234,238,243,247,252,257,260,265,271,277,282,287,291,296,301,305,311,316,322,328,333,340,344,350,355,362,368,373,379,386]


# Logistic Function Trial Values
K = 1000000
Q = 2000
λ = 0.5
v = 0.5


# Quadratic Program (NLP)

def I(t, K, Q, λ, v):   # I(t) Equations Number (1 & 2)
    return (K / (1 + Q * np.exp(-λ * t)) ** (1 / v)) 


def N(t, K, Q, λ, v):   # N(t) Equations Number (3 & 4)
    if t == 0:
        return I(0, K, Q, λ, v)
    else:
        return I(t, K, Q, λ, v) - I(t - 1, K, Q, λ, v)



# Decision Variables

# Epsilon (Error) ε
ε = COVID_19_QP_CPLEX_MODEL.continuous_var_list(Days, name = 'ε') 

# aj is the expected rate of individuals that die j days afer being infected (j = τmin, ... , τmax). Including Constraint Number 21 Bounding
a = COVID_19_QP_CPLEX_MODEL.continuous_var_list(τ_max, name = 'a' , lb = 0) # No need for ub 

#  bj represents the rate of individuals that recover j days afer being infected (j = rmin, ... ,rmax). Including Constraint Number 22 Bounding
b = COVID_19_QP_CPLEX_MODEL.continuous_var_list(ζ_max, name = 'b' , lb = 0) # No need for ub



# Constraint Number 17
COVID_19_QP_CPLEX_MODEL.add_constraints((sum(sum(a[j] * N(k - j, K, Q, λ, v) for j in range(τ_min, τ_max)) for k in range(Days)) - ε[t] == Ĉ[t]) for t in range(Days))


# Constraint Number 18
COVID_19_QP_CPLEX_MODEL.add_constraint(sum(a[j] for j in range(τ_min, τ_max)) + sum(b[j] for j in range(ζ_min, ζ_max)) == 1)


# Constraint Number 19
COVID_19_QP_CPLEX_MODEL.add_constraints(b[j - 1] <= b[j] for j in range(ζ_min + 1, ζ_mode))


# Constraint Number 20
COVID_19_QP_CPLEX_MODEL.add_constraints(b[j] >= b[j + 1] for j in range(ζ_mode, ζ_max - 1))


# Objective Function
Objective_Function = sum(ε[t]*ε[t] for t in range(Days)) 


# Setting Objective Function (Minimize)
COVID_19_QP_CPLEX_MODEL.set_objective('min', Objective_Function)


# Print Information
COVID_19_QP_CPLEX_MODEL.print_information()



COVID_19_QP_CPLEX_MODEL.solve()


# Print Solution
#print('Optimization is done. Objective Function Value: %.2f' % COVID_19_QP_CPLEX_MODEL.objective_value)

COVID_19_QP_CPLEX_MODEL.print_solution()

Also, if I add the following command -> print('Optimization is done. Objective Function Value: %.2f' % COVID_19_QP_CPLEX_MODEL.objective_value) the following error appears -> docplex.mp.utils.docplexexception: model<docplex_model1> did not solve successfully

Upvotes: 1

Views: 787

Answers (1)

Alex Fleischer
Alex Fleischer

Reputation: 10059

if you add

print(COVID_19_QP_CPLEX_MODEL.solve_details)

after the solve you will see

status = infeasible

So you do not get a solution because the model is not feasible

If you turn your objective function to non quadratic

Objective_Function = sum(ε[t] for t in range(Days)) 

then you could get some relaxation if you write

from docplex.mp.relaxer import Relaxer
rx = Relaxer()
rx.relax(COVID_19_QP_CPLEX_MODEL)

print ("number_of_relaxations= " + str(rx.number_of_relaxations))
rx.print_information()

Upvotes: 1

Related Questions