1

I recently started using CPLEX integrated in python for my master project and I have a hard time with one of my variables. I am modelling the charge and discharge of a battery in function of the wind and solar power as well as electricity market prices. All my variables for charge, discharge and production are well defined but my battery state of charge ends up being null at all times after solving. When calling get values of this variable i get a list of zeros (with sol the solution of the optimization and Ebes the name of the State of charge):

sol.get_values(Ebes[t]for t in time)

It is even unfeasible that this variable would be null as i also have the constraints in the model :

for t in time:    
    mdl.add_constraint(Ebes[t]>=Ebmin)
    mdl.add_constraint(Ebes[t]<=Ebmax)

When I display the model before solving with print(mdl.export_to_string()) it shows that Ebes is constrained to be higher than Ebmin(=20) for all the time steps. The only hint I get is that the name of the variables in there is slightly different from the others. In here the variables of Ebes are named _Ebes_date whereas the other variables are named for example Pdischarge_date and not _Pdischarge_date. I guess this "_" before the name shows that there is a problem but i can't manage to find what to change.

My variables are defined as:

Ebes=mdl.continuous_var_dict(time,name='Ebes')

for i in range(len(time)):
if i==0:
    mdl.add_constraint(Ebes[time[0]]==Ebes[time[len(time)-1]]*(1-etaleak)+Pcha[time[0]]*etacha*dt-(Pdis[time[0]]/etadis*dt))
else:
    t=time[i]
    tm=time[i-1]
    mdl.add_constraint(Ebes[t]==Ebes[tm]*(1-etaleak)+Pcha[t]*etacha*dt-(Pdis[t]/etadis*dt))

Thank you if you take time to answer me :)

The whole example:

import pandas as pd
from docplex.mp.model import Model
ind=['01_09_2016 00','01_09_2016 01','01_09_2016 02','01_09_2016 03','01_09_2016 04']#[1,2,3,4,5]
M=pd.Series(data=[10,30,30,15,30],index=ind)
P=pd.DataFrame(data={'Time':ind,'DK2_wind':[0.3,0.24,0.14,0.18,0.22],'DK2_solar':[0,0,0,0,0.01]}).set_index('Time',drop=True)

mdl=Model('dispatch')
time=P.index
Psolar=300             #MW
Pwind= 400             #MW
P.DK2_solar=P.DK2_solar*Psolar
P.DK2_wind=P.DK2_wind*Pwind
Pres=P.sum(axis=1)     #MW
Pmax=800               #MW

#Battery parameters:
Pbmax= 50             #MW
Ebmax= 100             #MWh
Ebmin= 20              #MWh

Pbal =mdl.continuous_var_dict(time,name='Pbal')
Pcha =mdl.continuous_var_dict(time,name='Pcharge')
Pdis =mdl.continuous_var_dict(time,name='Pdischarge')
Ebes=mdl.continuous_var_dict(time,name='Ebes')
switch=mdl.binary_var_dict(time,name='switch')
for t in time:
    mdl.add_constraint(Pbal[t]==Pres[t]+Pdis[t]-Pcha[t])
for t in time:
    mdl.add_constraint(Pdis[t]<=Pbmax*(1-switch[t]))
for t in time:
    mdl.add_constraint(Pdis[t]>=0)
for t in time:
    mdl.add_constraint(Pcha[t]<=Pbmax*switch[t])
for t in time:
    mdl.add_constraint(Pcha[t]>=0)
for t in time:    
    mdl.add_constraint(Ebes[t]>=Ebmin)
for t in time:
    mdl.add_constraint(Ebes[t]<=Ebmax)   
for i in range(len(time)):
    if i==0:
        mdl.add_constraint(Ebes[time[0]]==Ebes[time[len(time)-1]]+Pcha[time[0]]-Pdis[time[0]])
    else:
        t=time[i]
        tm=time[i-1]
        mdl.add_constraint(Ebes[t]==Ebes[tm]+Pcha[t]-Pdis[t])        
mdl.maximize(mdl.sum((Pbal[t]*M[t]) for t in time))

