comparison src/samer/maths/opt/Constraints.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
3 * All rights reserved.
4 *
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
6 * without even the implied warranty of MERCHANTABILITY or
7 * FITNESS FOR A PARTICULAR PURPOSE.
8 */
9
10 package samer.maths.opt;
11 import samer.maths.*;
12 import samer.core.*;
13
14 public abstract class Constraints
15 {
16 public State S;
17 public int[] active; // active dimensions
18 public int[] inactive; // inactive dimensions
19 public int n,m; // number of active dimensions
20
21 public Constraints(State s)
22 {
23 S=s; n=S.n;
24 active = new int[n];
25 inactive = new int[n];
26 activateAll();
27 }
28
29 public final int activeCount() { return m; }
30
31 public void report()
32 {
33 Shell.print("inactive dimensions:");
34 for (int i=0; i<(n-m); i++) {
35 Shell.print(" - "+inactive[i]);
36 }
37 Shell.print("active dimensions:");
38 for (int i=0; i<m; i++) {
39 Shell.print(" + "+active[i]);
40 }
41 Shell.print(" active: "+m);
42 Shell.print("inactive: "+(n-m));
43 }
44
45 public void activateAll() {
46 for (int i=0; i<n; i++) active[i]=i;
47 m=n;
48 }
49
50 public void deactivateAll() {
51 for (int i=0; i<n; i++) inactive[i]=i;
52 m=0;
53 }
54
55 public void zeroInactive(double [] z) {
56 for (int i=0; i<(n-m); i++) z[inactive[i]]=0;
57 }
58
59 /**
60 Inactivate the i'th dimension
61 Presupposes that it is not already inactive
62 */
63 public final void inactivate(int i)
64 {
65 inactive[n-m]=i;
66 int j=Util.find(i,active,m);
67 if (j<(m-1)) {
68 System.arraycopy(active,j+1,active,j,m-j-1);
69 }
70 m--;
71 }
72
73 /**
74 activate the i'th dimension
75 Presupposes that it is not already inactive
76 */
77 public final void activate(int i)
78 {
79 active[m]=i;
80 int j=Util.find(i,inactive,n-m);
81 if (j<(n-m-1)) {
82 System.arraycopy(inactive,j+1,inactive,j,n-m-j-1);
83 }
84 m++;
85 }
86
87 /**
88 take a step of lambda*h, set alpha, but don't
89 evaluate function at new point
90 */
91 public void step(double lambda)
92 {
93 for (int i=0; i<m; i++) {
94 int j=active[i];
95 S.P2.x[j] = S.P1.x[j] + lambda*S.h[j];
96 }
97 S.alpha=lambda;
98 }
99
100 /** evaluate function and gradient at P2 */
101 public void eval2()
102 {
103 S.func.evaluate(S.P2);
104 S.P2.s = dot(S.P2.g,S.h);
105 }
106
107 /** sparse matrix mutiplication */
108 public final void mul( double [] y, double [][]A, double [] x)
109 {
110 double [] arow;
111
112 for (int k=0; k<m; k++) {
113 int i=active[k];
114 double t=0;
115 arow=A[i];
116 for (int l=0; l<m; l++) { int j=active[l]; t+=arow[j]*x[j]; }
117 y[i]=t;
118 }
119 }
120
121 /** sparse vector dot product */
122 public final double dot( double [] a, double [] b)
123 {
124 double t=0;
125 for (int k=0; k<m; k++) { int i=active[k]; t+=a[i]*b[i]; }
126 return t;
127 }
128
129 /** sparse vector time scalar */
130 public final void mul( double [] a, double b)
131 {
132 for (int k=0; k<m; k++) { int i=active[k]; a[i]*=b; }
133 }
134
135 /** sparse vector subtraction */
136 public final void sub( double [] out, double [] a, double [] b)
137 {
138 for (int k=0; k<m; k++) { int i=active[k]; out[i]=a[i]-b[i]; }
139 }
140
141 /** sparse vector subtraction in place */
142 public final void sub( double [] a, double [] b)
143 {
144 for (int k=0; k<m; k++) { int i=active[k]; a[i]-=b[i]; }
145 }
146
147
148 /** sparse vector negation in place */
149 public final void negate(double [] x)
150 {
151 for (int k=0; k<m; k++) { int i=active[k]; x[i]=-x[i]; }
152 }
153
154 /** sparse vector negation in place */
155 public final void negate(double [] x, double [] y)
156 {
157 for (int k=0; k<m; k++) { int i=active[k]; y[i]=-x[i]; }
158 }
159
160 /** sparse max(abs(x)) */
161 public final double maxabs(double [] x)
162 {
163 double max=0;
164 for (int k=0; k<m; k++) {
165 double xk = Math.abs(x[active[k]]);
166 if (xk>max) max=xk;
167 }
168 return max;
169 }
170
171 // ugh
172 public boolean release(Datum P) { return false; }
173 public int getReleasedDimension() { return -1; }
174 public int getReleasableDimensions() { return 0; }
175 public abstract void lineSearch(Condition c, CubicLineSearch ls, double step);
176 public abstract void initialise();
177
178 public interface Factory {
179 public Constraints build(MinimiserBase min);
180 }
181 }