0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 classdef sosHistory < handle
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 properties
0069 sosObj
0070 itHistory
0071 tempHistory
0072 costHistory
0073 deltaCostHistory
0074 pFlipHistory
0075 pvalTestNames
0076 pvalHistory
0077 outFile
0078 bufferedWrite
0079 end
0080
0081
0082 methods
0083
0084
0085 function obj = sosHistory(sosObj)
0086
0087
0088
0089
0090
0091 obj.sosObj = sosObj;
0092
0093 obj.costHistory = [];
0094 obj.deltaCostHistory = [];
0095 obj.pvalTestNames = {};
0096 obj.pvalHistory = [];
0097 obj.tempHistory= [];
0098 obj.pFlipHistory = [];
0099 obj.itHistory = [];
0100
0101 obj.bufferedWrite = false;
0102
0103
0104
0105 for i=1:length(sosObj.sosstattests)
0106 obj.pvalTestNames = horzcat(obj.pvalTestNames,...
0107 sosObj.sosstattests{i}.label);
0108 end
0109
0110 verbosePrint('Detailed optimization history will now be recorded...', ...
0111 'sosHistory_constructor_end');
0112
0113 end
0114
0115
0116
0117 function updateHistory(obj,curIt,cost,deltaCost,pFlip, ...
0118 testNames,testps,temp)
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132 obj.costHistory = vertcat(obj.costHistory,cost);
0133 obj.deltaCostHistory = vertcat(obj.deltaCostHistory,deltaCost);
0134 obj.tempHistory= vertcat(obj.tempHistory, temp);
0135 obj.pFlipHistory = vertcat(obj.pFlipHistory, pFlip);
0136 obj.itHistory = vertcat(obj.itHistory, curIt);
0137
0138
0139
0140 newRow = nan(1,length(obj.pvalTestNames));
0141 updateHeader = false;
0142
0143 for i=1:length(testNames)
0144 index = -1;
0145 for j =1:length(obj.pvalTestNames)
0146 if strcmp(testNames{i},obj.pvalTestNames{j})
0147 index = j;
0148 break;
0149 end
0150 end
0151
0152
0153
0154 if index == -1
0155 updateHeader = true;
0156 obj.pvalTestNames = horzcat(obj.pvalTestNames,testNames{i});
0157 newRow = horzcat(newRow,testps(i));
0158 else
0159
0160 newRow(index)=testps(i);
0161 end
0162
0163 end
0164
0165
0166
0167
0168
0169 if(size(obj.pvalHistory,2) == length(newRow))
0170 obj.pvalHistory = vertcat(obj.pvalHistory, newRow);
0171 elseif(size(obj.pvalHistory,2) < length(newRow))
0172
0173 diff = length(newRow) - size(obj.pvalHistory,2);
0174 len = size(obj.pvalHistory,1);
0175 newCols = nan(len,diff);
0176
0177
0178 obj.pvalHistory = horzcat(obj.pvalHistory,newCols);
0179 obj.pvalHistory = vertcat(obj.pvalHistory, newRow);
0180 else
0181 error('An error occured when adding a new row to pvalHistory');
0182 end
0183
0184
0185 if obj.bufferedWrite
0186 obj.writeBuffer(updateHeader);
0187 end
0188
0189 end
0190
0191
0192
0193 function setBufferedHistoryOutfile(obj,outFile)
0194
0195
0196
0197
0198
0199
0200
0201
0202 if exist('outFile','var') == 0
0203 error('"Outfile" argument was not supplied to writeHistory()');
0204 end
0205
0206 if(ischar(outFile) == false)
0207 error('Outfile has not been set to a string != "null".');
0208 end
0209
0210 if ischar(outFile) == false || strcmp(outFile,'null')
0211 error('Outfile has not been set to a string != "null".');
0212 end
0213
0214
0215
0216 try
0217 fid = fopen(outFile,'w');
0218 catch exception
0219 error(['Could not open file: ', outFile]);
0220 end
0221
0222 obj.writeHeader(fid);
0223
0224 fclose(fid);
0225
0226 obj.outFile = outFile;
0227 obj.bufferedWrite = true;
0228
0229 verbosePrint(['Current and future history will be written to: ' outFile,' on each report...'],...
0230 'sosHistory_bufferedHistoryWrite_end');
0231 end
0232
0233
0234
0235 function disableBufferedHistoryWrite(obj)
0236
0237
0238 obj.bufferedWrite = false;
0239
0240 verbosePrint('Buffered writing of history disabled',...
0241 'sosHistory_disableBufferedHistoryWrite_end');
0242 end
0243
0244
0245
0246 function enableBufferedHistoryWrite(obj)
0247
0248
0249
0250 if isempty(obj.outFile) == 0
0251
0252 obj.bufferedWrite = true;
0253
0254 verbosePrint('Buffered writing of history enabled',...
0255 'sosHistory_enableBufferedHistoryWrite_end');
0256 else
0257 error('bufferedHistoryWrite(outFile) must be called to specify an outFile first!');
0258 end
0259 end
0260
0261
0262
0263 function writeBuffer(obj,updateHeader)
0264
0265
0266
0267
0268
0269
0270
0271 try
0272 fid = fopen(obj.outFile,'a');
0273 catch exception
0274 error(['Could not open file: ', obj.outFile]);
0275 end
0276
0277 if updateHeader
0278 obj.writeHeader(fid);
0279 end
0280
0281
0282 for i=length(obj.itHistory):length(obj.itHistory)
0283 obj.writeData(fid,i);
0284 end
0285
0286 fclose(fid);
0287
0288 end
0289
0290
0291
0292 function writeHistory(obj,outFile)
0293
0294
0295
0296
0297
0298
0299
0300 if exist('outFile','var') == 0
0301 error('"Outfile" argument was not supplied to writeHistory()');
0302 end
0303
0304 if(ischar(outFile) == false)
0305 error('Outfile has not been set to a string != "null".');
0306 end
0307
0308 if ischar(outFile) == false || strcmp(outFile,'null')
0309 error('Outfile has not been set to a string != "null".');
0310 end
0311
0312 verbosePrint(['Writing optimization history to: ' outFile,' ...'],...
0313 'sosHistory_writeHistory_begin');
0314
0315 try
0316 fid = fopen(outFile,'w');
0317 catch exception
0318 error(['Could not open file: ', outFile]);
0319 end
0320
0321
0322 obj.writeHeader(fid);
0323
0324
0325
0326
0327
0328
0329 for i=1:length(obj.itHistory)
0330 obj.writeData(fid,i);
0331 end
0332
0333 fclose(fid);
0334
0335 end
0336
0337
0338 function addStatTestName(obj,name)
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348 obj.pvalTestNames = horzcat(obj.pvalTestNames,name);
0349
0350 end
0351
0352 end
0353
0354
0355 methods (Access = private)
0356
0357
0358 function writeHeader(obj,fid)
0359
0360
0361
0362
0363
0364 fprintf(fid,'%s\t','iteration');
0365 fprintf(fid,'%s\t','temp');
0366 fprintf(fid,'%s\t','cost');
0367 fprintf(fid,'%s\t','deltaCost');
0368 fprintf(fid,'%s\t','pFlip');
0369
0370 for i=1:length(obj.pvalTestNames)
0371 fprintf(fid,'%s\t',obj.pvalTestNames{i});
0372 end
0373
0374 fprintf(fid,'\r\n');
0375
0376 end
0377
0378
0379 function writeData(obj,fid,index)
0380
0381
0382
0383
0384
0385
0386 fprintf(fid,'%s\t',num2str(obj.itHistory(index)));
0387 fprintf(fid,'%s\t',num2str(obj.tempHistory(index)));
0388 fprintf(fid,'%s\t',num2str(obj.costHistory(index)));
0389 fprintf(fid,'%s\t',num2str(obj.deltaCostHistory(index)));
0390 fprintf(fid,'%s\t',num2str(obj.pFlipHistory(index)));
0391
0392
0393
0394 for j=1:length(obj.pvalTestNames)
0395 fprintf(fid,'%s\t',num2str(obj.pvalHistory(index,j)));
0396 end
0397
0398 fprintf(fid,'\r\n');
0399
0400 end
0401
0402 end
0403
0404 end
0405