IBMDecisionOptimization/docplex-examples

Code Python

Julia000199 opened this issue · 8 comments

Hi @vlkong ,
Currently I am working on a docplex solved code, a MILP problem like the code below.
I want to plot a histogram showing the value from the objective function and the constraint r on the same graph, but I haven't been able to do it yet.
Can anyone help me to solve the above problem, I really appreciate it!

from docplex.mp.model import Model
mdl = Model('Workforce')
n = 4
bccn = [i for i in range(1, n+1)]

m = 29
tram = [j for j in range(1, m+1)]

arcos = [(i,j) for i in bccn for j in tram]

import data

import pandas as pd
excel = pd.ExcelFile('data_lvan.xlsx')

#temperature
Nhiet_do = pd.read_excel(excel,'Nhiet_do')
Nhiet_do1 = {k: row['Nhiet do'] for k, row in Nhiet_do.iterrows()}
se = {tram[k]:Nhiet_do1[k] for k in Nhiet_do1}

Cost

Chi_phi_vuot = pd.read_excel(excel,'Chi_phi')
Chi_phi_vuot1 = {k: row['Chi phi vuot'] for k, row in Chi_phi_vuot.iterrows()}
wc1 = {bccn[k]:Chi_phi_vuot1[k] for k in Chi_phi_vuot1}

Recommended temperature

WT = pd.read_excel(excel,'WT')
WT1 = {k: row['Nhiet do khuyen nghi'] for k, row in WT.iterrows()}
wt = {arcos[k]:WT1[k] for k in WT1}

Variable

X = mdl.binary_var_dict(arcos,name='X')
O = mdl.continuous_var_dict(tram,name='O')

Objective

z1 = mdl.sum(mdl.sum(X[i,j] * wc1[i] for i,j in arcos))
mdl.minimize (z1)

#Constraint

def r(wt,x):
mdl.add_constraints(mdl.sum((wt[i,j] * X[i,j]) for i,j in arcos) == 130)
return r

Hello,

Can you please provide a fully working sample code in a github repo, that demonstrate what is not working for you ?

Thanks

Hi @vlkong ,
I send all the code related to the mathematical model I am studying. Regarding the data of the article, because it is quite a lot, I cannot add it here. Like I said above, for this model I want to build matplotlib between objective function and 1 constraint, can you help me with this problem?
Thank you very much!

from docplex.mp.model import Model
import docplex.mp.solution as solucion

import matplotlib.pyplot for visualization

import matplotlib.pyplot as plt

mdl = Model('Workforce')

#Cong nhan
n = 4
bccn = [i for i in range(1, n+1)]

Tram

m = 29
tram = [j for j in range(1, m+1)]
#print(bccn)

arcos = [(i,j) for i in bccn for j in tram]

import data

import pandas as pd
excel = pd.ExcelFile('data_lvan.xlsx')

#Nhiet do tai tram
Nhiet_do = pd.read_excel(excel,'Nhiet_do')
Nhiet_do1 = {k: row['Nhiet do'] for k, row in Nhiet_do.iterrows()}
se = {tram[k]:Nhiet_do1[k] for k in Nhiet_do1}

San_luong = pd.read_excel(excel,'San_luong')
San_luong1 = {k: row['San luong yeu cau'] for k, row in San_luong.iterrows()}
SD = {tram[k]:San_luong1[k] for k in San_luong1}

#So luong cong nhan cac bac

Chi_phi = pd.read_excel(excel,'Chi_phi')
Chi_phi1 = {k: row['So luong'] for k, row in Chi_phi.iterrows()}
socn = {bccn[k]:Chi_phi1[k] for k in Chi_phi1}

#San luong toi thieu cong nhan n thuc hien tai moi tram i

WMIN = pd.read_excel(excel,'WMIN')
WMIN1 = {k: row['San luong toi thieu'] for k, row in WMIN .iterrows()}
wmin = {arcos[k]:WMIN1[k] for k in WMIN1}

