error in ga optimization code

6 views (last 30 days)
Here is the code I'm trying to run for optimization
function finalResult = OptimizeScript()
% Decision variables are the
% Servers Capacity
% Process Time
% These are set by the genetic algorithm as it runs multiple
% simulations of the OptimizandMask model via the variable
% ResourceCapacity.
% Open a parallel pool
%pool = parpool;
% Setting plot and parallelization options
opts = gaoptimset(...
'PlotFcns', @gaplotbestf, ...
'Generations', 25, ...
'StallGenLimit', 10, ...
'UseParallel', 'always');
% Loading the model to run on all parallel workers
% Lower bound of decision variables
lb = [1 5];
% Upper bound of decision variables
ub = [10 15];
% Integer constraints: If the third was not an integer, it would be [1, 2,
% 4]
IntCon = [1 2];
% Track time spent for optimization
% Execute genetic algorithm solver
[finalResult, ~, ~] = ga(@productionCost, 2, [], [], ...
lb, ub, [], IntCon, opts);
% Shut down the parallel pool
% Cost function that assign different values to the decision variables in
% the model
function obj = productionCost(ResourceCapacity)
% Assigns costs to the values of ResourceCapacity, which correspond
% to [batch reactors, water tanks, heaters, drains]
cost = [1000 -200] * ResourceCapacity';
% Assigns variables to the base workspace for simulation
assignin('base', 'ResourceCapacity', ResourceCapacity);
% Simulation of the model and assigns output to the variable z
if isempty(find_system('type', 'block_diagram', ...
'Name', 'OptimizeandMask'))
set_param('OptimizeandMask/ConfigResource','ServerCapacity', ...
num2str(ResourceCapacity(1)), 'ProcessTime',...
[~, ~, z] = sim('OptimizeandMask');
% Takes the last value of the logged data as the final backlog
% value
backlog = z(end);
% Calculates the objective function, based on the backlog and costs
obj = backlog*10000 + cost;
but Matlab is giving error
Error using functionHandleOrCell (line 12)
The constraint function must be a function handle.
Error in validate (line 228)
[nonlcon,NonconFcnArgs] = functionHandleOrCell('NonconFcn',nonlcon);
Error in gacommon (line 65)
[options,nvars,FitnessFcn,NonconFcn] =
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in OptimizeScript (line 38)
[finalResult, ~, ~] = ga(@productionCost, 2, [], [], ...
I am unable to understand the error and solution because the syntax seems correct to me. can anybody help me out?

Accepted Answer

Walter Roberson
Walter Roberson on 2 May 2018
You missed the Aeq and beq parameters.
Walter Roberson
Walter Roberson on 2 May 2018
In some cases, using trailing ~ like you show in [finalresult,~,~] can result in extra computation for no useful reason, but in most cases there is no effective difference. It is better not to add trailing ~ like that.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by