Optimization with custom constraint

I am trying to formulate a linear optimization problem with a simple cost function of the form
Cost = X*Price_X + Y*Price_Y
But additionally, I have a parameter Z that I had obtained before through an iterative process. Z is a 2x2 matrix, where each value index is the value of X and Y.
Example: ParameterValue = Z(X,Y)
In my optimization problem, on top of having to minimize the cost of X and Y, the ParameterValue has to be within certain limits, or under a certain threshold
My Question is: is it possible to pass that parameter as a function handle, although it is a function of the other variables? Or should I formulate a multi-objective algorithm where ParameterValue is another objective function.
What I have tried was to have the following code:
if true
Value= @(X,Y) Z(X,Y) %To have value as an anonymous function
ObjFunc = PriceX * X + PriceY * Y + 0*Value(X,Y)
end
Then adding the threshold as a constraint for Value. But this has not worked at all.
Is my formulation, procedure right? or doable in any case? or should I try to do some multi-objective linear optimization if possible. Please note that the values of Z (2x2 matrix)are obtained before, so I also need to call them somehow to the optimization function.
Thank you in advance.

 采纳的回答

Alan Weiss
Alan Weiss 2018-5-14
I suggest that you read the documentation on how to specify an objective function and how to specify a nonlinear constraint.
Alan Weiss
MATLAB mathematical toolbox documentation

3 个评论

Thank you for your answer. I have tried specifying the parameter value as a non linear constraint but I am having a problem.
Following the "Specify a nonlinear constraint" document I did the following
if true
Z = @(x,y) input(x,y); %input is the value I had computed before
c = @(x) Z(x(2),x(1)) - 0.3; % so I want Z for the values of x1 and x2 to be less than 0.3
ceq=[];
nonlinconstraint = @(x) deal(c(x),ceq(x));
obj = @(x) Price1*x(1) + Price2*x(2); %Price1 and Price2 are already introduced variables
opts = optimoptions(@fmincon,'Algorithm','sqp');
z = fmincon(obj,[lb;ub],[],[],[],[],lb,ub,nonlinconstraint,opts);
end
But this doesn't seem to be working, I get the "index exceeds matrix dimensions" error.
Is this lookup function handle possible? The reason I am doing this is because every combination of X and Y has a value parameter Z that is not a direct function of X and Y.
The only alternative to this that I am seeing is to create a surface fit function and implement the coefficients of the function as a constraint instead. But I would prefer to just go to the look-up if possible.
Thank you. Ali
I think that your x0 input to fmincon has the wrong number of elements. Each of lb and ub should be the same size, and your x0 has twice as many elements as they do.
Alan Weiss
MATLAB mathematical toolbox documentation
Thanks, I will try it out!

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by