summaryrefslogtreecommitdiffstats
path: root/matlab/algorithms/DART/tools/DARToptimizer.m
blob: 424ff22354c6020257f1caafb82dfb12c97c49a6 (plain)
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
%--------------------------------------------------------------------------
% This file is part of the ASTRA Toolbox
%
% Copyright: 2010-2016, iMinds-Vision Lab, University of Antwerp
%            2014-2016, CWI, Amsterdam
% License: Open Source under GPLv3
% Contact: astra@uantwerpen.be
% Website: http://www.astra-toolbox.com/
%--------------------------------------------------------------------------

classdef DARToptimizer < handle
	
	%----------------------------------------------------------------------
	properties (SetAccess=public,GetAccess=public)
		
		% optimization options
		max_evals	= 100;					% SETTING: Maximum number of function evaluations during optimization.
		tolerance	= 0.1;					% SETTING: Minimum tolerance to achieve.
		display		= 'off';				% SETTING: Optimization output. {'off','on','iter'}
		verbose		= 'yes';				% SETTING: verbose? {'yes','no}
		
		metric		= ProjDiffOptimFunc();	% SETTING: Optimization object. Default: ProjDiffOptimFunc.
		
		% DART options
		DART_iterations = 20;				% SETTING: number of DART iterations in each evaluation.
		
		D_base = [];
		
	end
	
	%----------------------------------------------------------------------
	properties (SetAccess=private,GetAccess=public)
		
		stats = Statistics();
		
	end	
	
	%----------------------------------------------------------------------
	methods (Access=public)

		%------------------------------------------------------------------
		% Constructor
		function this = DARToptimizer(D_base)

			this.D_base = D_base;
			
			% statistics
			this.stats = Statistics();
			this.stats.register('params');
			this.stats.register('values');
			this.stats.register('score');
		end	
	
		%------------------------------------------------------------------
		function opt_values = run(this, params, initial_values)
			
			if nargin < 3
				for i = 1:numel(params)
					initial_values(i) = eval(['this.D_base.' params{i} ';']);
				end
			end
			
			% fminsearch
			options = optimset('display', this.display, 'MaxFunEvals', this.max_evals, 'TolX', this.tolerance);
			opt_values = fminsearch(@this.optim_func, initial_values, options, params);

			% save to D_base
			for i = 1:numel(params)
				eval(sprintf('this.D_base.%s = %d;',params{i}, opt_values(i)));
			end
			
		end
		%------------------------------------------------------------------
		
	end
		
	%----------------------------------------------------------------------
	methods (Access=protected)		
		
		%------------------------------------------------------------------
		function score = optim_func(this, values, params)

			% copy DART 
			D = this.D_base.deepcopy();

			% set parameters
			for i = 1:numel(params)
				eval(sprintf('D.%s = %d;',params{i}, values(i)));
				D.output.pre = [D.output.pre num2str(values(i)) '_'];
			end

			% evaluate
			if D.initialized == 0
				D.initialize();
			end
			rng('default');
			D.iterate(this.DART_iterations);

			% compute score
			score = this.metric.calculate(D, this);
			
			% statistics
			this.stats.add('params',params);
			this.stats.add('values',values);
			this.stats.add('score',score);

			% output
			if strcmp(this.verbose,'yes')
				disp([num2str(values) ': ' num2str(score)]);
			end
			
		end
		%------------------------------------------------------------------

	end
	
end