Optimization: Optimize multiple input variables to minimize the output
43 次查看(过去 30 天)
显示 更早的评论
Hello
I am looking to optimize multiple input variables to minimize the output using fminsearch.
Clearly, I am doing it wrong :( ( see below ) Below is my initial attempt.
Ultimately wanted to bound the predictions for all the variables ( x,y,z,p,q,r) from 0.1 to 100 in the step 0.1
Any help will be greatly appreciated.Thanks a ton!
%Objective: Attempting to Minimize function output with respect to multiple input variables
% Wanted to minimize function, Pow(X) = ((x*p) + (y*q) + (z*r) ) *l*w), by
% optimizing the variables, x, y,z,p ,q and r.
%l and w are constants
%Creating the objective function with its extra parameters( l,w) as extra arguments.
f =@(X, l,w)(X(1)*X(4) + X(2)*X(5) + X(3)*X(6))*l*w; %
%Declaring extra parameter values
l =2;
w=1;
%Create an anonymous function of x alone that includes the workspace value of the parameter.
fun =@(X)f(X,l,w)
%x0 = [-1,1.9];
X_guess = [1 1.5 1 2 1.25 1];
Xmin = fminsearch(fun,X_guess)
x1 = Xmin(1);
y1 = Xmin(2);
z1 = Xmin(3);
p1 = Xmin(4);
p2 = Xmin(5);
p3 = Xmin(6);
4 个评论
采纳的回答
Walter Roberson
2021-10-3
Ultimately wanted to bound the predictions for all the variables ( x,y,z,p,q,r) from 0.1 to 100 in the step 0.1
fminsearch() cannot bound variables. fmincon() can bound variables though.
However, you have discrete variables. fminsearch() and fmincon() cannot handle discrete variables.
You have a few options:
- use ga() with each of those variables being marked as having an integer constraint from 1 to 1000 (not 100), and divide each variable by 10 inside the objective function; or
- Use ndgrid() to construct all of the possible combinations of inputs, and evaluate the function at all of them and take the minimum of all of the evaluations
- recognize that multiplying positive values by positive values and summing them is always going to have its minima when the values are as small as possible, so just take the lower bounds of everything and do not bother optimizing.
13 个评论
Walter Roberson
2021-10-8
%defining optimization variables and an optimization problem object.
a = optimvar('a','LowerBound',0.1,"UpperBound",20);
b = optimvar('b','LowerBound',0.1,"UpperBound",20);
c = optimvar('c','LowerBound',0.1,"UpperBound",20);
d = optimvar('d','LowerBound',0.1,"UpperBound",20);
e = optimvar('e','LowerBound',0.1,"UpperBound",20);
prob = optimproblem;
k= 2;
w=1;
v=1.5;
%constraints
% cons1 = e >= (a+d);
% cons2 = d >=a ;
% cons3 = b <=c ;
% cons4 = (e-d) >= (d-a) ;
% cons5 = (c-b) <= b;
cons1 = e - a- d >= 0.1;
cons2 = d - a >= 0.1 ;
cons3 = c-b >= 0.1;
cons4 = b - a >= 0.1 ;
cons5 = (e-c) >=0.1;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;
prob.Constraints.cons4 = cons4;
prob.Constraints.cons5 = cons5;
x0.a = 4;
x0.b = 6;
x0.c = 8;
x0.d = 7;
x0.e = 12;
%new variables
AB = sqrt(a.^2 + b.^2);
BC = sqrt( c.^2 + ((e-d)/2).^2 );
CS = sqrt( c.^2 + ((e-d)/2).^2 );
VAB = sqrt(((((a.*v).^2/(((b.^2).*4))) + (v^2)/2 )));
% VBS = sqrt(((a*v)^2/((4*b*b)) + (v^2)/2 ));
VCS = ((2*c)./(e-d)).*sqrt(AB.^2);
VBC= CS.^2 + BC.^2;
%objective function as an expression in the optimization variables.
P = (AB.*VAB + BC.*VBC + CS.*VCS).*k*w;
%the objective function in prob.
prob.Objective = P;
sol = solve(prob, x0)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!