summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/optimization/linesearch/ProjectedDifferentiableLineSearchObjective.java
blob: 29ccbc32734ccd05da69363508db39555b321d34 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package optimization.linesearch;

import optimization.gradientBasedMethods.Objective;
import optimization.gradientBasedMethods.ProjectedObjective;
import optimization.util.MathUtils;
import optimization.util.MatrixOutput;


/**
 * See ArmijoLineSearchMinimizationAlongProjectionArc for description
 * @author javg
 *
 */
public class ProjectedDifferentiableLineSearchObjective extends DifferentiableLineSearchObjective{

	
	
	ProjectedObjective obj;
	public ProjectedDifferentiableLineSearchObjective(Objective o) {
		super(o);
		if(!(o instanceof ProjectedObjective)){
			System.out.println("Must receive a projected objective");
			throw new RuntimeException();
		}
		obj = (ProjectedObjective) o;
	}

	
	
	public double[] projectPoint (double[] point){
		return ((ProjectedObjective)o).projectPoint(point);
	}
	public void updateAlpha(double alpha){
		if(alpha < 0){
			System.out.println("alpha may not be smaller that zero");
			throw new RuntimeException();
		}
		
		if(obj.auxParameters == null){
			obj.auxParameters = new double[obj.getParameters().length];
		}
		
		nrIterations++;
		
		steps.add(alpha);		
		System.arraycopy(originalParameters, 0, obj.auxParameters, 0, obj.auxParameters.length);
		
		//Take a step into the search direction
		
//		MatrixOutput.printDoubleArray(obj.getGradient(), "gradient");
		
//		alpha=gradients.get(0)*alpha/(gradients.get(gradients.size()-1));
	
		//x_t+1 = x_t - alpha*gradient = x_t + alpha*direction
		MathUtils.plusEquals(obj.auxParameters, searchDirection, alpha);
//		MatrixOutput.printDoubleArray(obj.auxParameters, "before projection");
		obj.auxParameters = projectPoint(obj.auxParameters);
//		MatrixOutput.printDoubleArray(obj.auxParameters, "after projection");
		o.setParameters(obj.auxParameters);
//		System.out.println("new parameters");
//		o.printParameters();
		values.add(o.getValue());
		//Computes the new gradient x_k-[x_k-alpha*Gradient(x_k)]+ 
		MathUtils.minusEqualsInverse(originalParameters,obj.auxParameters,1);
//		MatrixOutput.printDoubleArray(obj.auxParameters, "new gradient");
		//Dot product between the new direction and the new gradient
		double gradient = MathUtils.dotProduct(obj.auxParameters,searchDirection);
		gradients.add(gradient);	
		if(gradient > 0){
			System.out.println("Gradient on line search has to be smaller than zero");
			System.out.println("Iter: " + nrIterations);
			MatrixOutput.printDoubleArray(obj.auxParameters, "new direction");
			MatrixOutput.printDoubleArray(searchDirection, "search direction");
			throw new RuntimeException();
			
		}
		
	}
	
	/**
	 * 
	 */
//	public void updateAlpha(double alpha){
//		
//		if(alpha < 0){
//			System.out.println("alpha may not be smaller that zero");
//			throw new RuntimeException();
//		}
//		
//		nrIterations++;
//		steps.add(alpha);
//		//x_t+1 = x_t - alpha*direction
//		System.arraycopy(originalParameters, 0, parametersChange, 0, parametersChange.length);
////		MatrixOutput.printDoubleArray(parametersChange, "parameters before step");
////		System.out.println("Step" + alpha);
//		MatrixOutput.printDoubleArray(originalGradient, "gradient + " + alpha);
//
//		MathUtils.minusEquals(parametersChange, originalGradient, alpha);
//		
//		//Project the points into the feasibility set
////		MatrixOutput.printDoubleArray(parametersChange, "before projection");
//		//x_k(alpha) = [x_k - alpha*grad f(x_k)]+
//		parametersChange = projectPoint(parametersChange);
////		MatrixOutput.printDoubleArray(parametersChange, "after projection");
//		o.setParameters(parametersChange);
//		values.add(o.getValue());
//		//Computes the new direction x_k-[x_k-alpha*Gradient(x_k)]+
//		
//		direction=MathUtils.arrayMinus(parametersChange,originalParameters);
////		MatrixOutput.printDoubleArray(direction, "new direction");
//		
//		double gradient = MathUtils.dotProduct(originalGradient,direction);
//		gradients.add(gradient);		
//		if(gradient > 1E-10){
//			System.out.println("cosine " + gradient/(MathUtils.L2Norm(originalGradient)*MathUtils.L2Norm(direction)));
//			
//			
//			System.out.println("not a descent direction for alpha " + alpha);
//			System.arraycopy(originalParameters, 0, parametersChange, 0, parametersChange.length);
//			MathUtils.minusEquals(parametersChange, originalGradient, 1E-20);
//			
//			parametersChange = projectPoint(parametersChange);
//			direction=MathUtils.arrayMinus(parametersChange,originalParameters);
//			gradient = MathUtils.dotProduct(originalGradient,direction);
//			if(gradient > 0){
//				System.out.println("Direction is really non-descent evern for small alphas:" + gradient);
//			}
//			System.out.println("ProjecteLineSearchObjective: Should be a descent direction at " + nrIterations + ": "+ gradient);
////			System.out.println(Printing.doubleArrayToString(originalGradient, null,"Original gradient"));
////			System.out.println(Printing.doubleArrayToString(originalParameters, null,"Original parameters"));
////			System.out.println(Printing.doubleArrayToString(parametersChange, null,"Projected parameters"));
////			System.out.println(Printing.doubleArrayToString(direction, null,"Direction"));
//			throw new RuntimeException();
//		}
//	}
	
}