# Problem with various type in linear programming

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.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 close merge delete

Sort by ยป oldest newest most voted

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]))

more