Optimization function break and start over with new dataset

4 次查看(过去 30 天)
I have a Surrogate optimization program which is searching for the optimal workpiece position on the table. It searches only x, y and rotation angle around z-axis (3 variables).
% SURROGATE OPTIMIZATION
clear;clc; rng default
lb = [-0.34,-0.85,0];
ub = [0.34,-0.25,360];
A = []; b = []; Aeq = []; beq = []; intcon = [];
nvars = 3;
options = optimoptions('surrogateopt','PlotFcn','surrogateoptplot');
[xopt,fval,exitflag,output] = surrogateopt(@ManipulabilityFun,lb,ub,intcon,A,b,Aeq,beq,options);
And then I have this optimization function:
% Optimization setup
load ('T.mat');
xos=xopt(1);
yos=xopt(2);
rotz=xopt(3);
Z = -0.095845; %Z is fixed
rot=trotz(rotz,'deg');
TWtoB_org= [1.000000, 0.000000, 0.000000, xos; %X
0.000000, 1.000000, 0.000000, yos; %Y
0.000000, 0.000000, 1.000000, Z; %Z(fixed)
0.000000, 0.000000, 0.000000, 1.000000]; %Horizontal Option1
TWtoB_org=TWtoB_org*rot;
TPtoB=pagemtimes(TWtoB_org,T);
%% REACHABILITY
n2=0;
for b=1:size(TPtoB1,4)
n2=n2+1;
q1=ur5_kin.inverse_kinematics(TPtoB1(:,:,:,n2)*Ttransl, 0);
if isempty(q1)
q=zeros(1,6);
else
for c=1:size(q1,1)
config=conf_func(q1(c,:));
if config(1) == 1 && config(2) == 0 && config(3) == 1
q=q1(c,:);
end
end
if exist ('q','var')~=1
q=zeros(1,6);
end
end
Sol1(n2,:)=[q];
clearvars q q1
S1=find(Sol1(:,1)==0);
S2=n2-size(S1,1);
Part_reach1=-S2/size(TPtoB,4)*100; %Reachability of part - NEGATIVE FOR OPTIMIZATION
end
if Part_reach1 ~=-100 %IF reachability is not 100% set joint values to 0
Sol2=zeros(size(Sol2,1),6);
end
If I understand correctly optimization program generates random n variables for x y and rotation angle which are then put into the optimization function. Some random values that surrogate optimization generators are not optimal and I am solving this with some kind of weights like here:
if Part_reach1 ~=-100 %IF reachability is not 100% set joint values to 0
Sol2=zeros(size(Sol2,1),6);
end
So if based on the input data (workpiece position), not all the points on the workpiece in that position are reachable I am changing all solutions to zeros to lower the average value. I need to do this because the program goes further and in the next step I am using this artificially lowered values. This has become difficult to navigate throughout the program and I am starting to have problems (Code smell).
My question is: When condition in optimization function is not satisfied, how can I break the program so it goes completely from beginning and starts with new data?

回答(1 个)

Anurag
Anurag 2023-10-25
Hi RoboTomo,
I understand that you want your loop to break and carryon with your other existing variables if the process of optimisation is not met. Please refer the code below to help you address this issue.
% Surrogate Optimization Parameters
lb = [-0.34, -0.85, 0];
ub = [0.34, -0.25, 360];
A = []; b = []; Aeq = []; beq = []; intcon = [];
nvars = 3;
options = optimoptions('surrogateopt', 'PlotFcn', 'surrogateoptplot');
% Loop for optimization
while true
% Optimization
[xopt, fval, exitflag, output] = surrogateopt(@ManipulabilityFun, lb, ub, intcon, A, b, Aeq, beq, options);
% Your optimization function
load('T.mat');
xos = xopt(1);
yos = xopt(2);
rotz = xopt(3);
% Check your condition (e.g., reachability)
if Part_reach1 ~= -100
% Restart optimization by breaking the loop
break;
end
% Continue with your optimization process
% ...
% Optionally, update lb and ub based on previous solutions
% lb = ...;
% ub = ...;
end
Hope this helped,
Regards,
Anurag

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by