Filter löschen
Filter löschen

gamultiobj output function error -- too many output arguments

6 Ansichten (letzte 30 Tage)
Zixuan Zhang
Zixuan Zhang am 6 Dez. 2023
Beantwortet: Aditya am 12 Dez. 2023
I used 'gamultiobj' for a multi-objective task. I have two objectives and I would like to save the results of each iteration as the output. But Matlab gave me a warning says 'too many output arguments'
here is my code:
% Define the objective function for NSGA-II optimization
objectiveFunction = @(x) plateObjective(x);
% Define the number of design variables
numVariables = 3;
% Define lower and upper bounds for the design variables
lb = [20, 10, 2]; % Lower bounds for width, radius, and thickness
ub = [100, 50, 10]; % Upper bounds for width, radius, and thickness
% Define options for the NSGA-II algorithm
options = optimoptions('gamultiobj', ...
'PopulationSize', 100, ...
'MaxGenerations', 100, ...
'FunctionTolerance', 1e-4, ...
'PlotFcn', @gaplotpareto, ...
'OutputFcns', @outputfunction);
% Run NSGA-II optimization
[xOptimal, fvalOptimal, exitflag, output, population, scores] = gamultiobj(objectiveFunction, numVariables, [], [], [], [], lb, ub, options);
options = optimoptions('ga','OutputFcn',{@myfun1,@myfun2});
function stop = outputfunction(options, state, flag)
persistent history
stop = false;
switch flag
case 'init'
history = struct('generation', [], 'population', [], 'scores', []);
case 'iter'
% Append current generation, population, and scores to the history
history.generation = [history.generation; state.Generation];
history.population = [history.population; state.Population];
history.scores = [history.scores; state.Score];
case 'done'
% Save the final generation, population, and scores
history.generation = [history.generation; state.Generation];
history.population = [history.population; state.Population];
history.scores = [history.scores; state.Score];
% Save the history structure to a file (you can customize the filename)
save('optimization_history.mat', 'history');
end
end
% Function to define the objective for NSGA-II
function f = plateObjective(x)
% x(1): Width, x(2): Radius, x(3): Thickness
% Set the dimensions
MyWidth = x(1);
MyRadius = x(2);
MyThickness = x(3);
% Check the constraint: MyWidth > MyRadius
if MyWidth <= MyRadius
% Penalize the objective for violating the constraint
penalty = 1e6; % Set penalty value
f = [penalty, 0]; % Ask optimizer to avoid this solution
return;
end
% Write dimensions to input.txt
vals = [MyWidth, MyRadius, MyThickness];
fileID = fopen('input.txt', 'w');
fprintf(fileID, '%i\n %i\n %0.2f', vals);
fclose(fileID);
% Run Abaqus simulation
system('abaqus cae nogui=abaqus_script_platehole.py -- PlateHole');
% Read maximum von Mises stress from output.txt
fileID = fopen('output.txt', 'r');
S_vm_max = fscanf(fileID, '%f');
fclose(fileID);
% Calculate plate weight (you may need to customize this based on your specific problem)
plateWeight = (MyWidth^2-pi*MyRadius^2)*MyThickness*2700;
% Define the objective vector for NSGA-II
f = [S_vm_max, plateWeight];
end

Antworten (1)

Aditya
Aditya am 12 Dez. 2023
Hi Zixuan,
It seems you're facing a problem with the gamultiobj function, specifically encountering a 'too many output arguments' error.
This error typically occurs when the output function specified in the options for gamultiobj does not match the expected signature.
The output function for ‘gamultiobj should return three output arguments: state, options, and stop. Here's the correct structure for the output function:
function [state, options, stop] = outputfunction(options, state, flag)
end
Hope this helps!

Kategorien

Mehr zu Multiobjective Optimization finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by