#San luong toi da cong nhan n thuc hien tai moi tram i
WMAX = pd.read_excel(excel,'WMAX')
WMAX1 = {k: row['San luong toi da'] for k, row in WMAX .iterrows()}
wmax = {arcos[k]: WMAX1[k] for k in WMAX1}
#print (wmax)

#Nhiet do toi thieu cho moi cong nhan n tai moi tram i
WTMIN = pd.read_excel(excel,'WTMIN')
WTMIN1 = {k: row['Nhiet do toi thieu'] for k, row in WTMIN .iterrows()}
wtmin = {arcos[k]: WTMIN1[k] for k in WTMIN1}

Nhiet do toi da cho moi cong nhan n tai moi tram i

WTMAX = pd.read_excel(excel,'WTMAX')
WTMAX1 = {k: row['Nhiet do toi da'] for k, row in WTMAX .iterrows()}
wtmax = {arcos[k]:WTMAX1[k] for k in WTMAX1}

Nhiet do khuyen nghi cho moi cong nhan n tai moi tram i

WT = pd.read_excel(excel,'WT')
WT1 = {k: row['Nhiet do khuyen nghi'] for k, row in WT.iterrows()}
wt = {arcos[k]:WT1[k] for k in WT1}

#Do on toi thieu cho moi cong nhan n tai moi tram i
TOMIN = pd.read_excel(excel,'TOMIN')
TOMIN1 = {k: row['Do on toi thieu'] for k, row in TOMIN.iterrows()}
tomin = {arcos[k]:TOMIN1[k] for k in TOMIN1}

#Do on toi da cho moi cong nhan n tai moi tram i
TOMAX = pd.read_excel(excel,'TOMAX')
TOMAX1 = {k: row['Do on toi da'] for k, row in TOMAX.iterrows()}
tomax = {arcos[k]:TOMAX1[k] for k in TOMAX1}

Tieng on khuyen nghi cho moi cong nhan n tai moi tram i

TO = pd.read_excel(excel,'TO')
TO1 = {k: row['Tieng on khuyen nghi'] for k, row in TO.iterrows()}
to = {arcos[k]:TO1[k] for k in TO1}

#print (to)

#Chi phi tra luong cho cong nhan n tai moi tram
WC = pd.read_excel(excel,'WMAX')
WC1 = {k: row['Chi phi luong'] for k, row in WMAX.iterrows()}
wc = {arcos[k]:WC1[k] for k in WC1}

#Chi phi doi voi moi san pham vuot cua tung loai cong nhan
Chi_phi_vuot = pd.read_excel(excel,'Chi_phi')
Chi_phi_vuot1 = {k: row['Chi phi vuot'] for k, row in Chi_phi.iterrows()}
wc1 = {bccn[k]:Chi_phi_vuot1[k] for k in Chi_phi_vuot1}

VARIABLES

#Cong nhan i lam viec tai moi tram i

X = mdl.binary_var_dict(arcos,name='X')
SL = mdl.continuous_var_dict(arcos,name='SL')
ND = mdl.continuous_var_dict(arcos,name='ND')
O = mdl.continuous_var_dict(tram,name='O')

DO = mdl.continuous_var_dict(arcos,name='DO')

#CO = mdl.continuous_var_dict(name='CO')

x = mdl.continuous_var_dict(1,name='x')

T = mdl.continuous_var_dict(1,name='T')

#OBJECTIVE

z1 = mdl.sum(X[i,j] * wc[i,j] for i,j in arcos) + (mdl.sum(X[i,j]* O[j] * wc1[i] for i,j in arcos))
mdl.minimize (z1)

#z2= mdl.sum(ND[i,j]- wt[i,j]*X[i,j] for i,j in arcos )
#mdl.minimize (z2)

CONSTRAINTS

#RB (C1) dam bao co it nhat 1 cong nhan tai moi tram
mdl.add_constraints(mdl.sum(X[i,j] for i in bccn) >= 1 for j in tram)

