JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
RBnSCMCSystem.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.LinkedHashMap;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Vector;
17 
18 import org.apache.commons.math.complex.Complex;
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 
36 public class RBnSCMCSystem extends RBSCMSystem {
37 
39  super(sys);
40  }
41 
42  // Logging tag
43  private static final String DEBUG_TAG = "RBnSCMCSystem";
44 
45  private double[] B_min;
46  private double[] B_max;
47  private int n_mubar;
48  private int[] n_muhat;
49  private Vector<double[]> mu_bar;
50  private Vector<double[]>[] mu_hat;
51  private double[] beta_bar;
52  private double[][] beta_hat;
53  private double[][][] zval;
54 
60  @SuppressWarnings("unchecked")
61  public void loadOfflineData(AModelManager m) throws IOException, InconsistentStateException {
62 
63  BufferedReader reader = m.getBufReader("SCMdata.dat");
64 
65  String line = reader.readLine();
66  String[] tokens = line.split(" ");
67 
68  int count = 0;
69 
70  B_min = new double[getQa()];
71  B_max = new double[getQa()];
72  for (int i = 0; i < B_min.length; i++) {
73  B_max[i] = Double.parseDouble(tokens[count]);
74  B_min[i] = -B_max[i];
75  count++;
76  }
77 
78  n_mubar = Integer.parseInt(tokens[count]);
79  count++;
80 
81  int np = sys.getParams().getNumParams();
82  mu_bar = new Vector<double[]>(0);
83  for (int i = 0; i < n_mubar; i++) {
84  mu_bar.add(new double[np]);
85  for (int j = 0; j < np; j++) {
86  mu_bar.get(i)[j] = Double.parseDouble(tokens[count]);
87  count++;
88  }
89  }
90 
91  beta_bar = new double[n_mubar];
92  for (int i = 0; i < n_mubar; i++) {
93  beta_bar[i] = Double.parseDouble(tokens[count]);
94  count++;
95  }
96 
97  mu_hat = (Vector<double[]>[]) new Vector<?>[n_mubar];
98  n_muhat = new int[n_mubar];
99  beta_hat = new double[n_mubar][];
100  zval = new double[n_mubar][][];
101  for (int i = 0; i < n_mubar; i++) {
102  n_muhat[i] = Integer.parseInt(tokens[count]);
103  count++;
104  beta_hat[i] = new double[n_muhat[i]];
105  zval[i] = new double[n_muhat[i]][getQa() * 2];
106 
107  mu_hat[i] = new Vector<double[]>(0);
108  for (int j = 0; j < n_muhat[i]; j++) {
109  mu_hat[i].add(new double[np]);
110  for (int k = 0; k < np; k++) {
111  mu_hat[i].get(j)[k] = Double.parseDouble(tokens[count]);
112  count++;
113  }
114  }
115 
116  for (int j = 0; j < n_muhat[i]; j++) {
117  beta_hat[i][j] = Double.parseDouble(tokens[count]);
118  count++;
119  }
120 
121  for (int k = 0; k < getQa() * 2; k++)
122  for (int j = 0; j < n_muhat[i]; j++) {
123  zval[i][j][k] = Double.parseDouble(tokens[count]);
124  count++;
125  }
126  }
127 
128  reader.close();
129 
130  Log.d(DEBUG_TAG, "Finished reading SCMdata.dat");
131 
132  }
133 
137  public double get_SCM_LB() {
138  // return 0.01;
139 
140  double min_J_obj = 0.;
141  double[] min_Jlocal_obj = new double[n_mubar];
142 
143  // Sort the indices of mu_bar based on distance from current_parameters
144  List<Integer> sortedmubarIndices = getSorted_CJ_Indices(mu_bar);
145  int icount = 0;
146  // while ((min_J_obj<=0) && (icount < sortedmubarIndices.size())){
147  while ((min_J_obj <= 0) && (icount < sortedmubarIndices.size())) {
148  int imubar = sortedmubarIndices.get(icount);
149 
150  // First, declare the constraints
151  Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
152 
153  // Add bounding box constraints for the get_Q_a() variables
154  for (int q = 0; q < getQa(); q++) {
155  double[] index = new double[getQa() * 2];
156  index[q] = 1.;
157 
158  constraints.add(new LinearConstraint(index, Relationship.GEQ, B_min[q] / beta_bar[imubar]));
159  constraints.add(new LinearConstraint(index, Relationship.LEQ, B_max[q] / beta_bar[imubar]));
160 
161  index[q] = 0.;
162  index[q + getQa()] = 1.;
163 
164  constraints.add(new LinearConstraint(index, Relationship.GEQ, B_min[q] / beta_bar[imubar]));
165  constraints.add(new LinearConstraint(index, Relationship.LEQ, B_max[q] / beta_bar[imubar]));
166  }
167 
168  // Save the current_parameters since we'll change them in the loop
169  // below
171 
172  // Add the constraint rows
173  if (n_muhat[imubar] > 0) {
174  for (int imuhat = 0; imuhat < n_muhat[imubar]; imuhat++) {
175  sys.getParams().setCurrent(mu_hat[imubar].get(imuhat));
176 
177  double[] constraint_row = new double[getQa() * 2];
178  for (int q = 0; q < getQa(); q++) {
179  Complex theta_q_a = sys.complex_eval_theta_q_a(q);
180  constraint_row[q] = theta_q_a.getReal() * beta_bar[imubar];
181  constraint_row[q + getQa()] = theta_q_a.getImaginary() * beta_bar[imubar];
182  }
183 
184  constraints.add(new LinearConstraint(constraint_row, Relationship.GEQ, beta_hat[imubar][imuhat]));
185  }
186  }
187 
188  // Now load the original parameters back into current_parameters
189  // in order to set the coefficients of the objective function
191 
192  // Create objective function object
193  double[] objectiveFn = new double[getQa() * 2];
194  for (int q = 0; q < getQa(); q++) {
195  Complex theta_q_a = sys.complex_eval_theta_q_a(q);
196  objectiveFn[q] = theta_q_a.getReal() * beta_bar[imubar];
197  objectiveFn[q + getQa()] = theta_q_a.getImaginary() * beta_bar[imubar];
198  }
199  LinearObjectiveFunction f = new LinearObjectiveFunction(objectiveFn, 0.);
200 
201  try {
202  SimplexSolver solver = new SimplexSolver(1e-6);
203  RealPointValuePair opt_pair = solver.optimize(f, constraints, GoalType.MINIMIZE, false);
204  min_Jlocal_obj[icount] = opt_pair.getValue();
205  } catch (OptimizationException e) {
206  Log.e("RBSCMSYSTEM_TAG", "Optimal solution not found");
207  e.printStackTrace();
208  } catch (Exception e) {
209  Log.e("RBSCMSYSTEM_TAG", "Exception occurred during SCM_LB calculation");
210  e.printStackTrace();
211  }
212 
213  min_J_obj = min_J_obj > min_Jlocal_obj[icount] ? min_J_obj : min_Jlocal_obj[icount];
214  icount++;
215  }
216  return min_J_obj;
217  }
218 
219  public double get_SCM_UB() {
220 
221  // cheating, since betaUB is rarely good
222  double maxbetabar = beta_bar[0];
223  for (int i = 0; i < n_mubar; i++)
224  maxbetabar = maxbetabar < beta_bar[i] ? beta_bar[i] : maxbetabar;
225  return maxbetabar;
226 
227  /*
228  * double[] theta_a = new double [get_Q_a()*2]; for(int q=0;
229  * q<get_Q_a(); q++){ Complex theta_q_a = complex_eval_theta_q_a(q);
230  * theta_a[q] = theta_q_a.getReal(); theta_a[q+get_Q_a()] =
231  * theta_q_a.getImaginary(); } double betaUB = -1e8; for (int i = 0; i <
232  * n_mubar; i++){ double localbetaUB = 1e8; for (int j = 0; j <
233  * n_muhat[i]; j++){ double calbetaUB = 0.; for (int k = 0; k <
234  * get_Q_a()*2; k++) calbetaUB += zval[i][j][k]*theta_a[k]; localbetaUB
235  * = localbetaUB > calbetaUB ? calbetaUB : localbetaUB; } betaUB =
236  * betaUB < localbetaUB ? localbetaUB : betaUB; } return betaUB;
237  */
238 
239  }
240 
241  private List<Integer> getSorted_CJ_Indices(Vector<double[]> C_J) {
242 
243  int J = C_J.size();
244 
245  LinkedHashMap<Double, Integer> dist_from_mu = new LinkedHashMap<Double, Integer>(J);
246 
247  for (int j = 0; j < J; j++) {
248  double dist = param_dist(get_current_parameters(), C_J.get(j));
249  dist_from_mu.put(dist, j);
250  }
251 
252  List<Map.Entry<Double, Integer>> list = new LinkedList<Map.Entry<Double, Integer>>(dist_from_mu.entrySet());
253  Collections.sort(list, new Comparator<Map.Entry<Double, Integer>>() {
254  public int compare(Map.Entry<Double, Integer> o1, Map.Entry<Double, Integer> o2) {
255  return o1.getKey().compareTo(o2.getKey());
256  /*
257  * return ((Comparable<?>) ((Map.Entry<Double,Integer>)
258  * (o1)).getKey()) .compareTo(((Map.Entry<Double,Integer>)
259  * (o2)).getKey());
260  */
261  }
262  });
263 
264  // Create a sorted list of values to return
265  List<Integer> result = new LinkedList<Integer>();
266  for (Map.Entry<Double, Integer> entry : list) {
267  result.add(entry.getValue());
268  }
269 
270  return result;
271  }
272 
273 }
This class provides the Online stage for the reduced basis method for elliptic steady state problems...
Definition: RBSystem.java:54
double get_SCM_LB()
TODO create local double[] currentParam field and remove save_/restore_params fcns.
RBnSCMCSystem(RBSystem sys)
This class implements the Online stage of the Successive Constraint Method for coercive problems...
Reduced basis SCM system.
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
void loadOfflineData(AModelManager m)
double[] get_current_parameters()
Exception imported from rbappmit.
void save_current_parameters()
Save current_parameters in saved_parameters.