function interface(H_, ETA_, LAMBDA_, E, S1, savefile, R, S2, rankfile)
%  INTERFACE  Main interface to the NN-Seven card game training functions.
%  INTERFACE(H, ETA, LAMBDA, E, S1, SAVEFILE, R, S2, RANKFILE), where H is
%  the number of hidden nodes, ETA is the learning rate [0, 1], LAMBDA is
%  the learning decay factor [0, 1], E is the number of epochs on which to
%  train, S is the number of epochs between saving intermediate results,
%  SAVEFILE is the name of the file to which results are saved, R is the
%  number of games to play in the ranking cycle, S2 is the number of epochs
%  between ranking cycles, and and RANKFILE is the name of the file to which
%  the ranks are saved, trains a special neural network to learn to play
%  the Swedish card game Seven.
%
%  THIS FUNCTION UPDATES GLOBAL VARIABLES.


global N A_H DELTA_K DELTA_H W1 W2 DELTA_W1 DELTA_W2 ETA LAMBDA;


%
%  Initialize the neural network matrices and parameters.
%
ETA      = ETA_;
LAMBDA   = LAMBDA_;

N        = 108;  % 52 + 8 * 7
H        = H_;
K        = 1;

A_H      = zeros(H + 1, 1) * 0.5;           %  +1 for the bias node
DELTA_K  = zeros(K, 1);
DELTA_H  = zeros(H + 1, 1);                 %  +1 for the bias node
W2       = rand(K, H + 1) * 0.2 - 0.1;      %  +1 for the bias node
W1       = rand(H + 1, N + 1) * 0.2 - 0.1;  %  +1 for the bias node
DELTA_W2 = zeros(K, H + 1);                 %  +1 for the bias node
DELTA_W1 = zeros(H + 1, N + 1);             %  +1 for the bias node


%
%  Save the settings used for these training runs to the savefile.
%
if (S1 > 0)
    [fid, msg] = fopen(savefile, 'a');  %  Open the savefile
    if (fid == -1) error(msg); end      %  Exit on error

    str = sprintf('Hidden nodes = %d, learning rate = %f, lambda decay = %f', H, ETA, LAMBDA);
    disp(str);
    fprintf(fid, '%s\n', str);

    str = sprintf('Running %d training epochs and saving state every %d epochs.', E, S1);
    disp(str);
    fprintf(fid, '%s\n', str);

    str = sprintf('Using %d games per ranking cycle which occur every %d epochs.', R, S2);
    disp(str);
    fprintf(fid, '%s\n', str);

    fclose(fid);  %  Close the savefile
end


%
%  Train the network.
%
for e = 1:E
    %
    %  Do the actual training by calling playgame.
    %
    playgame;
    str = sprintf('Finished game %d', e);
    disp(str);


    %
    %  Save the current network ranking every S2 epochs.
    %
    if (mod(e, S2) == 0)
        %
        %  Play R games of Seven with one AI player and two random players.
        %  Then calculate the ranking of the current network which
        %  represents the probability that the network can win a game.
        %
        rank = [];
        for i = 1:R
            rank = [rank playgame_rand];
        end
        rank = mean(rank);

        str = sprintf('- AI rated at %f', rank);
        disp(str);

        %
        %  Save the ranking to the rankfile.
        %
        [fid, msg] = fopen(rankfile, 'a');
        if (fid == -1) error(msg); end
        fprintf(fid, '%6d %f\n', e, rank);
        fclose(fid);
    end


    %
    %  Save the weight matrices every S1 epochs.
    %
    if (mod(e, S1) == 0)
        disp('Saving weight matrices.');
        %
        %  Open the savefile.
        %
        [fid, msg] = fopen(savefile, 'a');
        if (fid == -1) error(msg); end

        %
        %  Save the weight matrices to the savefile.
        %
        fprintf(fid, 'INPUT_NODES = %d;\n', N);
        fprintf(fid, 'HIDDEN_NODES = %d;\n', H);
        fprintf(fid, 'OUTPUT_NODES = %d;\n', K);

	fprintf(fid, 'W_I2H = [%f', W1(1));
        for i = 2:(N + 1) * (H + 1)
            fprintf(fid, ', %f', W1(i));
        end
        fprintf(fid, '];\n');

	fprintf(fid, 'W_H2O = [%f', W2(1));
        for i = 2:(H + 1) * K
            fprintf(fid, ', %f', W2(i));
        end
        fprintf(fid, '];\n\n');

        %
        %  Close the savefile.
        %
        fclose(fid);
    end
end
