Optimization: Optimize multiple input variables to minimize the output

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

Matt J
Matt J 2021년 10월 3일
편집: Matt J 2021년 10월 3일
Isn't the solution obviously going to be that all variables xyz,p,q,r=0.1 ? Since the variables are constrained to be positive, the objective function is independently monotonic in all the variables. Therefore, setting them all to their smallest possible values will oviously minimize the function.
Clearly, I am doing it wrong :( ( see below )
It's not clear because you haven't shown the output and discussed what you don't like about it.
Apologies.
Below is the code with output (P) and the optimized variables(x1,y1,z1,p1,q1,r1). Clearly they are off the charts.
If I simply execute the function with my initial guesses, the output P=.75. ( added those calculations as well below.)
I am guessing, that I might be incorrect with the sybtaxm but I am unable to determine whats wrong.
Also, guessing, I should bound it (if so, now sure how?) so it doesnt go off charts, espcially I need the optimized variables to remain positive numbers.
%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)
fun = function_handle with value:
@(X)f(X,l,w)
X_guess = [1 1.5 1 2 1.25 1];
Xmin = fminsearch(fun,X_guess)
Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: -302178148141371722286612819344701945253341106128754416021789277177203582000056690981679928524741379537028251648.000000
Xmin = 1×6
1.0e+55 * -0.1661 0.8198 0.0940 -1.8024 -2.2644 0.4914
x1 = Xmin(1);
y1 = Xmin(2);
z1 = Xmin(3);
p1 = Xmin(4);
q1 = Xmin(5);
r1 = Xmin(6);
P= ((x1*p1)+(y1*q1)+(z1*r1))*l*w
P = -3.0218e+110
If I simply execute the function with my initial guesses:
l =2;
w=1;
X = [1 1.5 1 2 1.25 1];
P = (X(1)*X(4) + X(2)*X(5) + X(3)*X(6))*l*w
P = 9.7500

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2021년 10월 3일

0 개 추천

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:
  1. 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
  2. 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
  3. 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 Walter I followed one of the fmincon example and below is the code. Sorry I didnt know how to integrate ndgrid, ga() to this code. Not sure if I am going in the right direction.
fun = @(x)(x(1)*x(4) + x(2)*x(5) + x(3)*x(6))*l*w; ;
%Declaring extra parameter values
l=2 ;
w=1;
%Look in the region where has positive values, , and .
lb = [0,0,0,0,0,0];
ub = [10,10,10,10,10,10];
%The problem has no linear constraints, so set those arguments to [].
A = [];
b = [];
Aeq = [];
beq = [];
%Intial guss
x0 = [1 2 1 1.5 2 2];
%Solving the problem using fmincon
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Unrecognized function or variable 'l'.

Error in solution (line 1)
fun = @(x)(x(1)*x(4) + x(2)*x(5) + x(3)*x(6))*l*w; ;

