function [thrustRow, thrustCol] = solver(chart, aIndex, bIndex, maxThrottle)
rowWind = chart(:,:,1);
colWind = chart(:,:,2);
rw = mean(rowWind(:));
cw = mean(colWind(:));
[nR,nC] = size(rowWind);
[AR,AC] = ind2sub([nR,nC],aIndex);
[BR,BC] = ind2sub([nR,nC],bIndex);
rd=BR-AR; cd=BC-AC;
dist = abs(rd)+abs(cd);
thrustRow = [1 zeros(1,rd-1) -1 zeros(1,cd-1) -1 zeros(1,rd) 1 zeros(1,cd)] * sign(BR-AR);
thrustCol = [zeros(1,rd-1) 1 zeros(1,cd-1) -1 zeros(1,rd) -1 zeros(1,cd) 1] * sign(BC-AC);
[dA,dB, out] = runsolution(thrustRow, thrustCol, chart, nR,nC,AR,AC,BR,BC);
solverScore = dB + dA + sum(abs(thrustRow)) + sum(abs(thrustCol));
dsc =1e6;
%L = 2*dist;
L = numel(thrustRow);
for k = 1:10000
tR = [round(maxThrottle*(rand(1,L-1)-0.5))]; %+ thrustRow;
tC = [round(maxThrottle*(rand(1,L-1)-0.5))]; %+ thrustCol;
fR = AR; fC =AC;
fvR = 0; fvC = 0;
dB = (fR-BR)^2 + (fC-BC)^2;
for i = 1:numel(tR)
ivR = fvR + tR(i) + rowWind(fR,fC);
ivC = fvC + tC(i) + colWind(fR,fC);
iR = fR + ivR;
iC = fC + ivC;
if iR>nR || iR<1 || iC>nC || iC<1
tR = tR(1:i-1);
tC = tC(1:i-1);
break
end
fR = iR;
fC = iC;
fvR = ivR;
fvC = ivC;
if ((fR-BR)^2 + (fC-BC)^2) < dB
dB = (fR-BR)^2 + (fC-BC)^2;
end
end
dA = (fR-AR)^2 + (fC-AC)^2;
Sc = dB + dA + sum(abs(tR)) + sum(abs(tC));
if Sc < solverScore
thrustRow = tR;
thrustCol = tC;
% dsc = solverScore-Sc;
solverScore = Sc;
end
end
end
function [dA, dB, i] = runsolution(thrustRow, thrustCol, chart, nR,nC,AR,AC,BR,BC)
rowWind = chart(:,:,1);
colWind = chart(:,:,2);
fR = AR; fC =AC;
fvR = 0; fvC = 0;
dB = (fR-BR)^2 + (fC-BC)^2;
for i = 1:numel(thrustRow)
ivR = fvR + thrustRow(i) + rowWind(fR,fC);
ivC = fvC + thrustCol(i) + colWind(fR,fC);
iR = fR + ivR;
iC = fC + ivC;
if iR>nR || iR<1 || iC>nC || iC<1
break
end
fR = iR;
fC = iC;
fvR = ivR;
fvC = ivC;
if ((fR-BR)^2 + (fC-BC)^2) < dB
dB = (fR-BR)^2 + (fC-BC)^2;
end
end
dA = (fR-AR)^2 + (fC-AC)^2;
end
|