Error/Sub-optimal Values: Discrete optimization with stochastic project.

190 views
Skip to first unread message

Sidd

unread,
Dec 30, 2015, 4:42:26 AM12/30/15
to cvxpy

Hello Steve and everybody,

CVXPY seems like a really intuitive tool for python users, I have been going through the group over the past couple of days and learning a lot. Great job! 

However I have been frustratingly juggling with a control problem for a couple of days for my project. Formulation of the problem has been done but I think I am having problems framing it in cvxpy. This might be slightly long since I want to be explicit but please help me out.

A power system problem involving a time-series of power data/load (stochastic), wind power (stochastic) etc wherein: At every time-step (say 1 hour each, total of 24 hours). I have to find the optimal power that should be withdrawn from a battery given constraints over variables and certain battery bounds. For a base case I am assuming perfect data (no uncertainty) and directly reading it from a csv.  

I ideally expected to define a decision variable, say
P_grid = cvx.Variable(24)

Then code the discrete control optimization problem over all time-steps and simulate. Finally, accessing the optimal value for P_grid using P_grid.value thus  printing the 24 optimal values for each time-step. That did not seem to work and instead I got an array of values [0, 0, .........., 2134.6] i.e only the value for the final step optimization. I also tried the same with a (24,24) variable but still got a similar result (only final step values). 

ISSUE: So I redefined by variables and started storing these values at every step in a list/numpy array, but my results don't really make sense. Part of the new problem looks like this:

1) following are my decision variables 
r_t = cvx.Variable(1,T+1)
d_t
= cvx.Variable(1,T+1)
P_grid
= cvx.Variable(1,T+1)  
E_t
= cvx.Variable(1,T+1)
grid_list
= np.zeros(25)
avg
= np.eye(25)


2) Part of the code is as follows(P_wind, P_load are constants read from a csv):
for i in range(T):
    gridsave
= (grid_list[-1])
    grid_list
= grid_list.tolist()
    grid_list
.remove(grid_list[0])
    grid_list
.append(gridsave)
   
   
if t == 0:
        avg_grid
= 0
   
else:
        avg_grid
= (1/t)*sum(grid_list)
   
    obj
= cvx.Minimize(cvx.sum_squares(P_grid[:,t+1] - avg_grid))

    constraints
= [E_t[:,t+1] >= (1 - DOD) * E_cap,
                   E_t
[:,t+1] <= E_cap,
                   r_t
[:,t+1] <= P_cap,
                   r_t
[:,t+1] >= 0,
                   d_t
[:,t+1] <= P_cap,
                   d_t
[:,t+1] >= 0,
                   P_grid
[:,t+1] == P_load[t] + r_t[:,t+1] - d_t[:,t+1] - P_wind[t],
                   E_t
[:,t+1] == E_t[:,t] + r_t[:,t+1] - d_t[:,t+1],
                   E_t
[:,0] == 0,
                   d_t
[:,0] == 0,
                   r_t
[:,0] == 0,
                   P_grid
[:,0] == 0]
   
    prob
= cvx.Problem(obj, constraints)
    prob
.solve(solver=cvx.CVXOPT)
    grid_list
.append(P_grid[:,t+1].value)


3) What am I doing wrong? Is there a better way to model discrete optimization problems? Are there any examples I can refer to? 

Thanks! 




Sidd

unread,
Dec 30, 2015, 4:47:59 AM12/30/15
to cvxpy
Ignore the logic for grid_list, it's erroneous and I've fixed it. I need to understand how to model discrete optimization/stochastic control problems in cvxpy. I'm not too thorough with MATLAB unfortunately.    

Steven Diamond

unread,
Dec 30, 2015, 12:20:05 PM12/30/15
to cvxpy
It looks like you're creating a separate problem for each state, rather than a single problem encompassing all the states. Here's an example of modeling a control problem in cvxpy:


In regards to the stochastic element, you might be interested in CVXSTOC:


Message has been deleted

Sidd

unread,
Dec 30, 2015, 2:21:00 PM12/30/15
to cvxpy
1) Yes because I wish to optimize at every time-step in the problem. Isn't that possible? I however am getting a SolverError and my problem doesn't seem to converge. I tried changing the solvers too. For some reason I only have ECOS, CVXOPT solvers, other options aren't recognized. I also tried updating cvxpy but other solvers don't appear.

2) I initially tried running the control problem but was getting an error like this:

