summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/optimization/gradientBasedMethods/Objective.java
blob: 0e2e27ac2c0dc9f3c349e01f65617caedb0bb4c6 (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
package optimization.gradientBasedMethods;


/**
 * Defines an optimization objective:
 * 
 * 
 * @author javg
 *
 */
public abstract  class Objective {

	protected int functionCalls = 0;
	protected int gradientCalls = 0;
	protected int updateCalls = 0;
	
	protected double[] parameters;
	
	//Contains a cache with the gradient
	public double[] gradient;
	int debugLevel = 0;
	
	public void setDebugLevel(int level){
		debugLevel = level;
	}
	
	public int getNumParameters() {
		return parameters.length;
	}

	public double getParameter(int index) {
		return parameters[index];
	}

	public double[] getParameters() {
		return parameters;
	}

	public abstract double[] getGradient( );
	
	public void setParameter(int index, double value) {
		parameters[index]=value;
	}

	public void setParameters(double[] params) {
		if(parameters == null){
			parameters = new double[params.length];
		}
		updateCalls++;
		System.arraycopy(params, 0, parameters, 0, params.length);
	}

	
	public int getNumberFunctionCalls() {
		return functionCalls;
	}

	public int getNumberGradientCalls() {
		return gradientCalls;
	}
	
	public String finalInfoString() {
		return "FE: " + functionCalls + " GE " + gradientCalls + " Params updates" +
		updateCalls;
	}
	public void printParameters() {
		System.out.println(toString());
	}	
	
	public abstract String toString();	
	public abstract double getValue ();
	
	/**
	 * Sets the initial objective parameters
	 * For unconstrained models this just sets the objective params = argument no copying
	 * For a constrained objective project the parameters and then set
	 * @param params
	 */
	public  void setInitialParameters(double[] params){
		parameters = params;
	}

}