function DesignPlan

clear all;

% Cycle through Problems
ProbDir = struct( ...
   'p1', ['1063_acoustic_brain'], ...
   'p2', ['2792_lung'], ...
   'p3', ['7295_abdomen'] );

for dn = 1:1

for resolution = 3:3

% clear out some things
clear prescription Prob Soln;

% Make directory for the problem
DirString = ['../solns/', eval( [ sprintf('ProbDir.p%d',dn) ] ), ...
   sprintf('-%dmm', resolution) ];
if ~isdir(DirString)
   eval([sprintf('!mkdir %s', DirString)]);
end

% Link images directory to soln directory
!rm -rf ../images;
DirString = ['./solns/', eval( [ sprintf('ProbDir.p%d',dn) ] ), ...
   sprintf('-%dmm', resolution) ];
eval( [ sprintf('!ln -sf %s ../images', DirString) ] );

%
% Remove the previous images
%
%!rm -rf ../images/*;

% Start the log file
LogFile = fopen('../images/Rad.log', 'wt+');
fprintf(LogFile, 'RAD -> (Attempting) To Solve %s \n', ...
   eval( [ sprintf('ProbDir.p%d',dn) ] ) );
fclose(LogFile);

%
% Load the Dose Matrix
%
DirString = [ eval( [ sprintf('ProbDir.p%d', dn) ] ) ];
LogMesg('RAD -> Loading DoseMatrix')
!rm -rf ./DoseMatrix.m
eval( sprintf('!ln -sf ../probs/%s/%dmm/DoseMatrix.m DoseMatrix.m', ...
   DirString, resolution));
A = spconvert(load('DoseMatrix.m'));

%load uniLevel
UniLevel = 5;

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

Types
Prescript
DoseMatrixIndices

% Figure out the number of slices
[m, n, NumSlices] = size(prescript);

% Make the prescription
for slice = 1:NumSlices
   prescription(:,:,slice,1) = prescript(:,:,slice)';
   prescription(:,:,slice,2) = types(:,:,slice)';
end

%Save some memory
clear prescript 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.
%

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

% Don't need this any longer
clear A;

%
% Check to make sure its possible to treat the tumor
%
[m, n] = size(Prob.TumorDoseMatrix);
if min(Prob.TumorDoseMatrix * ones(n,1)) == 0
   LogMesg('Tumor Dose Matrix contains zero rows');
   return
end

%
% Get a Solution Structure
%
Soln = GetSoln;
Soln.ProbName = DirString;

%
% 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.
%

LogMesg('RAD -> Calculating PDFs')
[Prob, Soln] = PDFmaker(Prob, SaveType, Soln);

% Get the figures of the full angle solution.
Figure(Prob, prescription, '72AngleSoln', '.', SaveType)

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

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']);
save ../images/AngleAssingments Prob PDFtype
return

%save /tmp/xx Prob A prescription Soln SaveType PDFtype

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

%
% Generate Solutions via Scoring - Problem, 
%    vec. of NumAngles, Scores
%
LogMesg('RAD -> Going Into Scoring Beam Selection')
Soln = Scoring(Prob, NumToSelect, PDFtype, prescription, SaveType, Soln);
%save /tmp/xx Prob A prescription Soln SaveType PDFtype NumToSelect

%
% Go through the local searches
%
%LogMesg('RAD -> Going into Local Search')
%Soln = LocalSearch(Prob, NumToSelect, prescription, SaveType, Soln);
%save /tmp/xx Prob A prescription Soln SaveType PDFtype NumToSelect

%
% Generate Solutions via vect. quantization
%
LogMesg('RAD -> Going Into Vector Quantizer')
Soln = VQ(Prob, NumToSelect, PDFtype, prescription, SaveType, Soln);
%save /tmp/xx Prob A prescription Soln SaveType PDFtype NumToSelect

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

%
%  Lets make some guesses
%
%LogMesg('RAD -> Going into Simmulated Annealing')
%Soln = SimmulatedAnnealing(Prob, NumToSelect, prescription, SaveType, Soln);
%save /tmp/xx Prob A prescription Soln SaveType PDFtype NumToSelect

%
% Go to the genetic algorithms
%
%LogMesg('RAD -> Going into GAs')
%Soln = GA(Prob, NumToSelect, prescription, SaveType, Soln);
%save /tmp/xx Prob A prescription Soln SaveType PDFtype NumToSelect

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

LogMesg('RAD -> Saving Solution Stats')
save ../images/SolutionStats Soln;
save ../images/MatVars Prob A prescription Soln SaveType PDFtype NumToSelect

end

end
return