54 states.append(cvx.Problem(cvx.Minimize(obj), constraints)) ---> 55 prob = sum(states) 56 prob.constraints += [E_t[:,0] == 0, d_t[:,0] == 0, r_t[:,0] == 0, P_grid[:,0] == 0]
57 prob.solve(solver=cvx.CVXOPT, verbose=True) TypeError: unsupported operand type(s) for +: 'int' and 'Problem'


3) Thanks for the reference stochastic reference! But first I'll have to find a way around the predictable data problem. 


Steven Diamond

unread,
Dec 30, 2015, 2:47:53 PM12/30/15
to cvxpy
What do you mean by optimize at every time step? Right now you're optimizing over one entry in each variable at a time, which is completely different from optimizing over the entire variable. This is why all the entries except for the last were zero, because those entries weren't part of your optimization problem.

The error you're getting is kind of odd. It has nothing to do with the solvers. The error is happening before the solvers are ever called. What version of cvxpy are you using?

Sidd

unread,
Dec 30, 2015, 4:43:11 PM12/30/15
to cvxpy
My cvxpy version says:

Metadata-Version: 1.0
Version: 0.3.5


By optimizing at each step, I meant that each entry for each the constants (P_wind, P_Load) is only available for a step ahead in the horizon, something like real-time. So I wouldn't have all the entires for the corresponding variable for the day. Therefore I tried to model for each entry in P_wind = Variable(25) (each time-step) AND then store the resultant decision variable entries from the optimization, in that single variable like an array. But I figured that wouldn't work (naive me) so I switched to doing it differently as shown above.  

Now it doesn't converge with the solvers.

2) The second doubt in the previous comment I had is regarding the control example in the notebook link from the examples in cvxpy repository. 

56 prob = sum(states)
57 prob.constraints += [E_t[:,0] == 0, d_t[:,0] == 0, r_t[:,0] == 0, P_grid[:,0] == 0]
58 prob.solve(solver=cvx.CVXOPT, verbose=True)

TypeError: unsupported operand type(s) for +: 'int' and 'Problem'
 
According to the 'Problem Arithmetic' in the Advanced Section of the cvxpy.org website (and the notebook) summing should work however I am unable to add all problems objects and optimize. I tried printing the states list and it gave me the following output, which seems incorrect.

[Problem(Minimize(square(var6009[0:, 1]) + -303.060478524), [50.0 <= var6010[0:, 1], var6010[0:, 1] <= 500, var6007[0:, 1] <= 2000, 0 <= var6007[0:, 1], var6008[0:, 1] <= 2000, 0 <= var6008[0:, 1], var6009[0:, 1] == 189.1872146 + var6007[0:, 1] + -var6008[0:, 1] + -122.6652946, var6010[0:, 1] == var6010[0:, 0] + var6007[0:, 1] + -var6008[0:, 1]]), Problem(Minimize(square(var6009[0:, 1]) + -91845.653643), [50.0 <= var6010[0:, 1], var6010[0:, 1] <= 500, var6007[0:, 1] <= 2000, 0 <= var6007[0:, 1], var6008[0:, 1] <= 2000, 0 <= var6008[0:, 1], var6009[0:, 1] == 189.1872146 + var6007[0:, 1] + -var6008[0:, 1] + -122.6652946, var6010[0:, 1] == var6010[0:, 0] + var6007[0:, 1] + -var6008[0:, 1]]), ....................... #so on and so forth

Where am I messing up?

Steven Diamond

unread,
Dec 30, 2015, 5:50:09 PM12/30/15
to cvxpy


On Wednesday, December 30, 2015 at 4:43:11 PM UTC-5, Sidd wrote:
My cvxpy version says:

Metadata-Version: 1.0
Version: 0.3.5


By optimizing at each step, I meant that each entry for each the constants (P_wind, P_Load) is only available for a step ahead in the horizon, something like real-time. So I wouldn't have all the entires for the corresponding variable for the day. Therefore I tried to model for each entry in P_wind = Variable(25) (each time-step) AND then store the resultant decision variable entries from the optimization, in that single variable like an array. But I figured that wouldn't work (naive me) so I switched to doing it differently as shown above.  

Now it doesn't converge with the solvers.

I see, that makes perfect sense. I was confused by your code. The solver doesn't care about the values you stored in the variables, as you found out. It resets the variables each time you solve the problem.

Could you attach your entire code? It's very hard to answer these questions without seeing the exact errors that you're getting.
 

2) The second doubt in the previous comment I had is regarding the control example in the notebook link from the examples in cvxpy repository. 

