error in ga optimization code

조회 수: 1 (최근 30일)
summyia qamar
summyia qamar 2018년 5월 2일
댓글: Walter Roberson 2018년 5월 2일
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
%pctRunOnAll('load_system(''OptimizeandMask'')');
load_system('OptimizeandMask');
% 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
tic;
% Execute genetic algorithm solver
[finalResult, ~, ~] = ga(@productionCost, 2, [], [], ...
lb, ub, [], IntCon, opts);
toc;
% Shut down the parallel pool
%delete(pool);
end
% 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'))
load_system('OptimizeandMask');
end
set_param('OptimizeandMask/ConfigResource','ServerCapacity', ...
num2str(ResourceCapacity(1)), 'ProcessTime',...
num2str(ResourceCapacity(2)));
[~, ~, 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;
end
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] =
validate(options,type,nvars,fun,nonlcon,user_options);
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?

채택된 답변

Walter Roberson
Walter Roberson 2018년 5월 2일
You missed the Aeq and beq parameters.
  댓글 수: 3
summyia qamar
summyia qamar 2018년 5월 2일
I understand now the error but can you please explain the difference in
[finalresult,~,~] and simple finalresult.?
Walter Roberson
Walter Roberson 2018년 5월 2일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by