sol=mdl.solve(url=URLmt,key=Mykey,log_output=True)
sol_Ebess=sol.get_values(Ebes[t]for t in time)
sol_Ebess
sol.solve_details.status

So here the sol_Ebess is null for all indices. if I change ind to be numbers instead it works and Ebess is equal to the real value.

anat
  • 11
  • 3
  • What is the solution status? What do you get if you print `sol.solve_details.status` (see the documentation [here](https://cdn.rawgit.com/IBMDecisionOptimization/docplex-doc/master/docs/mp/docplex.mp.sdetails.html#docplex.mp.sdetails.SolveDetails.status)). – rkersh Feb 27 '19 at 17:27
  • I get : 'integer optimal solution' So it seems there is no problems coming from there, it considers that everything worked fine. – anat Feb 28 '19 at 12:41
  • Overall it still seems that the constraint is considered and that the true value of the Ebes is not null. I calculated the value of Ebes using the equation `Ebes[t]==Ebes[tm]*(1-etaleak)+Pcha[t]*etacha*dt-(Pdis[t]/etadis*dt)` and the values i get for Pcha and Pdis from the solving of the problem. The resulting Ebes is in the bounds of the constraints and seems coherent. But why then when I do `sol.get_values(Ebes[t]for t in time)` I don't get anything else than zeros ? – anat Feb 28 '19 at 14:16
  • Can you please edit your question to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Without that, it's hard to know what is going wrong. – rkersh Feb 28 '19 at 18:30
  • Actually by trying to get a Minimal complete and Verifiable example I found out that the error was coming from the index. I still don't understand why but whenever I use the time index in the form of 'dd_mm_yyyy hh' the variable Ebes is null and when I use another index like 1,2,3,4,5 it works. – anat Mar 01 '19 at 14:10
  • If I run your example with docplex 2.8.125 and a local solve, it seems to run without problems (i.e., `sol_Ebess` contains non-zero values). Do you get different behavior when solving locally vs on the cloud? – rkersh Mar 04 '19 at 22:25
  • I have a hard time linking the local cplex and my python. I actually have the API installed and so on but I can't figure out how to use it with python, that's why I use the cloud ... – anat Mar 05 '19 at 15:33
  • If the CPLEX Python API is installed, solving locally should be as easy as replacing `sol=mdl.solve(url=URLmt,key=Mykey,log_output=True)` with `sol=mdl.solve(log_output=True)`. Or, do you mean that you are having trouble installing the CPLEX Python API in the first place? If so see the instructions [here](https://www.ibm.com/support/knowledgecenter/SSSA5P_12.8.0/ilog.odms.cplex.help/CPLEX/GettingStarted/topics/set_up/Python_setup.html). – rkersh Mar 05 '19 at 20:04
  • yep when i don't use the key it says that cplex is missing. I already seen the instructions but it apparently don't work. Anyways thanks for your time ! – anat Mar 06 '19 at 15:15
  • Okay i tried again to install the API. I uninstalled and reinstalled the AI version of the CPLEX Optimization studio. In my code i added to the PythonPAth: `import sys` `sys.path.append('C:\Program Files\IBM\ILOG\CPLEX_Studio129\cplex\python\3.6\x64_win64')` but still I get three errors when running with the argument for the docloud: `CPLEX Error 1016: Promotional version. Problem size limits exceeded. CPLEX Error 1217: No solution exists. CPLEX Error 1217: No solution exists.` – anat Mar 13 '19 at 17:32
  • My guess is that you have more than one version of Python installed on your machine and you have installed the AI version in the wrong one. The error indicates that the free Community Edition is still installed for the version of Python you are running your program with. You need to run `setup.py` with the same version of Python that you intend to use when running your program. – rkersh Mar 13 '19 at 18:28
  • You could also try setting the `PYTHONPATH` environment variable to "C:\Program Files\IBM\ILOG\CPLEX_Studio129\cplex\python\3.6\x64_win64". – rkersh Mar 13 '19 at 18:29

0 Answers0