Code covered by the BSD License  

Highlights from
MATLAB Contest - Knots

image thumbnail

MATLAB Contest - Knots

by

 

31 Oct 2012 (Updated )

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

runcontest(flagVisualize,whichPuzzles)
function [message,result,solverTime] = runcontest(flagVisualize,whichPuzzles)
%RUNCONTEST Test an entry.
%   [MESSAGE,RESULTS,TIME] = RUNCONTEST runs the file solver.m against all
%   the problems defined in testsuite_sample.mat. MESSAGE returns a summary
%   of the testing, RESULTS measures how well the entry solved the problem,
%   and TIME measures the time the entry took to compute the answer.
%
%   RUNCONTEST(true) graphically visualize the results.
%
%   RUNCONTEST(true/false, whichPuzzles) runs the file solver.m
%   against the problems enumerated in the vector whichPuzzles as defined in
%   the testsuite.

% Copyright 2012 The MathWorks, Inc.

% The MATLAB Contest Team
% November 2012

testSuiteFile = 'testsuite_sample.mat';
tests = load(testSuiteFile,'testsuite');

% Argument parsing.
if nargin < 2
    whichPuzzles = 1:numel(tests.testsuite); % do all puzzles in test suite
end
if nargin < 1
    flagVisualize = false;  % no visualization
end

numPuzzles = numel(whichPuzzles);
solverTime = zeros(numPuzzles,1);
solverScore = zeros(numPuzzles,1);

if flagVisualize
    handles = setUpFigure;
end

for n = 1:numPuzzles
    [a, xyIn, wts] = getPuzzle(tests.testsuite, whichPuzzles(n));

    time0 = cputime;
    xyOut = solver(a, xyIn, wts);
    solverTime(n) = cputime - time0;

    [solverScore(n), nKnots] = grade(xyOut, a, xyIn, wts);

    % Print a report to the Command Window
    if flagVisualize
        fprintf('\n  Puzzle: %d\n  Number of Knots: %d\n  Score: %d\n',...
            whichPuzzles(n), nKnots, solverScore(n));
        if ishandle(handles.axes)
            handles = getappdata(handles.f,'handles');
            visualizeSolution(handles, xyIn, xyOut, a, whichPuzzles(n));
        end
    end
end

% Report results.
result = sum(solverScore);
resultTime = sum(solverTime);
message = sprintf('results: %.2f\ntime: %.2f', result, resultTime);

end

function [a, xyIn, wts] = getPuzzle(testsuite,k)
% GETBOARD Gets the k-th problem from the loaded testsuite

k = round(min(max(1,k),numel(testsuite)));
a = testsuite(k).a;
xyIn = testsuite(k).xyIn;
wts = testsuite(k).wts;

end



function handles = setUpFigure
handles.f = figure();
handles.axes = axes('Parent',handles.f,...
    'Position',[0.3 0.1 0.65 0.8],...
    'XTick',[],'YTick',[],...
    'XLimMode','manual','YLimMode','manual');
axis equal
handles.nextButton = uicontrol('Parent',handles.f,...
    'String','Next Puzzle',...
    'Style','pushbutton',...
    'Units','normalized',...
    'Position',[0.05 0.8 0.2 0.1],...
    'Callback',@(s,e) uiresume(handles.f));
handles.stopButton = uicontrol('Parent',handles.f,...
    'String','Close Figure',...
    'Style','pushbutton',...
    'Units','normalized',...
    'Position',[0.05 0.7 0.2 0.1],...
    'Callback',@(s,e) myclose(handles.f));
handles.pntsToggle = uicontrol('Parent',handles.f,...
    'String','Grid Points',...
    'Style','togglebutton',...
    'Units','normalized',...
    'Position',[0.05 0.55 0.2 0.1],...
    'Callback',@pntsToggleCallback);
handles.origToggle = uicontrol('Parent',handles.f,...
    'String','Original',...
    'Style','togglebutton',...
    'Units','normalized',...
    'Position',[0.05 0.45 0.2 0.1],...
    'Callback',@origToggleCallback);
handles.solnToggle = uicontrol('Parent',handles.f,...
    'String','Solution',...
    'Style','togglebutton',...
    'Units','normalized',...
    'Value',1,...
    'Position',[0.05 0.35 0.2 0.1],...
    'Callback',@solnToggleCallback);
handles.animButton = uicontrol('Parent',handles.f,...
    'String','ANIMATE',...
    'Style','pushbutton',...
    'Units','normalized',...
    'Position',[0.05 0.1 0.2 0.2],...
    'Callback',@animCallback);
