JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
RBSCMSystem.java
Go to the documentation of this file.
1 package rb;
2 
3 import jarmos.Log;
5 
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.Comparator;
12 import java.util.Iterator;
13 import java.util.LinkedHashMap;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Vector;
18 
19 import org.apache.commons.math.optimization.GoalType;
20 import org.apache.commons.math.optimization.OptimizationException;
21 import org.apache.commons.math.optimization.RealPointValuePair;
22 import org.apache.commons.math.optimization.linear.LinearConstraint;
23 import org.apache.commons.math.optimization.linear.LinearObjectiveFunction;
24 import org.apache.commons.math.optimization.linear.Relationship;
25 import org.apache.commons.math.optimization.linear.SimplexSolver;
26 
37 public class RBSCMSystem {
38 
39  protected RBSystem sys;
40 
41  // Logging tag
42  private static final String DEBUG_TAG = "RBSCMSystem";
43 
47  private int SCM_M;
48 
52  private double[] B_min;
53  private double[] B_max;
54 
58  private Vector<double[]> C_J;
59 
63  protected double[] C_J_stability_vector;
64 
69  private double[][] SCM_UB_vectors;
70 
74  private double[] saved_parameters;
75 
77  this.sys = sys;
78  }
79 
80  protected double thetaQa(int q) {
81  return sys.thetaQa(q);
82  }
83 
84  protected int getQa() {
85  return sys.getQa();
86  }
87 
91  public double get_SCM_LB() {
92  double min_J_obj = 0.;
93 
94  try {
95 
96  // First, declare the constraints
97  Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
98 
99  // Add bounding box constraints for the get_Q_a() variables
100  for (int q = 0; q < getQa(); q++) {
101  double[] index = new double[getQa()];
102  index[q] = 1.;
103 
104  constraints.add(new LinearConstraint(index, Relationship.GEQ, B_min[q]));
105  constraints.add(new LinearConstraint(index, Relationship.LEQ, B_max[q]));
106  }
107 
108  // Sort the indices of C_J based on distance from current_parameters
109  List<Integer> sortedIndices = getSorted_CJ_Indices();
110 
111  // Save the current_parameters since we'll change them in the loop
112  // below
114 
115  // Add the constraint rows
116  int n_rows = Math.min(SCM_M, C_J.size());
117  int count = 1;
118 
119  if (n_rows > 0) {
120  for (Integer mu_index : sortedIndices) {
122  // current_parameters = C_J.get(mu_index);
123 
124  double[] constraint_row = new double[getQa()];
125  for (int q = 0; q < getQa(); q++) {
126  constraint_row[q] = sys.thetaQa(q);
127  }
128 
129  constraints.add(new LinearConstraint(constraint_row, Relationship.GEQ,
130  C_J_stability_vector[mu_index]));
131 
132  if (count >= n_rows)
133  break;
134 
135  count++;
136  }
137  }
138 
139  // Now load the original parameters back into current_parameters
140  // in order to set the coefficients of the objective function
142 
143  // Create objective function object
144  double[] objectiveFn = new double[getQa()];
145  for (int q = 0; q < getQa(); q++) {
146  objectiveFn[q] = sys.thetaQa(q);
147  }
148  LinearObjectiveFunction f = new LinearObjectiveFunction(objectiveFn, 0.);
149 
150  SimplexSolver solver = new SimplexSolver();
151  RealPointValuePair opt_pair = solver.optimize(f, constraints, GoalType.MINIMIZE, false);
152  min_J_obj = opt_pair.getValue();
153  } catch (OptimizationException e) {
154  Log.e("DEBUG_TAG", "Optimal solution not found");
155  e.printStackTrace();
156  } catch (Exception e) {
157  Log.e("DEBUG_TAG", "Exception occurred during SCM_LB calculation");
158  e.printStackTrace();
159  }
160 
161  Log.d(DEBUG_TAG, "SCM val = " + min_J_obj);
162  return min_J_obj;
163  }
164 
168  public double get_SCM_UB() {
169 
170  // Sort the indices of C_J based on distance from current_parameters
171  List<Integer> sortedIndices = getSorted_CJ_Indices();
172 
173  // For each mu, we just find the minimum of J_obj over
174  // the subset of vectors in SCM_UB_vectors corresponding
175  // to C_J_M (SCM_UB_vectors contains vectors for all of
176  // C_J).
177  double min_J_obj = 0.;
178  int n_rows = Math.min(SCM_M, C_J.size());
179  int count = 1;
180  for (Iterator<Integer> it = sortedIndices.iterator(); it.hasNext();) {
181  Integer mu_index = (Integer) it.next();
182 
184 
185  double[] UB_vector = SCM_UB_vectors[mu_index];
186 
187  double J_obj = 0.;
188  for (int q = 0; q < getQa(); q++) {
189  J_obj += sys.thetaQa(q) * UB_vector[q];
190  }
191 
192  if ((count == 1) || (J_obj < min_J_obj)) {
193  min_J_obj = J_obj;
194  }
195 
196  if (count >= n_rows)
197  break;
198 
199  count++;
200  }
201 
202  return min_J_obj;
203  }
204 
208  public static double param_dist(double[] mu_1, double[] mu_2) {
209  // Default distance is Euclidean norm
210  double sum = 0.;
211 
212  for (int i = 0; i < mu_1.length; i++) {
213  sum += Math.pow(mu_1[i] - mu_2[i], 2.);
214  }
215 
216  return Math.sqrt(sum);
217  }
218 
222  protected void get_current_parameters_from_C_J(int index) {
223  sys.getParams().setCurrent(C_J.get(index));
224  }
225 
229  protected void save_current_parameters() {
230  saved_parameters = sys.getParams().getCurrent().clone();
231  }
232 
236  protected void reload_current_parameters() {
237  sys.getParams().setCurrent(saved_parameters);
238  }
239 
243  public double[] get_current_parameters() {
244  return sys.getParams().getCurrent();
245  }
246 
247  // @Override
248  // protected void readConfigurationJRB(AModelManager m) {
249  // super.readConfigurationJRB(m);
250  // }
251 
258  public void readConfiguration(AModelManager m) throws IOException {
259  GetPot infile = new GetPot(m.getInStream(Const.parameters_filename), Const.parameters_filename);
260  // int n_SCM_parameters = infile.call("n_SCM_parameters",1);
261  int n_SCM_parameters = infile.call("n_SCM_parameters", infile.call("n_parameters", 1));
262  Log.d(DEBUG_TAG, "n_parameters = " + n_SCM_parameters);
263 
264  SCM_M = infile.call("SCM_M", 0);
265 
266  Log.d(DEBUG_TAG, "RBSCMSystem parameters from " + Const.parameters_filename + ":");
267  Log.d(DEBUG_TAG, "SCM_M: " + SCM_M);
268  }
269 
276  public void loadOfflineData(AModelManager m) throws IOException, InconsistentStateException {
277 
278  // Read in the bounding box minimum values
279  {
280  BufferedReader reader = m.getBufReader("B_min.dat");
281 
282  String line = reader.readLine();
283  String[] tokens = line.split(" ");
284  reader.close();
285  reader = null;
286 
287  B_min = new double[getQa()];
288  for (int i = 0; i < B_min.length; i++) {
289  B_min[i] = Double.parseDouble(tokens[i]);
290  }
291 
292  Log.d(DEBUG_TAG, "Finished reading B_min.dat");
293  }
294 
295  // Read in the bounding box maximum values
296  {
297  BufferedReader reader = m.getBufReader("B_max.dat");
298 
299  String line = reader.readLine();
300  String[] tokens = line.split(" ");
301 
302  B_max = new double[getQa()];
303  for (int i = 0; i < B_max.length; i++) {
304  B_max[i] = Double.parseDouble(tokens[i]);
305  }
306  reader.close();
307  reader = null;
308 
309  Log.d(DEBUG_TAG, "Finished reading B_max.dat");
310  }
311 
312  // Read in the stability constant values
313  {
314  BufferedReader reader = m.getBufReader("C_J_stability_vector.dat");
315 
316  String line = reader.readLine();
317  reader.close();
318  reader = null;
319 
320  try {
321  String[] tokens = line.split(" ");
322 
323  if ((tokens.length == 1) && (tokens[0] == "")) {
324  C_J_stability_vector = null;
325  } else {
326  C_J_stability_vector = new double[tokens.length];
327  for (int i = 0; i < C_J_stability_vector.length; i++) {
328  C_J_stability_vector[i] = Double.parseDouble(tokens[i]);
329  }
330  }
331  } catch (Exception e) {
332  Log.d(DEBUG_TAG, "Exception occurred when splitting string, " + "setting C_J_stability_vector to null");
333  C_J_stability_vector = null;
334  }
335 
336  Log.d(DEBUG_TAG, "Finished reading C_J_stability_vector.dat");
337  }
338 
339  // Read in C_J, the Greedily selected parameters
340  {
341  BufferedReader reader = m.getBufReader("C_J.dat");
342 
343  C_J = new Vector<double[]>(0);
344  if (C_J_stability_vector != null) {
345 
346  String line = reader.readLine();
347  reader.close();
348  reader = null;
349  String[] tokens = line.split(" ");
350 
351  int count = 0;
352  int np = sys.getParams().getNumParams();
353  for (int i = 0; i < C_J_stability_vector.length; i++) {
354  C_J.add(new double[np]);
355  for (int j = 0; j < np; j++) {
356  C_J.get(i)[j] = Double.parseDouble(tokens[count]);
357  count++;
358  }
359  }
360  }
361 
362  Log.d(DEBUG_TAG, "Finished reading C_J.dat");
363  }
364 
365  // Read in SCM_UB_vectors
366  {
367  BufferedReader reader = m.getBufReader("SCM_UB_vectors.dat");
368 
369  if (C_J_stability_vector != null) {
370 
371  String line = reader.readLine();
372  reader.close();
373  reader = null;
374  String[] tokens = line.split(" ");
375 
376  int count = 0;
377  // Resize SCM_UB_vectors based on C_J_stability_vector and Q_a
378  SCM_UB_vectors = new double[C_J_stability_vector.length][getQa()];
379  for (int i = 0; i < SCM_UB_vectors.length; i++) {
380  for (int j = 0; j < getQa(); j++) {
381  SCM_UB_vectors[i][j] = Double.parseDouble(tokens[count]);
382  count++;
383  }
384  }
385  }
386 
387  Log.d(DEBUG_TAG, "Finished reading SCM_UB_vectors.dat");
388  }
389 
390  }
391 
395  private List<Integer> getSorted_CJ_Indices() {
396 
397  int J = C_J.size();
398 
399  LinkedHashMap<Double, Integer> dist_from_mu = new LinkedHashMap<Double, Integer>(J);
400 
401  for (int j = 0; j < J; j++) {
402  double dist = param_dist(get_current_parameters(), C_J.get(j));
403  dist_from_mu.put(dist, j);
404  }
405 
406  List<Map.Entry<Double, Integer>> list = new LinkedList<Map.Entry<Double, Integer>>(dist_from_mu.entrySet());
407  Collections.sort(list, new Comparator<Map.Entry<Double, Integer>>() {
408  public int compare(Map.Entry<Double, Integer> o1, Map.Entry<Double, Integer> o2) {
409  return o1.getKey().compareTo(o2.getKey());
410  /*
411  * return ((Comparable<?>) ((Map.Entry) (o1)).getKey())
412  * .compareTo(((Map.Entry) (o2)).getKey());
413  */
414  }
415  });
416 
417  // Create a sorted list of values to return
418  List<Integer> result = new LinkedList<Integer>();
419  for (Map.Entry<Double, Integer> e : list) {
420  result.add(e.getValue());
421  }
422  return result;
423  }
424 
425 }
Utility helper class from rbAppMIT included for compatibility of rbAppMIT model loading.
Definition: GetPot.java:31
This class provides the Online stage for the reduced basis method for elliptic steady state problems...
Definition: RBSystem.java:54
double get_SCM_LB()
void readConfiguration(AModelManager m)
double get_SCM_UB()
Evaluate the SCM upper bound for current_parameters.
This class implements the Online stage of the Successive Constraint Method for coercive problems...
double thetaQa(int q)
static double param_dist(double[] mu_1, double[] mu_2)
This class serves as base class for accessing various types of models at different locations...
void reload_current_parameters()
Reload from saved_parameters.
Provides a Log class imitating the Android Log class when not used on android systems.
Definition: Log.java:11
RBSCMSystem(RBSystem sys)
double[] C_J_stability_vector
The values of the stability factor at the greedily selected parameters.
static final String parameters_filename
Inherited from the rbAppMIT models to read the model parameters.
Definition: Const.java:13
double[] get_current_parameters()
Exception imported from rbappmit.
Class with constants used throughout JRB.
Definition: Const.java:9
void loadOfflineData(AModelManager m)
Read in the stored data from the specified URL in order to initialize the SCM.
void save_current_parameters()
Save current_parameters in saved_parameters.
void get_current_parameters_from_C_J(int index)
Load the current_parameters from the set C_J.