Winner Ave (More tricks)

Finish 2003-04-08 00:00:00 UTC

RoseJoy6c

by Larry

Status: Failed
Results:

Based on: RoseJoy6a (diff)
Basis for: RoseJoy8a (diff)

Comments
Please login or create a profile.
Code
function c = solver(a)

global FREIGHT GAS
global BEST_ROUTE BEST_SCORE
global N D ROUTE SCORE COMPLETED
global AVAILABLE_FREIGHT
global STATION_VALUE

FREIGHT = a(:,3);
GAS     = a(:,4);
COMPLETED = 1;N = 1;D = [];
Visited = zeros(1,length(a));
STATION_VALUE = FREIGHT + GAS;
Visited(find(STATION_VALUE==0)) = 1;
Visited(1) = 0;
HomeCoord = a(1,1:2);
Freight   = a(1,3);
Gas       = a(1,4);
AVAILABLE_FREIGHT = sum(a(:,3));
availableGas     = sum(a(:,4));
x = a(:,1);
y = a(:,2);
p = x + y*1i;
[X,Y] = meshgrid(p,p);
D = sqrt(abs((X-Y).^2));
N = N+1;
BEST_ROUTE = [1 1];
BEST_SCORE = AVAILABLE_FREIGHT - Freight + Gas;
trip(1,1,Visited,Freight,Gas,Gas)
c = BEST_ROUTE;

function trip(route, station, visited, collected_freight, ...
              collected_gas, gas)

global FREIGHT GAS
global BEST_ROUTE BEST_SCORE
global N D COMPLETED
global AVAILABLE_FREIGHT
global STATION_VALUE

d = D(station,:);
available = ~visited;
available(1, station) = 0;
dltgas = (d<=gas);
%xxx=1;
%if length(available)<length(dltgas)
%    xxx(0)
%end
reachable = find((dltgas(:)) .* (available(:)));

if (COMPLETED > 5) | (N > 100)
    return
end

if isempty(reachable),
    N = N + 1;
    return
else
    mdist = median(D(station,:));
    kg = 2*mdist/gas;
    kf = 1/kg;
    STATION_VALUE = kf*FREIGHT + kg*GAS;
    
    [station_value,station_order] = sort(-STATION_VALUE([reachable]));
    for n = station_order'
        next_station = reachable(n);
        dist = D(station, next_station);
        if next_station==1
            score = AVAILABLE_FREIGHT-collected_freight+collected_gas;
            if score < BEST_SCORE
                BEST_SCORE = score;
                BEST_ROUTE = [route 1];
            end
            N = N + 1;
            COMPLETED = COMPLETED + 1;
            return
        else
            visited(next_station) = 1;
            trip([route next_station], next_station, visited, ...
                collected_freight + FREIGHT(next_station), ...
                collected_gas + GAS(next_station), ...
                gas + GAS(next_station)-dist)
        end
    end
end