Code covered by the BSD License  

Highlights from
MATLAB Contest - Molecule

image thumbnail

MATLAB Contest - Molecule

by

 

16 May 2002 (Updated )

All the files needed to develop and score an entry for the fifth MATLAB Programming Contest.

runcontest(drawboard)
function [results,message] = runcontest(drawboard)

% Copyright 2002 The MathWorks, Inc.
    
if nargin == 0
    drawboard = 0;
end

% Load the testsuite which contains the coordinates for the centers 
%  of each circle in the m-by-n-by-2 matrix gm where m is the number 
%  of boards, and n is the number of points on a board.
    load('testsuite');

for k = 1:length(t),
    bx = t(k).bx;
    kd = t(k).kd;
    
    npts = size(kd,1);
    
    % Run the submission for current game board
    
    time0 = cputime;
    xy = solver(kd,bx);
    timeElapsed = cputime-time0;
    
    % Check the solution
    
    % Check for the correct number of points
    if size(xy,1) ~= npts
        error('incorrect number of points returned')
    end
    if size(xy,2) ~= 2
        error('should be two columns for xy')
    end
    
    % Check against box bounds
    if min(xy(:,1)) < bx(1)
        error('x too low')
    end
    if min(xy(:,2)) < bx(3)
        error('y too low')
    end
    if max(xy(:,1)) > bx(2)
        error('x too high')
    end
    if max(xy(:,2)) > bx(4)
        error('y too high')
    end

    % Calculate distance matrix
    x=xy(:,1);
    y=xy(:,2);
    [XY1,XY2] = meshgrid(x,y);
    dist = sqrt((XY1 - XY1').^2 + (XY2 - XY2').^2); 
    
    % Calculate strain matrix
    strainMatrix = dist - kd;
    strainMatrix(kd < 0) = 0;
    
    % plot
    if drawboard
        cla
        rectangle('Position',[bx(1) bx(3) bx(2)-bx(1) bx(4)-bx(3)]);
        % plot the compressive strains in blue
        compstrain = strainMatrix;
        compstrain(strainMatrix>=0) = 0;
        compstrain(kd < 0) = 0;
        gplot(compstrain,xy,'b')
        hold on
        % plot the expanding strains in red
        compstrain = strainMatrix;
        compstrain(strainMatrix<=0) = 0;
        compstrain(kd < 0) = 0;
        gplot(compstrain,xy,'r')
        % plot the zero strains in black
        compstrain(strainMatrix~=0) = 1;
        compstrain(kd < 0) = 1;
        compstrain=1-compstrain;
        % plot in green instead!
        gplot(compstrain,xy,'g')
        plot(xy(:,1),xy(:,2),'.')
        hold off
        for i = 1:size(xy,1)
            text(xy(i,1),xy(i,2),[' ',num2str(i)])
        end
        axis off
        set(gcf,'Color','white')
        pause
    end
    
    results(k,1) = sum(abs(strainMatrix(:)));
    results(k,2) = timeElapsed;
end
message=sprintf('Average strain = %0.03f', sum(results(:,1))/size(results,1));

Contact us