summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/optimization/stopCriteria
diff options
context:
space:
mode:
Diffstat (limited to 'gi/posterior-regularisation/prjava/src/optimization/stopCriteria')
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/stopCriteria/CompositeStopingCriteria.java33
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/stopCriteria/GradientL2Norm.java30
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedGradientL2Norm.java48
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedProjectedGradientL2Norm.java60
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedValueDifference.java54
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/stopCriteria/ProjectedGradientL2Norm.java51
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/stopCriteria/StopingCriteria.java8
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/stopCriteria/ValueDifference.java41
8 files changed, 325 insertions, 0 deletions
diff --git a/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/CompositeStopingCriteria.java b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/CompositeStopingCriteria.java
new file mode 100644
index 00000000..15760f18
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/CompositeStopingCriteria.java
@@ -0,0 +1,33 @@
+package optimization.stopCriteria;
+
+import java.util.ArrayList;
+
+import optimization.gradientBasedMethods.Objective;
+
+public class CompositeStopingCriteria implements StopingCriteria {
+
+ ArrayList<StopingCriteria> criterias;
+
+ public CompositeStopingCriteria() {
+ criterias = new ArrayList<StopingCriteria>();
+ }
+
+ public void add(StopingCriteria criteria){
+ criterias.add(criteria);
+ }
+
+ public boolean stopOptimization(Objective obj){
+ for(StopingCriteria criteria: criterias){
+ if(criteria.stopOptimization(obj)){
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public void reset(){
+ for(StopingCriteria criteria: criterias){
+ criteria.reset();
+ }
+ }
+}
diff --git a/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/GradientL2Norm.java b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/GradientL2Norm.java
new file mode 100644
index 00000000..534ff833
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/GradientL2Norm.java
@@ -0,0 +1,30 @@
+package optimization.stopCriteria;
+
+import optimization.gradientBasedMethods.Objective;
+import optimization.util.MathUtils;
+
+public class GradientL2Norm implements StopingCriteria{
+
+ /**
+ * Stop if gradientNorm/(originalGradientNorm) smaller
+ * than gradientConvergenceValue
+ */
+ protected double gradientConvergenceValue;
+
+
+ public GradientL2Norm(double gradientConvergenceValue){
+ this.gradientConvergenceValue = gradientConvergenceValue;
+ }
+
+ public void reset(){}
+
+ public boolean stopOptimization(Objective obj){
+ double norm = MathUtils.L2Norm(obj.gradient);
+ if(norm < gradientConvergenceValue){
+ System.out.println("Gradient norm below treshold");
+ return true;
+ }
+ return false;
+
+ }
+}
diff --git a/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedGradientL2Norm.java b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedGradientL2Norm.java
new file mode 100644
index 00000000..4a489641
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedGradientL2Norm.java
@@ -0,0 +1,48 @@
+package optimization.stopCriteria;
+
+import optimization.gradientBasedMethods.Objective;
+import optimization.gradientBasedMethods.ProjectedObjective;
+import optimization.util.MathUtils;
+
+/**
+ * Divides the norm by the norm at the begining of the iteration
+ * @author javg
+ *
+ */
+public class NormalizedGradientL2Norm extends GradientL2Norm{
+
+ /**
+ * Stop if gradientNorm/(originalGradientNorm) smaller
+ * than gradientConvergenceValue
+ */
+ protected double originalGradientNorm = -1;
+
+ public void reset(){
+ originalGradientNorm = -1;
+ }
+ public NormalizedGradientL2Norm(double gradientConvergenceValue){
+ super(gradientConvergenceValue);
+ }
+
+
+
+
+ public boolean stopOptimization(Objective obj){
+ double norm = MathUtils.L2Norm(obj.gradient);
+ if(originalGradientNorm == -1){
+ originalGradientNorm = norm;
+ }
+ if(originalGradientNorm < 1E-10){
+ System.out.println("Gradient norm is zero " + originalGradientNorm);
+ return true;
+ }
+ double normalizedNorm = 1.0*norm/originalGradientNorm;
+ if( normalizedNorm < gradientConvergenceValue){
+ System.out.println("Gradient norm below normalized normtreshold: " + norm + " original: " + originalGradientNorm + " normalized norm: " + normalizedNorm);
+ return true;
+ }else{
+// System.out.println("projected gradient norm: " + norm);
+ return false;
+ }
+ }
+}
diff --git a/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedProjectedGradientL2Norm.java b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedProjectedGradientL2Norm.java
new file mode 100644
index 00000000..5ae554c2
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedProjectedGradientL2Norm.java
@@ -0,0 +1,60 @@
+package optimization.stopCriteria;
+
+import optimization.gradientBasedMethods.Objective;
+import optimization.gradientBasedMethods.ProjectedObjective;
+import optimization.util.MathUtils;
+
+/**
+ * Divides the norm by the norm at the begining of the iteration
+ * @author javg
+ *
+ */
+public class NormalizedProjectedGradientL2Norm extends ProjectedGradientL2Norm{
+
+ /**
+ * Stop if gradientNorm/(originalGradientNorm) smaller
+ * than gradientConvergenceValue
+ */
+ double originalProjectedNorm = -1;
+
+ public NormalizedProjectedGradientL2Norm(double gradientConvergenceValue){
+ super(gradientConvergenceValue);
+ }
+
+ public void reset(){
+ originalProjectedNorm = -1;
+ }
+
+
+ double[] projectGradient(ProjectedObjective obj){
+
+ if(obj.auxParameters == null){
+ obj.auxParameters = new double[obj.getNumParameters()];
+ }
+ System.arraycopy(obj.getParameters(), 0, obj.auxParameters, 0, obj.getNumParameters());
+ MathUtils.minusEquals(obj.auxParameters, obj.gradient, 1);
+ obj.auxParameters = obj.projectPoint(obj.auxParameters);
+ MathUtils.minusEquals(obj.auxParameters,obj.getParameters(),1);
+ return obj.auxParameters;
+ }
+
+ public boolean stopOptimization(Objective obj){
+ if(obj instanceof ProjectedObjective) {
+ ProjectedObjective o = (ProjectedObjective) obj;
+ double norm = MathUtils.L2Norm(projectGradient(o));
+ if(originalProjectedNorm == -1){
+ originalProjectedNorm = norm;
+ }
+ double normalizedNorm = 1.0*norm/originalProjectedNorm;
+ if( normalizedNorm < gradientConvergenceValue){
+ System.out.println("Gradient norm below normalized normtreshold: " + norm + " original: " + originalProjectedNorm + " normalized norm: " + normalizedNorm);
+ return true;
+ }else{
+// System.out.println("projected gradient norm: " + norm);
+ return false;
+ }
+ }
+ System.out.println("Not a projected objective");
+ throw new RuntimeException();
+ }
+}
diff --git a/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedValueDifference.java b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedValueDifference.java
new file mode 100644
index 00000000..6dbbc50d
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/NormalizedValueDifference.java
@@ -0,0 +1,54 @@
+package optimization.stopCriteria;
+
+import optimization.gradientBasedMethods.Objective;
+import optimization.util.MathUtils;
+
+public class NormalizedValueDifference implements StopingCriteria{
+
+ /**
+ * Stop if the different between values is smaller than a treshold
+ */
+ protected double valueConvergenceValue=0.01;
+ protected double previousValue = Double.NaN;
+ protected double currentValue = Double.NaN;
+
+ public NormalizedValueDifference(double valueConvergenceValue){
+ this.valueConvergenceValue = valueConvergenceValue;
+ }
+
+ public void reset(){
+ previousValue = Double.NaN;
+ currentValue = Double.NaN;
+ }
+
+
+ public boolean stopOptimization(Objective obj){
+ if(Double.isNaN(currentValue)){
+ currentValue = obj.getValue();
+ return false;
+ }else {
+ previousValue = currentValue;
+ currentValue = obj.getValue();
+ if(previousValue != 0){
+ double valueDiff = Math.abs(previousValue - currentValue)/Math.abs(previousValue);
+ if( valueDiff < valueConvergenceValue){
+ System.out.println("Leaving different in values is to small: Prev "
+ + (previousValue/previousValue) + " Curr: " + (currentValue/previousValue)
+ + " diff: " + valueDiff);
+ return true;
+ }
+ }else{
+ double valueDiff = Math.abs(previousValue - currentValue);
+ if( valueDiff < valueConvergenceValue){
+ System.out.println("Leaving different in values is to small: Prev "
+ + (previousValue) + " Curr: " + (currentValue)
+ + " diff: " + valueDiff);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ }
+}
diff --git a/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/ProjectedGradientL2Norm.java b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/ProjectedGradientL2Norm.java
new file mode 100644
index 00000000..aadf1fd5
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/ProjectedGradientL2Norm.java
@@ -0,0 +1,51 @@
+package optimization.stopCriteria;
+
+import optimization.gradientBasedMethods.Objective;
+import optimization.gradientBasedMethods.ProjectedObjective;
+import optimization.util.MathUtils;
+
+public class ProjectedGradientL2Norm implements StopingCriteria{
+
+ /**
+ * Stop if gradientNorm/(originalGradientNorm) smaller
+ * than gradientConvergenceValue
+ */
+ protected double gradientConvergenceValue;
+
+
+ public ProjectedGradientL2Norm(double gradientConvergenceValue){
+ this.gradientConvergenceValue = gradientConvergenceValue;
+ }
+
+ public void reset(){
+
+ }
+
+ double[] projectGradient(ProjectedObjective obj){
+
+ if(obj.auxParameters == null){
+ obj.auxParameters = new double[obj.getNumParameters()];
+ }
+ System.arraycopy(obj.getParameters(), 0, obj.auxParameters, 0, obj.getNumParameters());
+ MathUtils.minusEquals(obj.auxParameters, obj.gradient, 1);
+ obj.auxParameters = obj.projectPoint(obj.auxParameters);
+ MathUtils.minusEquals(obj.auxParameters,obj.getParameters(),1);
+ return obj.auxParameters;
+ }
+
+ public boolean stopOptimization(Objective obj){
+ if(obj instanceof ProjectedObjective) {
+ ProjectedObjective o = (ProjectedObjective) obj;
+ double norm = MathUtils.L2Norm(projectGradient(o));
+ if(norm < gradientConvergenceValue){
+ // System.out.println("Gradient norm below treshold: " + norm);
+ return true;
+ }else{
+// System.out.println("projected gradient norm: " + norm);
+ return false;
+ }
+ }
+ System.out.println("Not a projected objective");
+ throw new RuntimeException();
+ }
+}
diff --git a/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/StopingCriteria.java b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/StopingCriteria.java
new file mode 100644
index 00000000..10cf0522
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/StopingCriteria.java
@@ -0,0 +1,8 @@
+package optimization.stopCriteria;
+
+import optimization.gradientBasedMethods.Objective;
+
+public interface StopingCriteria {
+ public boolean stopOptimization(Objective obj);
+ public void reset();
+}
diff --git a/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/ValueDifference.java b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/ValueDifference.java
new file mode 100644
index 00000000..e5d07229
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/stopCriteria/ValueDifference.java
@@ -0,0 +1,41 @@
+package optimization.stopCriteria;
+
+import optimization.gradientBasedMethods.Objective;
+import optimization.util.MathUtils;
+
+public class ValueDifference implements StopingCriteria{
+
+ /**
+ * Stop if the different between values is smaller than a treshold
+ */
+ protected double valueConvergenceValue=0.01;
+ protected double previousValue = Double.NaN;
+ protected double currentValue = Double.NaN;
+
+ public ValueDifference(double valueConvergenceValue){
+ this.valueConvergenceValue = valueConvergenceValue;
+ }
+
+ public void reset(){
+ previousValue = Double.NaN;
+ currentValue = Double.NaN;
+ }
+
+ public boolean stopOptimization(Objective obj){
+ if(Double.isNaN(currentValue)){
+ currentValue = obj.getValue();
+ return false;
+ }else {
+ previousValue = currentValue;
+ currentValue = obj.getValue();
+ if(previousValue - currentValue < valueConvergenceValue){
+// System.out.println("Leaving different in values is to small: Prev "
+// + previousValue + " Curr: " + currentValue
+// + " diff: " + (previousValue - currentValue));
+ return true;
+ }
+ return false;
+ }
+
+ }
+}