最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Docplex use for timeseries based variables - Stack Overflow

programmeradmin1浏览0评论

I am trying to model an optimisation problem where each item in a list is essentially power generated at that hour. I am trying to minimise the amount of energy stored while still getting the same energy over the course of the time series, and as such am modelling it as a linear programming problem.

I am trying to add a constraint, but am running into problems. The important bits look like this:

var_1 = model.continuous_var_list(1000, name = 'var_1')
var_2 = model.continuous_var_list(1000, name = 'var_2')
var_3 = model.continuous_var_list(1000, name = 'var_3')

model.add_constraint(var_1[i] - var+2[i] == var_3[i] for i in range(1000))

Where I am trying to say that at timestep i, var_3 should always equal the difference between var_1 and var_2. Is there a good example for this kind of problem I can learn from, or does anyone have any advice? Either in Docplex, or a similar thing in CPLEX I can use to understand. This code results in the error:

Expecting constraint, got: <generator object Home.linearlySolve.<locals>.<genexpr> at 0x0000013F2BBBA940> with type: <class 'generator'>

Thanks!

I am trying to model an optimisation problem where each item in a list is essentially power generated at that hour. I am trying to minimise the amount of energy stored while still getting the same energy over the course of the time series, and as such am modelling it as a linear programming problem.

I am trying to add a constraint, but am running into problems. The important bits look like this:

var_1 = model.continuous_var_list(1000, name = 'var_1')
var_2 = model.continuous_var_list(1000, name = 'var_2')
var_3 = model.continuous_var_list(1000, name = 'var_3')

model.add_constraint(var_1[i] - var+2[i] == var_3[i] for i in range(1000))

Where I am trying to say that at timestep i, var_3 should always equal the difference between var_1 and var_2. Is there a good example for this kind of problem I can learn from, or does anyone have any advice? Either in Docplex, or a similar thing in CPLEX I can use to understand. This code results in the error:

Expecting constraint, got: <generator object Home.linearlySolve.<locals>.<genexpr> at 0x0000013F2BBBA940> with type: <class 'generator'>

Thanks!

Share Improve this question edited Mar 30 at 16:04 Trev asked Mar 30 at 16:02 TrevTrev 175 bronze badges 2
  • maybe you should use normal for-loop with .add_constraint() inside this loop. – furas Commented Mar 30 at 16:20
  • That would work, but add_constraints (with an S) is much more efficient in adding a batch of constraint – Philippe Couronne Commented Mar 31 at 14:28
Add a comment  | 

1 Answer 1

Reset to default 0

You could use add_constraints with an "s"

from docplex.mp.model import Model

model = Model(name='model')

var_1 = model.continuous_var_list(1000, name = 'var_1')
var_2 = model.continuous_var_list(1000, name = 'var_2')
var_3 = model.continuous_var_list(1000, name = 'var_3')

model.add_constraints(var_1[i] - var_2[i] == var_3[i] for i in range(1000))

sol=model.solve(log_output=True)

for v in model.iter_continuous_vars():
    print(v," = ",v.solution_value)

works fine

发布评论

评论列表(0)

  1. 暂无评论