Ask Your Question
0

Problem with various type in linear programming

asked 2022-09-02 16:28:37 +0100

Cyrille gravatar image

The following program is well defined

f(x) = 4*x-0.9*x^2
lr=6
pas=1# 0.5,0.25
lsp=lr/pas
X=[n(pas*i,14) for i in range(0,Integer(lr/pas))]
pente=[diff(f(x),x).subs(x=X[i]) for i in range(0,Integer(lsp))]
optim_1 = MixedIntegerLinearProgram(maximization=False, solver = "GLPK")
x = optim_1.new_variable(integer=False, indices=[0..len(X)+1]) # les nouvelles variables seront x[1]... x[7]}
for i in range(0, len(X)):
    optim_1.add_constraint(x[len(X)+1]<=pente[i]*(x[i]-X[i]))
optim_1.set_objective(x[len(X)+1])
optim_1.show()

but unfortunatelly for me the constraints are

optim_1.add_constraint(x[len(X)+1]<=pente[i]*(x[i]-X[i])+f(X[i])

But in that case, I raise the error unsupported operand parent(s) for +: 'Linear functions over Real Double Field' and 'Symbolic Ring'. I perfectly uderstand the problem but I do not know. I do not know in what type to transform f(X[i]), if this is the problem.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-02 16:41:43 +0100

tmonteil gravatar image

updated 2022-09-02 16:52:04 +0100

The problem is that f is a symbolic expression (like a formula, which is good to compute derivatives), not a function that can be called to transform floating-point numbers into floating-point numbers (it will instead return a symbolic expression).

You can transform a symbolic expression into a callable function defined on the floating-point numbers with fast_float:

optim_1.add_constraint(x[len(X)+1]<=pente[i]*(x[i]-X[i])+fast_float(f)(X[i]))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-09-02 16:28:37 +0100

Seen: 150 times

Last updated: Sep 02 '22