-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiment.m
181 lines (151 loc) · 5.95 KB
/
Experiment.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
%MIT License
%Copyright (c) 2019 Sherman Lo
%EXPERIMENT (Abstract) Superclass for check pointing experiments and saving results
% Objects derived from this class has a name and an indicator if the experiment is completed
% In addition, it is recommended the constructor of derived class should have no agurments
%
% The instantised object is saved in a .mat file in the results folder, with the class name.
% Call the method run() to run the experiment
%
% IMPORTANT: Should the experiment terminate for whatever reason, re-instantiate the experiment and
% ressume the experiment. This is so that all member variables are reset to its initial values or
% since its last checkpoint.
%
% The constructor is designed to be run differently depending if the file exist
% 1. if there is no file, the constructor will instantise itself and save itself to a .mat file.
% The method run() can be called to do the experiment
% 2. if the .mat file can be loaded, the member variables are read from that and instantised with
% these member variables
%
% Abstract methods:
% setup() this is where ALL member variables from the derived class are assigned
% doExperiment() run the full (or resume) the experiment
% printResults() print the results using derived member variables
classdef Experiment < handle
%MEMBER VARIABLES
properties (SetAccess = public)
experimentName; %string, name of the experiment and the file name for storing it in a .mat file
isComplete; %boolean, true if the experiment is completed
end
properties (SetAccess = protected, GetAccess = protected)
nIteration; %total of iterations in the experiment
iIteration; %number of iterations done so far
nArrow; %number of arrows to be displayed in the progress bar
end
%METHODS
methods (Access = public)
%CONSTRUCTOR
function this = Experiment()
this.experimentName = class(this); %use the class as the experiment name
%try and load an existing .mat file, then either print and run the experiment
try
%load the file
loadThis = load(fullfile(getResultsDirectory(),strcat(this.experimentName,'.mat')));
this = loadThis.this;
%catch problems reading the file
catch
%assign member variables
this.isComplete = false;
this.nArrow = -1;
%set up the experiment
this.setup();
%save a .mat file
this.save();
end
end %constructor
%METHOD: RUN EXPERIMENT
%Does the experiment if the experiment is not completed
function run(this)
%if the experiment is completed, show a message
if this.isComplete
disp(cell2mat({this.experimentName,' already complete'}));
%else, do the experiment, set isComplete to be true and save it
else
this.doExperiment();
this.isComplete = true;
this.save();
end
end
%METHOD: DELETE RESULTS
%Delete the .mat file storing the results
function deleteResults(this)
if (strcmp(input('Are you sure? ','s'),'yes'))
delete(fullfile(getResultsDirectory,strcat(this.experimentName,'.mat')));
disp(strcat(fullfile(getResultsDirectory,strcat(this.experimentName,'.mat')), ' deleted'));
else
disp('not deleted');
end
end
%METHOD: SAVE STATE
%Save itself in a .mat file
function save(this)
%turn off warning for saving java member variables
warning('off','MATLAB:Java:ConvertFromOpaque');
save(fullfile(getResultsDirectory,strcat(this.experimentName,'.mat')),'this');
warning('on','MATLAB:Java:ConvertFromOpaque');
%print text that the experiment has been saved
disp(strcat(fullfile(getResultsDirectory,strcat(this.experimentName,'.mat')), ' saved'));
end
end %methods
%PROTECTED METHODS
methods (Access = protected)
%METHOD: SET N ITERATION
%Set the number of iterations so that the method madeProgress can called
%PARAMETERS:
%nIteration: number of iterations in the experiment
function setNIteration(this, nIteration)
this.nIteration = nIteration;
this.iIteration = 0;
end
%METHOD: MADE PROGRESS
%Update the number of iterations made in this experiment and print the progress bar
function madeProgress(this)
this.iIteration = this.iIteration+1;
this.printProgress(this.iIteration / this.nIteration);
end
%METHOD: PRINT PROGRESS
%Displays a progress bar (with resoloution of 20)
%PARAMETERS:
%p: fraction of progress done (between 0 and 1)
function printProgress(this, p)
%get the number of arrows to be displayed
newNArrow = floor(p*20);
%if the number of arrows to be displayed is bigger than the number of arrows displayed before
if newNArrow > this.nArrow
%save the number of arrows
this.nArrow = newNArrow;
%declare an array of . enclosed by square brackets
progressBar(1:22) = '.';
progressBar(1) = '[';
progressBar(end) = ']';
%for each arrow, put an arrow
for i = 1:this.nArrow
progressBar(i+1) = '>';
end
%display time
time = clock;
time = time(4:5);
progressBar = [progressBar, ' ', class(this), ' at time: ', num2str(time)];
%display the progress bar
disp(progressBar);
end
end %printProgress
end
%ABSTRACT PROTECTED METHODS
methods (Abstract, Access = protected)
%SETUP EXPERIMENT (Abstract method)
%Recommended to be protected
setup(this)
%DO EXPERIMENT (Abstract method)
%Does (or resume) the experiment
%Recommended to be protected
doExperiment(this)
end
%ABSTRACT PUBLIC METHODS
methods (Abstract, Access = public)
%PRINT RESULTS (Abstract method)
%Export the results to a figure or table, to be used by latex
%Recommended to be protected
printResults(this)
end
end