Finish 2010-11-17 12:00:00 UTC

Bang-Bang Sailor 1

by Andre Fioravanti

Status: Passed
Results: 200816 (cyc: 9, node: 526)
CPU Time: 41.536
Score: 40164.2
Submitted at: 2010-11-10 22:39:08 UTC
Scored at: 2010-11-10 22:40:03 UTC

Current Rank: 2430th (Highest: 1st )
Basis for: Bang-Bang Sailor 2 (diff)

Comments
Please login or create a profile.
Code
function [thrustRow, thrustCol] = solver(chart, aIndex, bIndex, maxThrottle)
    
    thrustRow = [];
    thrustCol = [];
    best_score = inf;
    
    vec_thrustRow = zeros(1000,1);
    vec_thrustCol = zeros(1000,1);
    score = runsolution(vec_thrustRow, vec_thrustCol, chart, aIndex, bIndex);
    [y,idx_y] = min(score);
    if(y < best_score)
        thrustRow = vec_thrustRow(1:idx_y);
        thrustCol = vec_thrustCol(1:idx_y);
        best_score = y;
    end
    
    max_dep = 1;
    val_best = zeros(max_dep,3);
    for iter=1:max_dep
        best_score_ant = best_score;
        for i=1:length(thrustRow)
            if(any(i == val_best(:,1)))
                continue;
            end
            for wx = -maxThrottle:maxThrottle
                delta_w = maxThrottle - abs(wx);
                vec_thrustRow(i) = wx;
                for wy = -delta_w:delta_w;
                    vec_thrustCol(i) = wy;
                    score = runsolution(vec_thrustRow, vec_thrustCol, chart, aIndex, bIndex);
                    [y,idx_y] = min(score);
                    if(y < best_score)
                        thrustRow = vec_thrustRow(1:idx_y);
                        thrustCol = vec_thrustCol(1:idx_y);
                        best_score = y;
                        val_best(iter,:) = [i wx wy];
                    end
                end
            end
            vec_thrustRow(i) = 0;
            vec_thrustCol(i) = 0;
        end
        if(best_score_ant == best_score)
            return;
        end
        vec_thrustRow(val_best(iter,1)) = val_best(iter,2);
        vec_thrustCol(val_best(iter,1)) = val_best(iter,3);
    end
end

function score = runsolution(thrustRow, thrustCol, chart, aIndex, bIndex)
    % RUNSOLUTION Simulates the navigation trajectory given the winds and the
    % motor thrust.

    rowWind = chart(:,:,1);
    colWind = chart(:,:,2);
    [nR,nC] = size(rowWind);
    [AR,AC] = ind2sub([nR,nC],aIndex);
    [BR,BC] = ind2sub([nR,nC],bIndex);

    % Initialize variables at start point (A)
    fR = AR; fC =AC;
    fvR = 0; fvC = 0;
    dB = (fR-BR)^2 + (fC-BC)^2;
    score = zeros(1,numel(thrustRow));
    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
            if(i == 1)
                score = dB;
            else
                score = score(1:i-1);
            end
            break % out of bounds
        end
        fR = iR;
        fC = iC;
        fvR = ivR;
        fvC = ivC;
        % Verify if this is the closest point to B
        if ((fR-BR)^2 + (fC-BC)^2) < dB
             dB = (fR-BR)^2 + (fC-BC)^2;    
        end
        dA = (fR-AR)^2 + (fC-AC)^2; % Final distance to A
        score(i) = dB + dA + sum(abs(thrustRow)) + sum(abs(thrustCol));
    end
end