Facing error in the optimization part. Kindly suggest solution

조회 수: 1 (최근 30일)
Akhil
Akhil 2024년 2월 27일
댓글: Cris LaPierre 2024년 2월 27일
clc;
clear;
a=[241.0, 301.0, 261.0, 221.0, 281.0, 361.0, 401.0, 361.0, 301.0, 321.0, 221.0, 281.0, 201.0, 201.0, 261.0, 141.0, 181.0, 201.0, 161.0, 121.0, 161.0, 221.0, 141.0, 161.0, 201.0, 221.0, 221.0, 201.0, 221.0, 221.0, 241.0, 301.0, 281.0, 221.0];
b=[130.0, 150.0, 190.0, 170.0, 210.0, 130.0, 170.0, 230.0, 210.0, 190.0, 230.0, 270.0, 290.0, 330.0, 330.0, 150.0, 130.0, 150.0, 150.0, 170.0, 190.0, 210.0, 230.0, 230.0, 70.0, 90.0, 110.0, 110.0, 130.0, 50.0, 110.0, 110.0, 70.0, 70.0];
lb_w=[1e-04, 1e-04, 1e-04,1e-04, 1e-04, 1e-04,1e-04];
ub_w=[10000,10000,10000,10000,10000,10000,10000];
reg1=[11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11];
reg2=[10,10,10,10,10,11,11,11,11,11,12,12,12,12,12,17,17,17,17,18,18,18,18,18,38,38,38,38,38,39,39,39,39,39];
lb_x=[283.6939,203.9050,179.9683,134.7546,100.1794, 177.3087,207.4512];
ub_x=[419.3351 313.8364 296.1055 210.9974 231.3879 242.9129 325.3615];
lb_y=[123.6309 109.4000 209.0163 122.7415 153.8715 53.3659 19.5675];
ub_y=[259.7138 215.2423 360.2195 165.4341 240.1463 144.0878 138.7512];
initial_x = zeros(1, 7);
initial_y = zeros(1, 7);
initial_w = zeros(1, 7);
disp(['initial_x: ', num2str(initial_x)])
initial_x: 0 0 0 0 0 0 0
% Concatenating initial guesses, lower bounds, and upper bounds
%initialVars = [initial_x, initial_y, initial_w];
initialVars = [lb_x, lb_y, lb_w];
lb = [lb_x, lb_y, lb_w];
ub = [ub_x, ub_y, ub_w];
disp(lb);
Columns 1 through 19 283.6939 203.9050 179.9683 134.7546 100.1794 177.3087 207.4512 123.6309 109.4000 209.0163 122.7415 153.8715 53.3659 19.5675 0.0001 0.0001 0.0001 0.0001 0.0001 Columns 20 through 21 0.0001 0.0001
disp(ub);
1.0e+04 * Columns 1 through 19 0.0419 0.0314 0.0296 0.0211 0.0231 0.0243 0.0325 0.0260 0.0215 0.0360 0.0165 0.0240 0.0144 0.0139 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 20 through 21 1.0000 1.0000
% Optimization options
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'interior-point');
% Defining the objective function
objFun = @(vars) objective(vars, a, b, reg1, reg2);
% Run optimization
[optimalVars, fval] = fmincon(objFun, initialVars, [], [], [], [], lb, ub, [], options);
Initial point X0 is not between bounds LB and UB; FMINCON shifted X0 to strictly satisfy the bounds.
Index exceeds the number of array elements. Index must not exceed 7.

Error in solution>objective (line 59)
p(i)=((x(r1)-a(i))^2 + (y(r1)-b(i))^2)-abs(w(r1));

Error in solution>@(vars)objective(vars,a,b,reg1,reg2) (line 29)
objFun = @(vars) objective(vars, a, b, reg1, reg2);

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

Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
% Extract optimal x, y, and w
optimal_x = optimalVars(1:7);
optimal_y = optimalVars(8:14);
optimal_w = optimalVars(15:end);
% Display results
disp('Optimal x:');
disp(optimal_x);
disp('Optimal y:');
disp(optimal_y);
disp('Optimal w:');
disp(optimal_w);
disp('Objective function value:');
disp(fval);
hold on
scatter(a,b,"blue","filled")
scatter(optimal_x, optimal_y, '*','red');
grid on;
hold off
% Objective function
function f = objective(vars, a, b, reg1, reg2)
x = vars(1:7);
y = vars(8:14);
w = vars(15:end);
f = 0;
for i = 1:a(1:5)
r1=reg1(i);
p(i)=((x(r1)-a(i))^2 + (y(r1)-b(i))^2)-abs(w(r1));
for j=1:a(6:34)
r2 = reg2(j);
q(j)=((x(r2)-a(j))^2 + (y(r2)-b(j))^2)-abs(w(r2));
f = f + p(i)-q(j);
end
end
end
error: Index exceeds the number of array elements. Index must not exceed 7.
Error in grid_weight>objective (line 106)
p(i)=((x(r1)-a(i))^2 + (y(r1)-b(i))^2)-abs(w(r1));
Error in grid_weight>@(vars)objective(vars,a,b,reg1,reg2) (line 71)
objFun = @(vars) objective(vars, a, b, reg1, reg2);
Error in fmincon (line 568)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in grid_weight (line 74)
[optimalVars, fval] = fmincon(objFun, initialVars, [], [], [], [], lb, ub, [], options);

답변 (1개)

Cris LaPierre
Cris LaPierre 2024년 2월 27일
You have an indexing issue. Namely, w(r1).
  • r1=reg1(i)
  • reg1(1) = 11
  • w only has 7 elements
reg1=[11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11];
w = [1e-04, 1e-04, 1e-04,1e-04, 1e-04, 1e-04,1e-04]
w = 1×7
1.0e-04 * 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
r1 = reg1(1)
r1 = 11
w(r1)
Index exceeds the number of array elements. Index must not exceed 7.
You need to adjust your code so that your index does not exceed the length of your vector.
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2024년 2월 27일
Also note that the following line of code is likely not doing what you think it is doing
for i = 1:a(1:5)
The end result will be the same as if you had written for i = 1:a(1)

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

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by