error in ga optimization code
1 view (last 30 days)
Show older comments
summyia qamar
on 2 May 2018
Commented: Walter Roberson
on 2 May 2018
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?
0 Comments
Accepted Answer
Walter Roberson
on 2 May 2018
You missed the Aeq and beq parameters.
3 Comments
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.
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!