56 prob = sum(states)
57 prob.constraints += [E_t[:,0] == 0, d_t[:,0] == 0, r_t[:,0] == 0, P_grid[:,0] == 0]
58 prob.solve(solver=cvx.CVXOPT, verbose=True)
TypeError: unsupported operand type(s) for +: 'int' and 'Problem'
 
According to the 'Problem Arithmetic' in the Advanced Section of the cvxpy.org website (and the notebook) summing should work however I am unable to add all problems objects and optimize. I tried printing the states list and it gave me the following output, which seems incorrect.

[Problem(Minimize(square(var6009[0:, 1]) + -303.060478524), [50.0 <= var6010[0:, 1], var6010[0:, 1] <= 500, var6007[0:, 1] <= 2000, 0 <= var6007[0:, 1], var6008[0:, 1] <= 2000, 0 <= var6008[0:, 1], var6009[0:, 1] == 189.1872146 + var6007[0:, 1] + -var6008[0:, 1] + -122.6652946, var6010[0:, 1] == var6010[0:, 0] + var6007[0:, 1] + -var6008[0:, 1]]), Problem(Minimize(square(var6009[0:, 1]) + -91845.653643), [50.0 <= var6010[0:, 1], var6010[0:, 1] <= 500, var6007[0:, 1] <= 2000, 0 <= var6007[0:, 1], var6008[0:, 1] <= 2000, 0 <= var6008[0:, 1], var6009[0:, 1] == 189.1872146 + var6007[0:, 1] + -var6008[0:, 1] + -122.6652946, var6010[0:, 1] == var6010[0:, 0] + var6007[0:, 1] + -var6008[0:, 1]]), ....................... #so on and so forth

Where am I messing up?

I don't see anything wrong with the code snippets you posted. I can find the answer if you attach your entire code. Otherwise I would suggest starting with 1 or 2 time steps and a simpler problem and seeing if that works.
 

Sidd

unread,
Dec 31, 2015, 4:21:44 AM12/31/15
to cvxpy
Alright, but its dependent on a lot of other code, I will strip off the unrequired bits and send it to you, thanks!

But the control example in the repository doesn't work on my PC either, it throws the exact same error. For reference:

     11     states.append( Problem(Minimize(cost), constr) )
     12 # sums problem objectives and concatenates constraints.
---> 13 prob = sum(states)
     14 prob.constraints += [x[:,T] == 0, x[:,0] == x_0]
     15 prob.solve()


TypeError: unsupported operand type(s) for +: 'int' and 'Problem'

Stupid question but you tried to compile this with the latest updates on CVXPY? Otherwise I think there might be a problem with the cvxpy on my PC since I don't seem to have enough solvers either as stated earlier and the solvers_installed() function isn't recognized either. But all my packages are updated (according to pip/conda). 

Steven Diamond

unread,
Dec 31, 2015, 6:32:35 PM12/31/15
to cvxpy
Yes, I was using the latest cvxpy. I suspect the version of Python you're actually running is not the same as the one you got the cvxpy version from. When you're running python execute "import cvxpy; print cvxpy.__version__". This will give the actual cvxpy version being used.

Ultimately I would recommend uninstalling all versions of python except the one you want to use.

Sidd

unread,
Jan 3, 2016, 7:42:13 AM1/3/16
to cvxpy
I solved this issue by creating a stripped down virtual environment running python-2 and installing all dependencies using the pip command.

I don't seem to find proper installation instructions on the cvxstoc page. The installation section just states something confusing like:
1. TODO
2. ...


ehtisham...@gmail.com

unread,
Aug 20, 2020, 8:52:33 PM8/20/20
to cvxpy
HI Sidd,

As my problem is similar to yours, I am working on day ahead energy scheduling problem, I am facing problem to stop charging and discharging concurrently as charging and discharging efficiency are different so I am using a binary variable that gives solver error I am writing the constraint part below, I have to solve my problem on open source solver. Any help will be highly appreciated.

constraints += [P_dg[i] + P_s[i] - P_dis[i] == P_L[i] + P_ch[i], 0 <= P_dg[i],
                    P_dg[i] <= 750, 0 <= P_ch[i], P_ch[i] <= P_bmax*b_ess[i], 
                    (-P_bmax)*(1 - b_ess[i]) <= P_dis[i], P_dis[i] <= 0, SOC[0] == 30, 
                    SOC[i+1] == SOC[i] + (P_ch[i]*(Eff/Cap)*t*100) + (P_dis[i]*(1/(Eff*Cap))*t*100),
                    15 <= SOC[i+1], SOC[i+1] <= 70]

b_ess[ i ] is binary variable above.

Reply all
Reply to author
Forward
0 new messages