Finish 2011-11-09 12:00:00 UTC

solver_E will it run?

by James White

Status: Passed
Results: 77186269 (cyc: 16, node: 1764)
CPU Time: 139.29
Score: 1004610.0
Submitted at: 2011-11-04 12:11:55 UTC
Scored at: 2011-11-04 12:14:57 UTC

Current Rank: 1546th (Highest: 429th )
Based on: solver_D still don't understand (;o;) (diff)
Basis for: solver_F giddap (diff)

Comments
Please login or create a profile.
Code
% submissions/solver_E.m
% James White
function [moves, vine] = solver(board, limit)

SCORE = -Inf * ones(1,25);
VINE = cell(1,25);
MOVES = cell(1,25);

%[MOVES{1}, VINE{1}, SCORE(1)]  = initial_solver(board, limit);


A = [];

% [MOVES{2}, VINE{2}, SCORE(2)] = dfs(board,A,7);
% %check(MOVES{2},VINE{2},board,limit, SCORE(2));
% 
[MOVES{3}, VINE{3}, SCORE(3)]  = greedy(board,A,1);
% % check(MOVES{3},VINE{3},board,limit, SCORE(3));
[MOVES{4}, VINE{4}, SCORE(4)] = greedy(board,A, -1);
% % check(MOVES{4},VINE{4},board,limit, SCORE(4));


A = graph_from_board(board);

N=5;
[MOVES{N}, VINE{N}, SCORE(N)] = warnsdorff3(board,A);
%check(MOVES{N},VINE{N},board,limit, SCORE(N));

N=6;
[MOVES{N}, VINE{N}, SCORE(N)] = reverse_warnsdorff3(board,A);
%check(MOVES{N},VINE{N},board,limit, SCORE(N));


