function [thrustRow, thrustCol] = solver(chart, aIndex, bIndex, maxThrottle)
solverScore=inf;
for kk=0:0.1:2.5
[thrustRow1, thrustCol1,sc1] = subsolver(chart, aIndex, bIndex, maxThrottle,kk);
if sc1<solverScore
solverScore=sc1;
thrustRow=thrustRow1;
thrustCol=thrustCol1;
end
end
end
function [thrustRow, thrustCol,sc] = subsolver(chart, aIndex, bIndex, maxThrottle,key)
%maxThrottle
[aRow, aCol] = ind2sub(size(chart(:,:,1)),aIndex);
[bRow, bCol] = ind2sub(size(chart(:,:,1)),bIndex);
currentRow=aRow;
currentCol=aCol;
vR=0;
vC=0;
thrustRow = [];
thrustCol = [];
targetRow=bRow;
targetCol=bCol;
currentRows(1)=currentRow;
currentCols(1)=currentCol;
for s=1:1000
br=0;
rowLim=0;
colLim=0;
if currentRow==bRow && currentCol==bCol
targetRow=aRow;
targetCol=aCol;
end
if currentRow==aRow && currentCol==aCol && targetRow==aRow && targetCol==aCol
break
end
dvRw=chart(currentRow,currentCol,1);
dvCw=chart(currentRow,currentCol,2);
DR=targetRow-currentRow;
DC=targetCol-currentCol;
vRplusW=vR+dvRw;
vCplusW=vC+dvCw;
dvRm=(sign(DR)-sign(vRplusW))*(abs(vRplusW)+1)/2;
dvCm=(sign(DC)-sign(vCplusW))*(abs(vCplusW)+1)/2;
if sign(DR)==sign(vRplusW) && abs(DR)<abs(vRplusW)
dvRm=-(abs(vRplusW)-abs(DR))*sign(vRplusW);
end
if sign(DC)==sign(vCplusW) && abs(DC)<abs(vCplusW)
dvCm=-(abs(vCplusW)-abs(DC))*sign(vCplusW);
end
if key>0
if abs(vRplusW)>maxThrottle/key
dvRm=-round((abs(vRplusW)-maxThrottle/key)*sign(vRplusW));
end
if abs(vCplusW)>maxThrottle/key
dvCm=-round((abs(vCplusW)-maxThrottle/key)*sign(vCplusW));
end
end
if sign(DR)==0
dvRm=-vRplusW;
end
if sign(DC)==0
dvCm=-vCplusW;
end
if sign(vRplusW)==0
dvRm=sign(DR);
end
if sign(vCplusW)==0
dvCm=sign(DC);
end
%**************
if currentRow+vRplusW+dvRm<1
dvRm=1-currentRow-vRplusW;
if abs(dvRm)>maxThrottle
break
end
rowLim=1;
end
if currentCol+vCplusW+dvCm<1
dvCm=1-currentCol-vCplusW;
if abs(dvCm)>maxThrottle
break
end
colLim=1;
end
if currentRow+vRplusW+dvRm>size(chart(:,:,1),1)
dvRm=size(chart(:,:,1),1)-currentRow-vRplusW;
if abs(dvRm)>maxThrottle
break
end
rowLim=1;
end
if currentCol+vCplusW+dvCm>size(chart(:,:,1),2)
dvCm=size(chart(:,:,1),2)-currentCol-vCplusW;
if abs(dvCm)>maxThrottle
break
end
colLim=1;
end
for tt=1:10
if (abs(dvRm) + abs(dvCm))>maxThrottle
if rowLim==1 && colLim==1
br=1;
break
end
if rowLim==1
dvCm=max((maxThrottle-abs(dvRm)),0)*sign(dvCm);
end
if colLim==1
dvRm=max((maxThrottle-abs(dvCm)),0)*sign(dvCm);
end
end
if (abs(dvRm) + abs(dvCm))>maxThrottle
if rowLim==0 && colLim==0
m=(abs(dvRm) + abs(dvCm));
dvCm=fix(rand(1)*dvCm*(maxThrottle/m));
dvRm=fix(rand(1)*dvRm*(maxThrottle/m));
end
end
rowLim=0;
colLim=0;
if currentRow+vRplusW+dvRm<1
dvRm=1-currentRow-vRplusW;
if abs(dvRm)>maxThrottle
br=1;
break
end
rowLim=1;
end
if currentCol+vCplusW+dvCm<1
dvCm=1-currentCol-vCplusW;
if abs(dvCm)>maxThrottle
br=1;
break
end
colLim=1;
end
if currentRow+vRplusW+dvRm>size(chart(:,:,1),1)
dvRm=size(chart(:,:,1),1)-currentRow-vRplusW;
if abs(dvRm)>maxThrottle
br=1;
break
end
rowLim=1;
end
if currentCol+vCplusW+dvCm>size(chart(:,:,1),2)
dvCm=size(chart(:,:,1),2)-currentCol-vCplusW;
if abs(dvCm)>maxThrottle
br=1;
break
end
colLim=1;
end
if rowLim==0 && colLim==0 && (abs(dvRm) && abs(dvCm))<=maxThrottle
break
end
end
if tt==10 || br==1
break
end
vR=vRplusW+dvRm;
vC=vCplusW+dvCm;
currentRow=currentRow+vR;
currentCol=currentCol+vC;
thrustRow(end+1) = dvRm;
thrustCol(end+1) = dvCm;
currentRows(end+1)=currentRow;
currentCols(end+1)=currentCol;
end
scD=(currentRows-bRow).^2+(currentCols-bCol).^2;
for j=1:length(scD)
scD(j)=min(scD(1:j));
end
scL=cumsum([0 abs(thrustRow)])+cumsum([0 abs(thrustCol)])+(currentRows-aRow).^2+(currentCols-aCol).^2 +scD;
[sc mi]=min(scL);
if mi>1
thrustRow= thrustRow(1:mi-1);
thrustCol= thrustCol(1:mi-1);
else
thrustRow= [];
thrustCol= [];
end
end
|