Avoid the Optimization crash

30 views (last 30 days)
Chun Wai KO
Chun Wai KO on 18 Mar 2022
Commented: Chun Wai KO on 20 Mar 2022
Dear,
I am trying to do a tracking system
y1 and y2 are my prestored measurement data
v1q and v2q are my real time measurement data (assume)
i am trying to use optimization and interpolation together to solve this problem.
The solution is fine. In this case, it can compute the position is [4,3] which is correct.
The problem is when the program run for a while (like 1min), the system starts not responding.
I suspect that it might be the issue of computational power, so i also install the parallel computing toolbox, but the speed is still not fast and easy to be collasped.
I did some research showing that running program in gpa can increase the speed, but seems optimization is not allowed for gpu
Is there any way that i can avoid the crash?
Or can anyone tell me why it happens?
Thank you very much!!
function [out]= Untitled3(v1q,v2q,currentPandO)
%vertical coil
y1 = [3.24272914125766,2.83593179463819,2.44752064552654,2.05924203430804;
3.56186924377538,2.99624019270884,2.55850166648457,2.25064722251426;
3.57300859267674,2.90086878942227,2.48641813065078,2.19590225245865;
2.90037026793495,2.46229273094783,2.24971289753428,2.07094182706089];
%horizontal coil
y2 = [1.47213024926091,1.86376922289994,1.96744782910615,1.98366932726146;
1.65117827228894,2.08559296236809,2.31269691938603,2.28466504171977;
2.00030563764438,2.54342676501796,2.71125667257210,2.66662804271614;
2.84018880472288,3.15941792534719,3.21539885401633,3.12483330616169];
x = currentPandO(1);
y = currentPandO(2);
out = abs(interp2(y1,x,y) - v1q) + abs(interp2(y2,x,y) - v2q) ;
end
options = optimoptions(@patternsearch,'InitialMeshSize',1,'MaxFunctionEvaluations',1000,'MaxIterations',1000,'TimeLimit',0.1,'MeshTolerance',1.0000e-16,'StepTolerance',1e-16','UseParallel', true, 'UseCompletePoll', true, 'UseVectorized', false );
v1q = 2.19590225245865;
v2q = 2.66662804271614;
while(1)
disp(v1q);
disp(v2q);
solve = @(currentPandO)Untitled3(v1q,v2q,currentPandO);
A = [];
b = [];
Aeq = [];
beq = [];
lb = [1,1];
ub = [4,4];
nonlcon = [];
solution = patternsearch(solve, [1,1],A,b,Aeq,beq,lb,ub,nonlcon,options);
axes('xlim',[1 5 ], 'ylim',[1 5], 'zlim', [1 5])
view(3)
grid on
hold on
xlabel('x')
ylabel('y')
zlabel('z')
disp(v1q);
disp(v2q);
h2 = plot3(solution(1),solution(2),1,'.','color','red','MarkerSize',20);
pause(0.1)
delete(h2)
end
  7 Comments
Walter Roberson
Walter Roberson on 18 Mar 2022
What variables are you optimizing over?
Chun Wai KO
Chun Wai KO on 18 Mar 2022
changing the gather(out) to out = gather(out)
now the program can run
i found that it performs well for first 20 seconds, but then it will be slowing down.
even if i stop the program, and re-run the program, the speed cannot be as fast as it was.
i need to close the Matlab and wait for a while (for example 1min) (like cooling down)
then open the matlab and run the program
then it can run fast in 20 seconds again
For my optimiation, the solve is the objective function and my initial guess is [1,1] for x and y
so basically it is trying to find the (x and y) to minimize the out
out = abs(interp2(y1,x,y) - v1q) + abs(interp2(y2,x,y) - v2q) ;

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 19 Mar 2022
This is a proof of concept, that shows if you are willing to modify the cost from sum of abs() to sum of squares, then you can calculate the optimal position.
This particular version of the example deals with the possibility that x and y are both between 1 and 2, and does the bilinear interpolation mathematically, and then optimizes the query values.
The overall calculation would involve calculating for the nine 2 x 2 sub-arrays of y1 and y2 that are induced by taking the integer part of x and y to extract the relevant section of the matrices to be interpolated over.
To phrase that a different way: you can pre-calculate the cubic roots and square root formulas that would apply for each potential x and y combination according to what would be interpolated over; and then having done that pre-calculation, then given particular numeric x and y values, you would floor() those and use those to index the matrix of solutions -- a lookup table rather than an optimization at run-time.
It might be worth exploring to see whether only the first of the 5 potential solutions will be real-valued as hinted at when I substitute in random v1q and v2q, if you know the range of v1q and v2q.
y1 = [3.24272914125766,2.83593179463819,2.44752064552654,2.05924203430804;
3.56186924377538,2.99624019270884,2.55850166648457,2.25064722251426;
3.57300859267674,2.90086878942227,2.48641813065078,2.19590225245865;
2.90037026793495,2.46229273094783,2.24971289753428,2.07094182706089];
%horizontal coil
y2 = [1.47213024926091,1.86376922289994,1.96744782910615,1.98366932726146;
1.65117827228894,2.08559296236809,2.31269691938603,2.28466504171977;
2.00030563764438,2.54342676501796,2.71125667257210,2.66662804271614;
2.84018880472288,3.15941792534719,3.21539885401633,3.12483330616169];
syms x y v1q v2q real
assume(x >= 1 & y >=1)
weighted1 = ([2-x,x-1]+[0:2;0:-1:-2].') * y1(1:2,1:2).' * ([2-y;y-1]+[0:2;0:-1:-2]);
weighted2 = ([2-x,x-1]+[0:2;0:-1:-2].') * y2(1:2,1:2).' * ([2-y;y-1]+[0:2;0:-1:-2]);
out = (weighted1(1,1) - v1q).^2 + abs(weighted2(1,1) - v2q).^2
out = 
dx = simplify(diff(out, x))
dx = 
partialx = simplify(solve(dx, x))
Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
partialx = 
outx = subs(out, x, partialx)
outx = 
dy = simplify(diff(outx,y))
dy = 
bestyinfo = solve(dy, y, 'returnconditions', true)
bestyinfo = struct with fields:
y: [5×1 sym] parameters: [1×0 sym] conditions: [5×1 sym]
besty = bestyinfo.y;
bestx = simplify(subs(partialx, y, besty), 'steps', 20);
vpa(bestx)
ans = 
vpa(besty)
ans = 
vpa(bestyinfo.conditions)
ans = 
V1Q = rand() * 3 + 1
V1Q = 1.0523
V2Q = rand() * 3 + 1
V2Q = 2.3850
vpa(subs(bestx, {v1q, v2q}, {V1Q, V2Q}))
ans = 
vpa(subs(besty, {v1q, v2q}, {V1Q, V2Q}))
ans = 
  13 Comments
Chun Wai KO
Chun Wai KO on 20 Mar 2022
I am actually interested in your look up table method.
Because I read a paper about tracking system and they used look up table to solve the problem.
They used LabView to generate the look up table instead of Matlab.
I am just curious that how to implement a look up table or inverse interpolation in matlab (1D,2D,3D) so that I can find x and y (array index) by matching the v1q with y1 and v2q with y2
Chun Wai KO
Chun Wai KO on 20 Mar 2022
What I want to do is like “contour” function Find the x and y coordinate from f(x,y) (with interpolation)
But contour is just for 2D I need a 3D model.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!