handles.origPlot = [];
handles.solnPlot = [];
handles.gridPlot = [];
setappdata(handles.f,'xyIn',[])
setappdata(handles.f,'xyOut',[])
setappdata(handles.f,'A',[])
setappdata(handles.f,'handles',handles)

end

function visualizeSolution(handles, xyIn, xyOut, a, n)

figure(handles.f)
axes(handles.axes)
cla
handles.origPlot = [];
handles.solnPlot = [];
handles.gridPlot = [];
title(sprintf('Puzzle Number %d',n))

setappdata(handles.f,'xyIn',xyIn)
setappdata(handles.f,'xyOut',xyOut)
setappdata(handles.f,'A',a)

% Set axes limits
minX = min([xyIn(:,1);xyOut(:,1)]) - 1;
maxX = max([xyIn(:,1);xyOut(:,1)]) + 1;
minY = min([xyIn(:,2);xyOut(:,2)]) - 1;
maxY = max([xyIn(:,2);xyOut(:,2)]) + 1;
set(handles.axes,'XLim',[minX maxX],'YLim',[minY maxY])
setappdata(handles.f,'handles',handles)

% Run callbacks
pntsToggleCallback(handles.pntsToggle)
origToggleCallback(handles.origToggle)
solnToggleCallback(handles.solnToggle)

uiwait(handles.f)
end

function myclose(f)
close(f)
drawnow
end

function pntsToggleCallback(s,e)
handles = getappdata(get(s,'parent'),'handles');
if get(handles.pntsToggle,'Value') % Turn on grid points
    xlim = get(handles.axes,'Xlim');
    ylim = get(handles.axes,'Ylim');

    [xgrid,ygrid] = meshgrid(xlim(1):xlim(2),ylim(1):ylim(2));
    handles.gridPlot = line(xgrid(:),ygrid(:),'Color','k','Marker','.','MarkerSize',1,'LineStyle','none');
elseif ishandle(handles.gridPlot) % Delete existing grid
    delete(handles.gridPlot)
    handles.gridPlot = [];
end
setappdata(handles.f,'handles',handles)
end

function origToggleCallback(s,e)
handles = getappdata(get(s,'parent'),'handles');
if ishandle(handles.origPlot) % Delete any existing origPlot
    delete(handles.origPlot)
    handles.origPlot = [];
end
if get(handles.origToggle,'Value') % Turn on original puzzle
    XY = getappdata(handles.f,'xyIn');
    A = getappdata(handles.f,'A');
    [X,Y] = gplot(A,XY);
    handles.origPlot = line(X,Y,'Color','r','Marker','o');
end
setappdata(handles.f,'handles',handles)
end

function solnToggleCallback(s,e)
handles = getappdata(get(s,'parent'),'handles');
if ishandle(handles.solnPlot) % Delete any existing solnPlot
    delete(handles.solnPlot)
    handles.solnPlot = [];
end
if get(handles.solnToggle,'Value') % Turn on original puzzle
    XY = getappdata(handles.f,'xyOut');
    A = getappdata(handles.f,'A');
    [X,Y] = gplot(A,XY);
    handles.solnPlot = line(X,Y,'Color','b','Marker','o');
end
setappdata(handles.f,'handles',handles)
end

function animCallback(s,e)
handles = getappdata(get(s,'parent'),'handles');

% Display original for 1 second and then delete
set(handles.solnToggle,'Value',0)
solnToggleCallback(handles.solnToggle)
set(handles.origToggle,'Value',1)
origToggleCallback(handles.origToggle)
pause(1)
set(handles.origToggle,'Value',0)
origToggleCallback(handles.origToggle)

% Do some animation

xyOrig = getappdata(handles.f,'xyIn');
xyFinal = getappdata(handles.f,'xyOut');
A = getappdata(handles.f,'A');
nSeconds = 2; %Approximate time (not including computation/render time)
framerate = 30; %frames per second
nFrames = round(nSeconds*framerate);
deltaXY = (xyFinal-xyOrig)/(nFrames-1);
deltaColor = ([0 0 1] - [1 0 0])/(nFrames-1);

[X,Y] = gplot(A,xyOrig);
animatePlot = line(X,Y,'Color','r','Marker','o');
for f = 1:nFrames
    xy = xyOrig + (f-1)*deltaXY;
    Color = [1 0 0] + (f-1)*deltaColor;
    [X,Y] = gplot(A,xy);
    set(animatePlot,'XData',X,'YData',Y,'Color',Color)
    pause(1/framerate);
end
delete(animatePlot)

% Display solution
set(handles.solnToggle,'Value',1)
solnToggleCallback(handles.solnToggle)
end

Contact us