Insufficient number of outputs from right hand side of equal sign to satisfy assignment. Error in test14>@(x​)[g1(x);g2​(x);g3(x)] (line 49) nonlcon = @(x) [g1(x); g2(x); g3(x)

1 view (last 30 days)
Kindly guide in resovling the error. Thank you!!
close all;
clear all;
% Define parameter ranges
f_rad_min = 5; % minimum radar frequency
f_comm_min = 2e9; % minimum communication frequency
gamma_min = 0.5; % minimum SINR threshold
P_max = 10; % maximum power
f_rad_max = 1000; % maximum radar frequency
gamma_max = 5; % maximum SINR threshold
f_comm_max= 10e9;
w1 = 0.5; % weight for communication rate
w2 = 0.25; % weight for interference power
w3 = 0.25; % weight for total power
% Define objective functions
f1 = @(x) -1 * x(1);
f2 = @(x) x(2); % minimize interference power
f3 = @(x) x(3); % minimize total power
f = @(x) w1*f1(x) + w2*f2(x) + w3*f3(x);
% Define constraints
g1 = @(x) -(x(3) - x(2));
g2 = @(x) x(1) - log2(1 + x(4)*x(5)/(x(3) - x(2) + eps));
g3 = @(x) x(2) - x(6); % ensure P_int <= gamma
g4 = @(x) x(4) - f_rad_min; % ensure f_rad >= f_rad_min
g5 = @(x) x(5) - f_comm_min; % ensure f_comm >= f_comm_min
g6 = @(x) x(3) - P_max; % ensure P_total <= P_max
g7 = @(x) f_rad_max - x(4); % ensure f_rad <= f_rad_max
g8 = @(x) gamma_max - x(6); % ensure gamma <= gamma_max
% Define number of variables and population size
nvars = 6; % number of variables
popsize = 1000; % population size
lb =[f_rad_min, f_comm_min, 0, gamma_min, gamma_min, 0.1];
ub =[f_rad_max, f_comm_max, P_max, gamma_max, gamma_max, gamma_max];
% Run genetic algorithm
options = optimoptions('ga', 'PopulationSize', popsize, 'MaxGenerations', 5000);
% Define initial population
init_pop = repmat([f_rad_min, f_comm_min, 0.5*P_max, gamma_min, gamma_min, gamma_min], popsize, 1) ...
+ rand(popsize, nvars) .* repmat([f_rad_max-f_rad_min, f_comm_max-f_comm_min, P_max, gamma_max-gamma_min, gamma_max-gamma_min, gamma_max-gamma_min], popsize, 1);
% Clip the values within the bounds
init_pop = min(max(init_pop, [f_rad_min, f_comm_min, 0, gamma_min, gamma_min, gamma_min]), [f_rad_max, f_comm_max, P_max, gamma_max, gamma_max, gamma_max]);
% Define constraints
nonlcon = @(x) [g1(x); g2(x); g3(x)];
% Run the GA optimization multiple times to get multiple solutions
num_solutions = 5;
solutions = zeros(num_solutions, nvars);
fvals = zeros(num_solutions, 3);
for i = 1:num_solutions
[x, fval] = ga(@(x) f(x), nvars, [], [], [], [], lb, ub, nonlcon, options);
% Store the solution and fval
solutions(i, :) = x;
fvals(i, :) = fval;
% Add the current solution to the initial population for the next iteration
init_pop(i, :) = x;
% Print the current iteration
fprintf('Iteration %d: fval = %s\n', i, mat2str(fval))
end
% Plot the Pareto front for all solutions
figure
scatter3(-fvals(:,1), fvals(:,2), fvals(:,3), 'filled')
xlabel('Communication rate')
ylabel('Interference power')
zlabel('Power consumption')
title('Pareto front')
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Error in test14>@(x)[g1(x);g2(x);g3(x)] (line 49)
nonlcon = @(x) [g1(x); g2(x); g3(x)];
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in constrValidate (line 23)
[cineq,ceq] = nonlcon(Iterate.x');
Error in gacommon (line 124)
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...
Error in ga (line 364)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in test14 (line 58)
[x, fval] = ga(@(x) f(x), nvars, [], [], [], [], lb, ub, nonlcon, options);
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.

Answers (1)

Torsten
Torsten on 10 Apr 2023
Edited: Torsten on 10 Apr 2023
nonlcon has two output arguments, not only one as set in your code:
nonlcon = @(x) [g1(x); g2(x); g3(x)];
It will be easiest to define "nonlcon" directly as a function, not as a function handle.
function [c,ceq] = nonlcon(x,g1,g2,g3)
c = [g1(x);g2(x);g3(x)];
ceq = [];
end
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!