Error in fmincon (line 568)
initVals.f = feval(funfcn{3},X,varargin{:});

Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
%Minimized power
P = (x(1)*x(4) + x(2)*x(5) + x(3)*x(6))*l*w
%% A different initial point can lead to a different solution.
x01 = x0/5;
x0 = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
%Minimized power
P1 = (x(1)*x(4) + x(2)*x(5) + x(3)*x(6))*l*w
%Actual Power
P2 = (X(1)*X(4) + X(2)*X(5) + X(3)*X(6))*l*w
format long g
%Declaring extra parameter values
l=2 ;
w=1;
fun = @(x)(x(1)*x(4) + x(2)*x(5) + x(3)*x(6))*l*w; ;
%Look in the region where has positive values, , and .
lb = zeros(1,6);
ub = 100*ones(1,6);
%The problem has no linear constraints, so set those arguments to [].
A = [];
b = [];
Aeq = [];
beq = [];
%Intial guss
x0 = [1 2 1 1.5 2 2];
%Solving the problem using fmincon
[x, P] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 1×6
0.0000 0.0005 0.0000 0.2510 0.0005 0.7280
P =
1.27675251615493e-06
%% A different initial point can lead to a different solution.
x01 = x0/5;
[x0, P1] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x0 = 1×6
0.0000 0.0005 0.0000 0.2510 0.0005 0.7280
P1 =
1.27675251615493e-06
But like I said, the real minima is at the lower bound
fun(lb)
ans =
0
@Walter Roberson Thank you!!
I get what you are saying. Setting lower bound as 0 will default all the variables to 0 as its the real minima. Was hoping to prevent hitting negative values with lower bounds.
However, assuming the variables are interdependent, will I be able to minimize if I provide them in nested loop in the below fashion?
for x=0.1 to 100 step 0.1
for z=x+0.1 to 100 step 0.1
for w= 0.1 to 100 step 0.1
for t = w+0.1 to 100 step 0.1
You probably do not have enough memory to use the ndgrid approach for all combinations up to 100, but you can see from this that the minima is at the lower bound, so testing higher values is pointless.
format long g
%Declaring extra parameter values
l=2 ;
w=1;
fun = @(x)(x(:,1).*x(:,4) + x(:,2).*x(:,5) + x(:,3).*x(6)).*l.*w;
[X1, X2, X3, X4, X5, X6] = ndgrid(0:.1:2);
X = [X1(:), X2(:), X3(:), X4(:), X5(:), X6(:)];
P = fun(X);
[bestP, idx] = min(P(:))
bestP =
0
idx =
1
best_X = X(idx,:)
best_X = 1×6
0 0 0 0 0 0
I have rephrased my question and I hope it will make sense now.
Below is the code, however, I am unable to code the portion where I am trying to minimize a,b and c.
% Objective:Optimize the lengths to minimze the power using a new variable.
%Constants
k= 1;
w=1;
%Variable lengths
l1 = 3*(5)^0.5;
l2 = 5*(2)^0.5
l3 = sqrt(5);
v1 = sqrt(5);
v2 = 2*sqrt(2);
v3 = 2*sqrt(5);
% Expressing the l and v in terms of a b and c. Open to expressing differently
% if it helps the objective below.
a = l1*v1;
b = l2*v2;
c = l3*v3;
%Actual Power
P = (l1*v1 + l2*v2 + l3*v3)*k*w; % Power
% Objective
%To search and find the values for a, b and c to minimize power
%Considering x to represent a,b and c.
fun = @(x)(x(:,1) + x(:,2) + x(:,3)).*k.*w;
[X1, X2, X3] = ndgrid(0:.1:2); % Should I give this condition in a nested loop?
X = [X1(:), X2(:), X3(:)];
P = fun(X);
[bestP, idx] = min(P(:))
best_X = X(idx,:)
I have the constrains included. Will it make any difference now? and can it be minimized?
I do not see where you posted the constraints?
Sorry Sir. I think it got deleted. Below is the code with constrains.
I am not sure whats wrong with the below
% Objective:Optimize the lengths to minimze the power using a new variable.
%Constants
k= 2;
w=1;
v=1.5
%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;
%Actual Power
P = (AB.*VAB + BC.*VBC + CS.*VCS).*k*w; % Power
%
% Objective
%To search and find the values for a, b, c d and eto minimize power
% Not sure how to write the function for above
fun = @(x)(x(:,1) + x(:,2) + x(:,3) + x(:,4) + x(:,5)).*k.*w;
a = 0.1:20;
b = 0.1:20;
d = a + 0.1:20;
c = b + 0.1:20;
e = d + 0.1:20;
%
% [X1, X2, X3, X4, X5] = ndgrid(0:.1:2); % Should I give this condition in a nested loop?
%
X = [a(:), b(:), c(:), d(:), e(:)];
%
P = fun(X);
[bestP, idx] = min(P(:))
best_X = X(idx,:)
To clarify, c,d and e have constrains. a and b assignment initial guesses. I think my decalration is inccorect, its more of a pesudo code.
I do not see any constraints expressed here.
I have the constrains added below. I incorrectly assumed constrains can be included in the bounds. I have updated the code with Optim problem approach. ot sure why its throwing error.
%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;
%the objective function in prob.
prob.Objective = P;
%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;
%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)
Solving problem using fmincon. Feasible point with lower objective function value found. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol = struct with fields:
a: 0.1000 b: 0.2000 c: 0.3000 d: 9.2995 e: 9.6959

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2021년 10월 3일

댓글:

2021년 10월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by