'Implement constrained optimization problem in Mystic
Is it possible to implement the following problem in mystic?
The problem minimizes a sum of squares between two (10000 by 40) matrices: Σ(X-A)^2 where X is a concatenation of four matrices (10000 by 10) and each segment is weighted (W) individually. There is also a constraint where the sum of the weights must equal 1 i.e. (W1 + W2 + W3 + W4 = 1). I'm currently using the SLSQP method in scipy optimize to get the optimal weight values but I want to determine if mystic can improve its performance. I also need to retrieve the final weight values.
from scipy.optimize import minimize
import numpy
def objective(W,X1,X2,X3,X4,A):
W1=W[0]
W2=W[1]
W3=W[2]
W4=W[3]
X=numpy.vstack((W1*X1,W2*X2,W3*X3,W4*X4))
return numpy.sum((X-A)**2)
def constraint1(W):
W1=W[0]
W2=W[1]
W3=W[2]
W4=W[3]
return W1+W2+W3+W4-1
x0=[[0.25,0.25,0.25,0.25]]
cons = {'type': 'eq', 'fun':constraint1}
#Random data only used for purposes of example
segment_1 = numpy.random.rand(10000, 10)
segment_2 = numpy.random.rand(10000, 10)
segment_3 = numpy.random.rand(10000, 10)
segment_4 = numpy.random.rand(10000, 10)
A = numpy.random.rand(10000, 40)
sol=minimize(objective,x0[0],args=(segment_1,segment_2,segment_3,segment_4,A),method='SLSQP',constraints=cons)
print(sol.x)
Solution 1:[1]
I'm very new to mystic so take my advice/questions with a grain of salt. I think this should be easily doable. You can almost use the same syntax, just replace the minimize function from scipy with a mystic minimizer.
Mystic offers a few minimal interface minimizers that are almost the same as the minimize function from scipy.
Some examples are diffev2, sparsity, buckshot, lattice, fmin and fminpowell.. All of these functions apply different minimization algorithms. Depending on which algorithm you'd like to use you can just insert one. For more information you should check https://mystic.readthedocs.io/en/latest/mystic.html#minimal-interface.
Cheers
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Douwe van der Heijden |