%
% This function recieves the prescription, (TLB, TUB, CUB, RUB),
% from the modeling GUI.  The following model is solved:
%
%  min \omega l^T \alpha + u_C^T \beta + u_G^T \gamma
%
%  such that
%
%     TLB - L \alpha <=  A_T x   <= TUB
%                        A_C x   <= CUB + U_C \beta  
%                        A_G x   <= RUB + U_G \gamma (RUB is GUB in paper)
%                  0 <= L \alpha <= TLB
%               -RUB <= U_C \beta
%                  0 <= U_G \gamma
%                  0 <= x
%
%  Complete details of this model are found in "Designing Radiotherapy
%  Plans with Elastic Constraints and Interior Point Methods" by Allen
%  Holder, Trinity University Mathematics, Technical Report #59, 2000.
%
%  Allen Holder - aholder@trinity.edu
%                 http://www.trinity.edu/aholder
%

function DesignPlan

% Define some global variables that are used in GUI.
% The global variables are used mostly for speed.
%
%
% prescription - an N x N x 2 matrix;
%       (:,:,1) contains the prescribed limits,
%       (:,:,2) is 0,1,2
%               0 - no prescribed limit
%               1 - indicates a tumor pixel
%               2 - indicates a regular tissue
%
% DisplayPrescription - same as the first level of prescription
% 	but that all regular tissue levels are set at 5 for viewing
%	purposes.
%
% UniLevel - Uniformity Level for the for the tumor
%

clear all;

% Cycle through Problems
ProbDir = struct( ...
   'p1', ['Acoustic'], ...
   'p2', ['AVM'], ...
   'p3', ['Lung'], ...
   'p4', ['Prostate'], ...
   'p5', ['CircleTarget'] );

for dn = 1:5

% clear out some things
clear prescription;

% Make directory for the problem
DirString = ['../solns/', eval( [ sprintf('ProbDir.p%d',dn) ] )];
if ~isdir(DirString)
   eval([sprintf('!mkdir %s', DirString)]);
else
   fprintf('RAD -> Director %s already exists \n', DirString)
end
% Link images directory to soln directory
!rm -rf ../images;
DirString = ['./solns/', eval( [ sprintf('ProbDir.p%d',dn) ] )];
eval( [ sprintf('!ln -sf %s ../images', DirString) ] );

%
% Remove the previous images
%
fprintf('\n\nRAD -> Removing Everything in ../images \n');
!rm -rf ../images/*;

%
% Load the Dose Matrix
%
DirString = [ eval( [ sprintf('ProbDir.p%d', dn) ] ) ];
fprintf('RAD -> Loading DoseMatrix \n')
!rm -rf ./DoseMatrix.m
eval( [ sprintf('!ln -sf ../probs/%s/DoseMatrix.m DoseMatrix.m', DirString) ] );
DoseMatrix

%
% Load UniLevel.  This is currently a link to a file.
%

%load uniLevel
UniLevel = 5;

%
% Load the prescription information
%
!rm -rf ./Types.m
eval( [ sprintf('!ln -sf ../probs/%s/Types.m Types.m', DirString) ] );
!rm -rf ./Prescript.m
eval( [ sprintf('!ln -sf ../probs/%s/Prescript.m Prescript.m', DirString) ] );
!rm -rf ./DoseMatrixIndices.m
eval( [ ...
  sprintf('!ln -sf ../probs/%s/DoseMatrixIndices.m DoseMatrixIndices.m', ...
  DirString) ] );

Types
Prescript
DoseMatrixIndices

% Make the prescription
prescription(:,:,1) = prescript;
prescription(:,:,2) = types;

%
% Start the Parse function. This function inputs the DoseMatrix, 
% prescription, and the UniLevel and outputs the data variables
% that we need in a structure.
%

fprintf('RAD -> Parsing Data and Forming Problem Structure \n')
Prob = Parse(A,prescription,UniLevel,ColIndex);

%
% Set the file format for the saved images
%
SaveType = 'epsc';

%
% Start the Solver function. This function loads the variables from
% the structure created by Parse and solves the linear program.
%

fprintf('RAD -> Calculating PDFs \n')
Prob = PDFmaker(Prob, SaveType);

%
% Start a LaTex file that creates a big table
%
LaTex('OpenFile','','',[])

%
%  Get list of PDF types
%
PDFtype = struct( ...
             't1', ['BalancedAvg'], ...
             't2', ['BalancedMax'], ...
             't3', ['PrimalAvg'], ...
             't4', ['PrimalMax'], ...
             't5', ['DualAvg'], ...
             't6', ['DualMax'], ...
             't7', ['InteriorAvg'], ...
             't8', ['InteriorMax'], ...
             't9', ['SetCover1'], ...
             't10', ['SetCover2'], ...
             't11', ['Scoring'], ...
             't12', ['EntropyAvgBalanced']);

%
% Number of Angles to select
%
NumToSelect=[5, 9, 14];

%
% Generate Solutions via Scoring - Problem, 
%    vec. of NumAngles, Scores
%
fprintf('RAD -> Going Into Scoring Beam Selection \n')
Scoring(Prob, NumToSelect, PDFtype, prescription, SaveType);

%
% Go through the local searches
%
fprintf('RAD -> Going into Local Search \n')
LocalSearch(Prob, NumToSelect, prescription, SaveType);

%
% Generate Solutions via vect. quantization
%
fprintf('RAD -> Going Into Vector Quantizer \n')
VQ(Prob, NumToSelect, PDFtype, prescription, SaveType);

%
% Generate Solutions via set cover
%
fprintf('RAD -> Going Into Set Cover \n')
SetCover(Prob, NumToSelect, [.5, .75], PDFtype, prescription, SaveType);

%
%  Lets make some guesses
%
fprintf('RAD -> Going into Simmulated Annealing \n')
SimmulatedAnnealing(Prob, NumToSelect, prescription, SaveType);

%
% Go to the genetic algorithms
%
fprintf('RAD -> Going into GAs \n')
GA(Prob, NumToSelect, prescription, SaveType);

LaTex('CloseFile','','',[], [])

end
return