% why are these guys with A' doing better?
N=10;
[MOVES{N}, VINE{N}, SCORE(N)] = reverse_warnsdorff3(board,A');
%check(MOVES{N},VINE{N},board,limit, SCORE(N));

N=12;
[MOVES{N}, VINE{N}, SCORE(N)] = warnsdorff3(board,A');
%check(MOVES{N},VINE{N},board,limit, SCORE(N));


% 
% 
N=8;
[MOVES{N}, VINE{N}, SCORE(N)] = greedorf(board,A, 1);
% %check(MOVES{8},VINE{8},board,limit, SCORE(8));
N=9;
[MOVES{N}, VINE{N}, SCORE(N)] = greedorf(board,A', -1);
% %check(MOVES{9},VINE{9},board,limit, SCORE(9));
N=20;
[MOVES{N}, VINE{N}, SCORE(N)] = greedorf(board,A', 1);
N=21;
[MOVES{N}, VINE{N}, SCORE(N)] = greedorf(board,A, -1);

[result, vine_idx]          = max(SCORE);
%%fprintf('%i',vine_idx);
vine                   = VINE{vine_idx};
moves                  = MOVES{vine_idx};
%out = double(board);


end

%function [moves, vine, score] = initial_solver(board, limit)
%
%moves = [];
%[score,vine] = max(board(:));
%
%end





function A = graph_from_board(board)

A = sparse(numel(board),numel(board),0);
nrows = size(board,1);


for i = 1:numel(board)
    
    if i+1 <= numel(board) && mod(i,nrows) % smaller index must not be at end of a column
        A(i,i+1    ) = board(i) <= board(i+1);
    end
    
    if i > 1 && mod(i-1,nrows) % smaller index must not be at end of a column
        A(i,i-1    ) = board(i) <= board(i-1);
    end
    
    if i + nrows <= numel(board)
        A(i,i+nrows) = board(i) <= board(i+nrows);
    end
    
    if i > nrows
        A(i,i-nrows) = board(i) <= board(i-nrows);
    end
    
end


end


% function flag = isconnected(a,b,m)
% % a,b:  Absolute indices
% % m,n:  Size of board
% % flag: True if indices a and b are four connected 
% d = abs(a(:)-b(:));
% flag = (d == m) | (d == 1 & mod(min(a(:),b(:)), m));
% end
function [moves, bestvine, bestscore] = greedy(board,A, UPDOWN)
% UPDOWN = 1 to walk uphill, -1 to walk downhill

board = UPDOWN*board;
VISITED = -Inf;
moves = [];
vine = zeros(numel(board),1);
bestscore = -Inf;

%startnodes = 1:numel(board); % all

startnodes = find(board(:) == min(board(:)))'; % maybe not the best strategy

if numel(startnodes) > 50,
   startnodes = startnodes(1:floor(numel(startnodes)/50):end); 
end
nrows = size(board,1);
nsew = [ 1 -1 nrows -nrows];

original_board = board;

for i = startnodes
    board = original_board;
    vine(1) = i;
    score = board(vine(1));
    curval = board(vine(1));
    board(vine(1)) = VISITED; % mark as visited
    k = 1;
    
    
    while(1)
        %fprintf('round %i\n',k);
        %fprintf('  vine(%i) = %i\n',k, vine(k));

        hi = Inf;
        next = 0;
        for adj = vine(k) + nsew
            if adj > 0 && adj <= numel(board) && ( abs(adj - vine(k))==nrows | mod(min(vine(k),adj),nrows) ) && curval <= board(adj) 
                %[r,c]=ind2sub(size(board), adj);
                %fprintf('  adj %i (%3i,%3i) is %i \n',adj, r,c, board(adj));

                if board(adj) < hi
                    hi = board(adj);
                    next = adj;
                end
            end
        end
        
        
        if ~next
            break;
        end
        
        curval = hi;
        k = k+1;
        
        score = score + hi;
        vine(k) = next;
        board(vine(k)) = VISITED; % mark as visited
    end
    

    score = UPDOWN*score;
    
    if score > bestscore
        bestscore = score;
        bestvine = vine;
        bestlength = k;
    end
    
    
end

if UPDOWN == 1
    bestvine = bestvine(1:bestlength);
else
    bestvine = bestvine(bestlength:-1:1);
end


end



function [moves, bestvine, bestscore] = greedorf(board,A, UPDOWN, nsew)
% UPDOWN = 1 to walk uphill, -1 to walk downhill
% try to stay in current region and try to wind


board = UPDOWN*board;
%A = graph_from_board(board);

VISITED = -Inf;
moves = [];
vine = zeros(numel(board),1);
bestscore = -Inf;

%startnodes = 1:numel(board); % all

startnodes = find(board(:) == min(board(:)))'; % maybe not the best strategy

if numel(startnodes) > 50,
    startnodes = startnodes(1:floor(numel(startnodes)/50):end);
end
nrows = size(board,1);
nsew = [ 1 -1 nrows -nrows];
%nsew = [ nrows -nrows 1 -1 ];
%nsew = [  -nrows 1 nrows -1 ];
original_board = board;

for i = startnodes

    board = original_board;
    edgecount = sum(A,2); % outdegree
    
    vine(1) = i;
    score = board(vine(1));
    curval = board(vine(1));
    neighbors = find(A(:, vine(1))); % nodes pointing to vine
    edgecount(neighbors) = edgecount(neighbors)-1;

    board(vine(1)) = VISITED; % mark as visited
    k = 1;
    
    %next = 0;
    %hi = Inf;
    
    while(1)
        %fprintf('round %i\n',k);
        %fprintf('  vine(%i) = %i\n',k, vine(k));
        edge_count_lo = Inf;
        hi = Inf;
        next = 0;
        %bob = find(A(vine(k),:));
        for adj = vine(k) + nsew(randperm(4))
        %for adj = bob
            if adj > 0 && adj <= numel(board) && ( abs(adj - vine(k))==nrows | mod(min(vine(k),adj),nrows) ) && curval <= board(adj)
            %assert( any( bob == adj ) );
            %assert(adj > 0 && adj <= numel(board) && ( abs(adj - vine(k))==nrows | mod(min(vine(k),adj),nrows) ));
            %if curval <= board(adj)

                %[r,c]=ind2sub(size(board), adj);
                %fprintf('  adj %i (%3i,%3i) is %i \n',adj, r,c, board(adj));
                %assert(edgecount(adj)<4);
                if board(adj) < hi
                    hi = board(adj);
                    next = adj;
                    edge_count_lo = edgecount(adj); %edges_left(adj, board);
                else if board(adj) == hi
                        %next_outdeg = edgecount(adj);
                        %n = edges_left(adj, board);

                        %assert(n<4,'next_outdeg = %i, n = %i, vine(%i) = %i,adj = %i',next_outdeg, n,k, vine(k), adj);%%%%%%%%%%
                        %assert(next_outdeg == n, 'next_outdeg = %i, n = %i',next_outdeg, n);
                        %assert(next_outdeg<4,'next_outdeg = %i, n = %i',next_outdeg, n);%%%%%%%%%%
                        %assert(k < numel(board));
                        
                        if edgecount(adj) < edge_count_lo
                            %hi = board(adj);
                            next = adj;
                            edge_count_lo = edgecount(adj);
                        end
                        
                    end
                end
            end
        end
        
        
        if ~next
            break;
        end
        
        curval = board(next); % hi;
        k = k+1;
        
        
        score = score +  board(next); %hi;
        vine(k) = next;
        
        neighbors = find(A(:, vine(k))); % nodes pointing to vine
        edgecount(neighbors) = edgecount(neighbors)-1;
        board(vine(k)) = VISITED; % mark as visited
    end
    
    
    score = UPDOWN*score;
    
    if score > bestscore
        bestscore = score;
        bestvine = vine;
        bestlength = k;
    end
    
    
end

if UPDOWN == 1
    bestvine = bestvine(1:bestlength);
else
    bestvine = bestvine(bestlength:-1:1);
end


end


% function n = edges_left(node, board)
% n = 0;
% nrows = size(board,1);
% nsew = [ 1 -1 nrows -nrows];
% 
% % counting issues if nrows = 1
% for adj = node + nsew
%     if adj > 0 && adj <= numel(board) && ( abs(adj - node)==nrows | mod(min(node,adj),nrows) ) && board(node) <= board(adj)
%         n = n+1;
%     end
% end
% 
% if nrows == 1 % counting issues if nrows = 1
%     n = n/2;
% end
% 
% end
% 


function [moves, bestvine, bestscore] = warnsdorff3(board,A)
% faster

%A = graph_from_board(board);


moves = [];
vine = zeros(numel(board),1);
bestscore = 0;

%startnodes = 1:numel(board); % all
startnodes = find(board(:) == min(board(:)))';
if numel(startnodes) > 50,
    startnodes = startnodes(1:floor(numel(startnodes)/50):end);
end
nrows = size(board,1);
nsew = [ 1 -1 nrows -nrows];

original_board = board;

for i = startnodes
    board = original_board;
    edgecount = sum(A,2); % outdegree
    vine(1) = i;
    score = board(vine(1));
    curval = board(vine(1));
    neighbors = find(A(:, vine(1))); % nodes pointing to vine
    edgecount(neighbors) = edgecount(neighbors)-1;
    board(vine(1)) = -Inf; % mark as visited
    k = 1;
    
    
    while(1)
        %fprintf('round %i\n',k);
        %fprintf('  vine(%i) = %i\n',k, vine(k));
        %hi = 0;
        lo = Inf;
        next = 0;
        for adj = vine(k) + nsew
            if adj > 0 && adj <= numel(board) && ( abs(adj - vine(k))==nrows | mod(min(vine(k),adj),nrows) ) && curval <= board(adj)
                %[r,c]=ind2sub(size(board), adj);
                %fprintf('  adj %i (%3i,%3i) is %i \n',adj, r,c, board(adj));
                
                %if board(adj) > hi %&& A(vine(d),next)
                %    hi = board(adj);
                %    next = adj;
                %end
                
                %n = edges_left(adj, board);  assert(edgecount(adj) == n); assert(n<4);%%%%%%%%%%
                n = edgecount(adj); % assert(n<4);
                if  n < lo
                    lo = n; %board(adj); 
                    next = adj;
                end
            end
        end
        
        
        if ~next
            break;
        end
        
        curval = board(next);
        k = k+1;
        
        score = score + board(next);
        vine(k) = next;
        
        neighbors = find(A(:, vine(k))); % nodes pointing to vine
        edgecount(neighbors) = edgecount(neighbors) -1;
       
        board(vine(k)) = -Inf; % mark as visited
    end
    
    
    
    if score > bestscore
        bestscore = score;
        bestvine = vine;
        bestlength = k;
    end
    
    
end

bestvine = bestvine(1:bestlength);

end

function [moves, bestvine, bestscore] = reverse_warnsdorff3(board,A)
% faster

BLOCKED = Inf;
%A = graph_from_board(board);


moves = [];
vine = zeros(numel(board),1);
bestscore = -Inf;

%startnodes = 1:numel(board); % all
startnodes = find(board(:) == max(board(:)))';
if numel(startnodes) > 50,
    startnodes = startnodes(1:floor(numel(startnodes)/50):end);
end
nrows = size(board,1);
nsew = [ 1 -1 nrows -nrows];

original_board = board;

for i = startnodes
    board = original_board;
    edgecount = sum(A,1); % indegree
    vine(1) = i;
    score = board(vine(1));
    curval = board(vine(1));
    neighbors = find(A(vine(1),:)); % nodes pointing to vine (reversed)
    edgecount(neighbors) = edgecount(neighbors)-1;
    board(vine(1)) = BLOCKED; % mark as visited
    k = 1;
    
    
    while(1)
        %fprintf('round %i\n',k);
        %fprintf('  vine(%i) = %i\n',k, vine(k));
        %hi = 0;
        lo = Inf;
        %lo = 0;
        next = 0;
        for adj = vine(k) + nsew
            if adj > 0 && adj <= numel(board) && ( abs(adj - vine(k))==nrows | mod(min(vine(k),adj),nrows) ) && curval >= board(adj)
                
                %if board(adj) > hi %&& A(vine(d),next)
                %    hi = board(adj);
                %    next = adj;
                %end
                
                %n = edges_left(adj, board);  assert(edgecount(adj) == n); %%%%%%%%%%
                n = edgecount(adj); % assert(n<4);
                if  n < lo
                    lo = n;
                    next = adj;
                    %disp('bob')
                end
            end
        end
        
        
        if ~next
            break;
        end
        
        curval = board(next);
        k = k+1;
        
        score = score + board(next);
        vine(k) = next;
        
        neighbors = find(A(vine(k),:)); % nodes pointing to vine
        edgecount(neighbors) = edgecount(neighbors) -1;
       
        board(vine(k)) = BLOCKED; % mark as visited
    end
    
    
    
    if score > bestscore
        bestscore = score;
        bestvine = vine;
        bestlength = k;
    end
    
    
end

bestvine = bestvine(bestlength:-1:1);

end