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;
17 import java.util.Vector;
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;
42 private static final String DEBUG_TAG =
"RBSCMSystem";
52 private double[] B_min;
53 private double[] B_max;
58 private Vector<double[]> C_J;
69 private double[][] SCM_UB_vectors;
74 private double[] saved_parameters;
81 return sys.thetaQa(q);
92 double min_J_obj = 0.;
97 Collection<LinearConstraint> constraints =
new ArrayList<LinearConstraint>();
100 for (
int q = 0; q <
getQa(); q++) {
101 double[] index =
new double[
getQa()];
104 constraints.add(
new LinearConstraint(index, Relationship.GEQ, B_min[q]));
105 constraints.add(
new LinearConstraint(index, Relationship.LEQ, B_max[q]));
109 List<Integer> sortedIndices = getSorted_CJ_Indices();
116 int n_rows = Math.min(SCM_M, C_J.size());
120 for (Integer mu_index : sortedIndices) {
124 double[] constraint_row =
new double[
getQa()];
125 for (
int q = 0; q <
getQa(); q++) {
126 constraint_row[q] = sys.thetaQa(q);
129 constraints.add(
new LinearConstraint(constraint_row, Relationship.GEQ,
144 double[] objectiveFn =
new double[
getQa()];
145 for (
int q = 0; q <
getQa(); q++) {
146 objectiveFn[q] = sys.thetaQa(q);
148 LinearObjectiveFunction f =
new LinearObjectiveFunction(objectiveFn, 0.);
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");
156 }
catch (Exception e) {
157 Log.e(
"DEBUG_TAG",
"Exception occurred during SCM_LB calculation");
161 Log.d(DEBUG_TAG,
"SCM val = " + min_J_obj);
171 List<Integer> sortedIndices = getSorted_CJ_Indices();
177 double min_J_obj = 0.;
178 int n_rows = Math.min(SCM_M, C_J.size());
180 for (Iterator<Integer> it = sortedIndices.iterator(); it.hasNext();) {
181 Integer mu_index = (Integer) it.next();
185 double[] UB_vector = SCM_UB_vectors[mu_index];
188 for (
int q = 0; q <
getQa(); q++) {
189 J_obj += sys.thetaQa(q) * UB_vector[q];
192 if ((count == 1) || (J_obj < min_J_obj)) {
208 public static double param_dist(
double[] mu_1,
double[] mu_2) {
212 for (
int i = 0; i < mu_1.length; i++) {
213 sum += Math.pow(mu_1[i] - mu_2[i], 2.);
216 return Math.sqrt(sum);
223 sys.getParams().setCurrent(C_J.get(index));
230 saved_parameters = sys.getParams().getCurrent().clone();
237 sys.getParams().setCurrent(saved_parameters);
244 return sys.getParams().getCurrent();
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);
264 SCM_M = infile.call(
"SCM_M", 0);
266 Log.d(DEBUG_TAG,
"RBSCMSystem parameters from " + Const.parameters_filename +
":");
267 Log.d(DEBUG_TAG,
"SCM_M: " + SCM_M);
280 BufferedReader reader = m.getBufReader(
"B_min.dat");
282 String line = reader.readLine();
283 String[] tokens = line.split(
" ");
287 B_min =
new double[
getQa()];
288 for (
int i = 0; i < B_min.length; i++) {
289 B_min[i] = Double.parseDouble(tokens[i]);
292 Log.d(DEBUG_TAG,
"Finished reading B_min.dat");
297 BufferedReader reader = m.getBufReader(
"B_max.dat");
299 String line = reader.readLine();
300 String[] tokens = line.split(
" ");
302 B_max =
new double[
getQa()];
303 for (
int i = 0; i < B_max.length; i++) {
304 B_max[i] = Double.parseDouble(tokens[i]);
309 Log.d(DEBUG_TAG,
"Finished reading B_max.dat");
314 BufferedReader reader = m.getBufReader(
"C_J_stability_vector.dat");
316 String line = reader.readLine();
321 String[] tokens = line.split(
" ");
323 if ((tokens.length == 1) && (tokens[0] ==
"")) {
327 for (
int i = 0; i < C_J_stability_vector.length; i++) {
331 }
catch (Exception e) {
332 Log.d(DEBUG_TAG,
"Exception occurred when splitting string, " +
"setting C_J_stability_vector to null");
336 Log.d(DEBUG_TAG,
"Finished reading C_J_stability_vector.dat");
341 BufferedReader reader = m.getBufReader(
"C_J.dat");
343 C_J =
new Vector<double[]>(0);
346 String line = reader.readLine();
349 String[] tokens = line.split(
" ");
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]);
362 Log.d(DEBUG_TAG,
"Finished reading C_J.dat");
367 BufferedReader reader = m.getBufReader(
"SCM_UB_vectors.dat");
371 String line = reader.readLine();
374 String[] tokens = line.split(
" ");
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]);
387 Log.d(DEBUG_TAG,
"Finished reading SCM_UB_vectors.dat");
395 private List<Integer> getSorted_CJ_Indices() {
399 LinkedHashMap<Double, Integer> dist_from_mu =
new LinkedHashMap<Double, Integer>(J);
401 for (
int j = 0; j < J; j++) {
403 dist_from_mu.put(dist, j);
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());
418 List<Integer> result =
new LinkedList<Integer>();
419 for (Map.Entry<Double, Integer> e : list) {
420 result.add(e.getValue());
Utility helper class from rbAppMIT included for compatibility of rbAppMIT model loading.
This class provides the Online stage for the reduced basis method for elliptic steady state problems...
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...
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.
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.
double[] get_current_parameters()
Exception imported from rbappmit.
Class with constants used throughout JRB.
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.