Error in using parallel computing with gamultiobj

3 vues (au cours des 30 derniers jours)
Jonathan
Jonathan le 25 Juil 2023
Commenté : Jonathan le 25 Juil 2023
Hello,
I have a problem using the parallel computing in 'gamultiobj' function. I call the optimization algorithm by using:
ObjFun = @(x)FitnessFunction(x, Data);
options = optimoptions("gamultiobj","UseParallel",true,"UseVectorized",false,'PopulationSize',30,'MaxGenerations',60, 'MaxStallGenerations', 10);
[pareto,fval,exitflag,output,population,scores] = gamultiobj(ObjFun,n_vars,[],[],[],[],lb,ub,[],intcon,options);
The function work perfectly if "UseParallel" is set as "false" but if it is set as "true" I get diffent errors.
If I use "Parallel Preferences"->"Parallel environment"->"Default profile" = Threads, I get this error:
Error using linprog
LINPROG has stopped because it encountered an internal error. We are sorry for the inconvenience.
Please contact technical support for assistance with your problem, quoting the code "-1000@-1000".
Error in CuttingBinProblem (line 32)
[values,nLogs,exitflag,~,lambda] = linprog(f,A,b,[],[],lb,[],lpopts);
Error in FitnessFunction (line 64)
n_bin = CuttingBinProblem(cnt_unique, unique_l, logLength);
Error in main_Opt_GA>@(x)FitnessFunction(x,Data) (line 77)
ObjFun = @(x)FitnessFunction(x, Data);
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in objAndConVectorizer (line 37)
parfor (i = 1:popSize)
Error in gamultiobjMakeState (line 93)
[Score,C,Ceq,isFeas] = objAndConVectorizer(state.Population(initScoreProvided+1:end,:), ...
Error in gamultiobjsolve (line 20)
state = gamultiobjMakeState(GenomeLength,FitnessFcn,ConstrFcn,output.problemtype,options);
Error in gamultiobj (line 338)
[x,fval,exitFlag,output,population,scores,residuals] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in main_Opt_GA (line 81)
[pareto,fval,exitflag,output,population,scores] = gamultiobj(ObjFun,n_vars,[],[],[],[],lb,ub,[],intcon,options);
Caused by:
Failure in user-supplied fitness function evaluation. Cannot continue.
In this case the function CuttingBinProblem is the function available on mathworks website (https://it.mathworks.com/help/optim/ug/cutting-stock-problem-solver-based.html) that I have slightly modified as follows:
function logsUsed = CuttingBinProblem(quantity,lengthlist,logLength)
%% Cutting bin problem
% https://it.mathworks.com/help/optim/ug/cutting-stock-problem-solver-based.html
% logLength = 12; % Length of the bins
% lengthlist = [1.5; 1.78; 1.30; 2.0; 3.2];
% quantity = [200; 42; 55; 30; 60];
nLengths = length(lengthlist);
patterns = diag(floor(logLength./lengthlist));
nPatterns = size(patterns,2);
lb2 = zeros(nLengths,1);
A2 = lengthlist';
b2 = logLength;
lpopts = optimoptions('linprog','Display','off');
ipopts = optimoptions('intlinprog',lpopts);
reducedCost = -Inf;
reducedCostTolerance = -0.0001;
exitflag = 1;
while reducedCost < reducedCostTolerance && exitflag > 0
lb = zeros(nPatterns,1);
f = lb + 1;
A = -patterns;
b = -quantity;
[values,nLogs,exitflag,~,lambda] = linprog(f,A,b,[],[],lb,[],lpopts);
if exitflag > 0
% fprintf('Using %g logs\n',nLogs);
% Now generate a new pattern, if possible
f2 = -lambda.ineqlin;
[values,reducedCost,pexitflag] = intlinprog(f2,1:nLengths,A2,b2,[],[],lb2,[],ipopts);
reducedCost = 1 + reducedCost; % continue if this reducedCost is negative
newpattern = round(values);
if pexitflag > 0 && reducedCost < reducedCostTolerance
patterns = [patterns newpattern];
nPatterns = nPatterns + 1;
end
end
end
if exitflag <= 0
disp('Error in column generation phase')
else
[values,logsUsed,exitflag] = intlinprog(f,1:length(lb),A,b,[],[],lb,[],[],ipopts);
if exitflag > 0
values = round(values);
logsUsed = round(logsUsed);
else
disp('Error in final optimization')
end
end
If I use "Parallel Preferences"->"Parallel environment"->"Default profile" = Processes, I get this error:
Error using StructuralAnalysis
Index in position 1 exceeds array bounds.
Error in AreaCalculation (line 16)
Verified = StructuralAnalysis(DataStru,index);
Error in FitnessFunction (line 67)
[A, indexTable] = AreaCalculation(DataStru);
Error in main_Opt_GA>@(x)FitnessFunction(x,Data) (line 77)
ObjFun = @(x)FitnessFunction(x, Data);
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in objAndConVectorizer (line 37)
parfor (i = 1:popSize)
Error in gamultiobjMakeState (line 93)
[Score,C,Ceq,isFeas] = objAndConVectorizer(state.Population(initScoreProvided+1:end,:), ...
Error in gamultiobjsolve (line 20)
state = gamultiobjMakeState(GenomeLength,FitnessFcn,ConstrFcn,output.problemtype,options);
Error in gamultiobj (line 338)
[x,fval,exitFlag,output,population,scores,residuals] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in main_Opt_GA (line 81)
[pareto,fval,exitflag,output,population,scores] = gamultiobj(ObjFun,n_vars,[],[],[],[],lb,ub,[],intcon,options);
Caused by:
Failure in user-supplied fitness function evaluation. Cannot continue.
In this case the function AreaCalculation is the following:
function [A,index] = AreaCalculation(DataStru)
global CrossSecTable
% Assign initial value to index and
index = 0;
Verified = 1;
while Verified == 1
index = index+1;
if index > length(CrossSecTable)
Verified = 2;
end
Verified = StructuralAnalysis(DataStru,index);
end
A = 0.0001*CrossSecTable(index,4);
end
StructuralAnalysis is the following:
function Verified1 = StructuralAnalysis(DataStru,index)
global CrossSecTable
n_vinc = DataStru.n_vinc;
load = [0;0;-10;0;0;0]; % [kN]
% Initialize
% (ns=number of sections; nn=number of nodes; ne=number of elements)
ns=1; nn=size(DataStru.u_end,1); ne=size(DataStru.link,1);
stru=InitializeStructure(ns,nn,ne);
% Cross sections and material
s=1;
stru.sec(s).nome='C 88.9 x 3.2';
A=0.0001*CrossSecTable(index,4); % [m^2]
J=(10^(-8))*CrossSecTable(index,6); % [m^4]
rho = 78.5; % [kN/m^3] peso per unità di volume acciaio
fy = 355; % [MPa]
E=2.1e8; % [kN/m^2]
G=E/2.4;
stru.sec(s).Ks=[E*J 0 0 0; 0 E*J 0 0; 0 0 E*A 0; 0 0 0 2*G*J];
% Nodes
for n = 1: nn
stru.node(n).x=DataStru.u_end(n,:)';
if n<=n_vinc
stru.node(n).v=[1;1;1;1;1;1];
else
stru.node(n).f=load;
end
end
% Elements
for e = 1: ne
stru.elem(e).nodes=DataStru.link(e,:)';
stru.elem(e).nz = 3;
end
for e=1:ne
stru.elem(e).form='EBBeam3D';
stru.elem(e).sec=1;
stru.elem(e).vr=[0;0;1];
stru.elem(e).qg = [0;0;-A*rho;0;0;0];
end
%% Analysis
stru.Analysis='Static';
stru=AnalyzeStructure(stru);
%% Verifications
Verified1 = 0;
% Max displacement
for iii = 1:ne
Ncr = -(pi^2)*E*J/(stru.elem(iii).L^2);
ymax = CrossSecTable(index,1)/1000/2;
N = max(stru.elem(iii).f(3,1:3));
M = max(stru.elem(iii).f(5,1:3));
Sigma_max = abs((N/A + M/J*ymax)/1000); % [MPa]
if Sigma_max > fy
Verified1 = 1;
break;
elseif N < Ncr
Verified1 = 1;
break;
end
end
% Max stress and linear buckling
if Verified1 == 0
for jjj = n_vinc+1:nn
Disp = abs(stru.node(jjj).d(3));
if Disp > 15/250
Verified1 = 1;
break;
end
end
end
toc
end
I cannot understand why the code works without the parallel computing but it is not able to run in parallel.

Réponses (1)

Walter Roberson
Walter Roberson le 25 Juil 2023
You cannot use global with parallel. Or to be more accurate, no global variables will be passed between processes so they will start out empty in the workers.
  1 commentaire
Jonathan
Jonathan le 25 Juil 2023
I eliminated the global and now it is running with "Parallel Preferences"->"Parallel environment"->"Default profile" = Processes but still not with "Parallel Preferences"->"Parallel environment"->"Default profile" = Threads. The error is:
Error using linprog
LINPROG has stopped because it encountered an internal error. We are sorry for the inconvenience.
Please contact technical support for assistance with your problem, quoting the code "-1000@-1000".
Error in CuttingBinProblem (line 32)
[values,nLogs,exitflag,~,lambda] = linprog(f,A,b,[],[],lb,[],lpopts);
Error in FitnessFunction (line 65)
n_bin = CuttingBinProblem(cnt_unique, unique_l, logLength);
Error in main_Opt_GA>@(x)FitnessFunction(x,Data) (line 77)
ObjFun = @(x)FitnessFunction(x, Data);
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in objAndConVectorizer (line 37)
parfor (i = 1:popSize)
Error in gamultiobjMakeState (line 93)
[Score,C,Ceq,isFeas] = objAndConVectorizer(state.Population(initScoreProvided+1:end,:), ...
Error in gamultiobjsolve (line 20)
state = gamultiobjMakeState(GenomeLength,FitnessFcn,ConstrFcn,output.problemtype,options);
Error in gamultiobj (line 338)
[x,fval,exitFlag,output,population,scores,residuals] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in main_Opt_GA (line 81)
[pareto,fval,exitflag,output,population,scores] = gamultiobj(ObjFun,n_vars,[],[],[],[],lb,ub,[],intcon,options);
Caused by:
Failure in user-supplied fitness function evaluation. Cannot continue.

Connectez-vous pour commenter.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by