#CONSTRAINTS (C2) quy dinh so luong cong nhan thuc hien khong duoc phep vuot qua so luong cong nhan san co
mdl.add_constraints(mdl.sum(X[i,j] for j in tram) >= socn[i] for i in bccn)

#CONSTRAINTS (C3) quy dinh gioi han ve san luong cua cong nhan thuc hien
mdl.add_indicator_constraints(mdl.indicator_constraint(X[i,j], SL[i,j] <= wmax[i,j]) for i,j in arcos)

mdl.add_indicator_constraints(mdl.indicator_constraint(X[i,j], SL[i,j] >= wmin[i,j]) for i,j in arcos)

#CONSTRAINTS (C4) quy dinh gioi han ve nhiet do cua cong nhan thuc hien

mdl.add_indicator_constraints(mdl.indicator_constraint(X[i,j], ND[i,j] <= wtmax[i,j]) for i,j in arcos)

mdl.add_indicator_constraints(mdl.indicator_constraint(X[i,j], ND[i,j] >= wtmin[i,j]) for i,j in arcos)

#CONSTRAINTS(C5) dam bao so cong nhan duoc phan bo dam bao duoc san luong yeu cau tai moi tram
mdl.add_constraints(mdl.sum(SL[i,j] for i in bccn) >= SD[j] for j in tram)

#CONSTRAINTS (C6) cho biet san luong ma kha nang cong nhan lam vuot so voi yeu cau tai tram
mdl.add_constraints(mdl.sum(SL[i,j] - SD[j] for i in bccn) >= O[j] for j in tram)

CONSTRAINTS (C7) quy dinh nhiet do chiu dung cua cong nhan thuc hien tai tram

mdl.add_constraints(ND[i,j] >= X[i,j] * se[j] for i,j in arcos)

CONSTRAINTS (C8) quy dinh cong nhan lam viec tai tram thi moi co san luong

def r(ND,x):
mdl.add_constraints(mdl.sum((ND[i,j] - wt[i,j] * X[i,j]) for i,j in arcos) == 130)
return r

print("")
print("model results")
solucion.display()

Hello,

This sample notebook shows how to use Docplex's progress listeners to plot MIP gap progress during search:\https://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/jupyter/progress.ipynb

With minor changes, this code can be used to plot the evolution of objective value, gap, best bound or any expression built from decision variables during MIP search.

However, I cannot understand what you mean by "value of a constraint". For example, what is the value of x==1 ?

Regards.

Philippe.

Hi!
This problem is built with multi-objective form, however I have converted one objective into constraint to solve and want to plot the resulting matplotlib of its values. Thank you for sharing, I will look into this issue more.
Best regards

Hello,
I suggest you take a look at KPIs. KPIs are named expressions built from decision variables, which are not necessarily used in objective or constraints, but can be evaluated in a solution.

See

  • Model.add_kpi() to add a KPI with a name
  • Model.kpi_by_name() t retrieve a KPI from its name
  • KPI.compute(s) to compute the value of a KPI in a given solution

From there, as described in th eprogress notebook, derive a subclass from the docplex.mp.progress.SolutionListener: for each incumbent, you'll get a SolveSolution object, from which you can compute values of KPIs and plot them.

For a multi-objective problem, ensure all your goals (sub objectives) are defined as named KPIs, and proceed as above.

Hello!
Currently, I do not have much knowledge in using KPI, can you introduce me to python code documents related to using KPI to solve the same problem as above?
Thank you very much.
Best regards!

Dear @Julia000199 ,
please explore the example notebooks: https://github.com/IBMDecisionOptimization/docplex-examples/tree/master/examples/mp/jupyter

Many of them includes KPIs, like ucp_pandas or nurses_scheduling.
The refman for the python api is here: https://ibmdecisionoptimization.github.io/docplex-doc/mp/docplex.mp.kpi.html
You also want to have a look at this notebook, which is learning material for python/cplex: https://ibmdecisionoptimization.github.io/tutorials/html/Beyond_Linear_Programming.html

Best regards

Hi!
Many thanks for your support
Best regards