I am trying to get to grips with using Nlopt for optimisation problems in Python. I set myself a basic example as a ways of getting to grips with how to navigate the lib. I have set up 3 simple simultaneous equations shown below, and have cast them into matrix form. I am now struggling to figure out why Nlopt isn't happy with what I'm giving it.
#testing nlopt
import nlopt
import numpy as np
from numpy import array as array
# create 3 simple simultaenous equations
#4x - 3y + z = -10 (1)
#2x + y + 3z = 0 (2)
#-x + 2y -5z = 17 (3)
# sol: x = 1, y = 4, z = -2
constraint_matrix = []
constraint_rhs = []
# eq 1
constraint_matrix.append([4,-3,1])
constraint_rhs.append(-10)
# eq 2
constraint_matrix.append([2,1,3])
constraint_rhs.append(0)
# eq 3
constraint_matrix.append([-1,2,-5])
constraint_rhs.append(17)
A = array(constraint_matrix)
b = np.reshape(array(constraint_rhs), (-1,))
#Ax = b represents the above equations
print(A)
print(b)
ncontrols = A.shape[1]
opt = nlopt.opt(nlopt.LD_SLSQP,ncontrols)
#defining a general function for the above
def my_fn(x):
print('test')
print(A)
print(b)
val = (A*x) - b
return val
opt.set_min_objective(my_fn)
x0 = array([6,2,-5])
xopt = opt.optimize(x0)
opt_val = opt.last_optimum_value()
result = opt.last_optimize_result()
print('end')
With the above I get the following error:
File "C:\Users\sm\Anaconda3\lib\site-packages\nlopt.py", line 335, in optimize
return _nlopt.opt_optimize(self, *args)
TypeError: my_fn() takes 1 positional argument but 2 were given
I've also tried to figure out how I actually pass A and b to my_fn to no avail. I tried simply adding A and b as arguments to the function, and doing opt.optimize([x0,A,b]) which has not worked. Can Nlopt not handle matrices, do I need to break this down into 3 individual equality constraints somehow?
Any help would be appreciated
Cheers
try
Some NLopt algorithms need an analytical gradient. LD_SLSQP needs a gradient. Your function returns an array. It should return a scalar. I never tried to solve equation systems with NLopt. It works for me with the objective function above and:
Algorithm LN_LN_NEWUOA needs fewer iterations. But to solve this equation system you can use:
Here is another example of minimizing an objective function with 2 parameters x[0] and x[1] and a constraint x[0] + x[1] <= 10:
Output should be: