Advertisement
Guest User

Untitled

a guest
Nov 18th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. Double Engine::optimise(adolc::CallBack& objective,
  2.     vector<Double>& start_values, vector<Double>& lower_bounds,
  3.     vector<Double>& upper_bounds, int& convergence, int& max_iterations,
  4.     int& max_evaluations, Double gradient_tolerance, double **out_hessian,
  5.     int untransformed_hessians, Double step_size) {
  6.  
  7.   // Variables
  8.   unsigned parameter_count = start_values.size();
  9.   double obj_score = 0.0;
  10.   adouble aobj_score = 0.0;
  11.   Double penalty = 0.0;
  12.   vector<double> scaled_candidate_values(parameter_count, 0.0);
  13.   vector<double> gradient_values(parameter_count, 0.0);
  14.  
  15.   /**
  16.    * Validate our values, bounds etc
  17.    */
  18.   if (lower_bounds.size() != parameter_count)
  19.     LOG_CODE_ERROR("lower_bounds.size() != parameter_count")
  20.   if (upper_bounds.size() != parameter_count)
  21.     LOG_CODE_ERROR("upper_bounds.size() != parameter_count")
  22.  
  23.   // Create our Minimiser
  24.   FMM fmm(parameter_count, max_evaluations, max_iterations, gradient_tolerance.value());
  25.  
  26.   vector<adouble> candidates(parameter_count, 0.0);
  27.   vector<adouble> scaled_candidates(parameter_count, 0.0);
  28.  
  29.   /**
  30.    * Scale our start values
  31.    */
  32.   cout << "Start Values (scaled): ";
  33.   for (unsigned i = 0; i < start_values.size(); ++i) {
  34.     if (start_values[i] < lower_bounds[i])
  35.       LOG_CODE_ERROR("start_values[i] < lower_bounds[i]");
  36.     if (start_values[i] > upper_bounds[i])
  37.       LOG_CODE_ERROR("start_values[i] > upper_bounds[i]");
  38.  
  39.  
  40.     if (dc::IsEqual(lower_bounds[i], upper_bounds[i]))
  41.       scaled_candidates[i] = 0.0;
  42.     else
  43.       scaled_candidates[i] = math::scale_value(start_values[i], lower_bounds[i], upper_bounds[i]);
  44.     cout << scaled_candidates[i] << ", ";
  45.   }
  46.   cout << endl;
  47.  
  48.   // Loop through our Minimiser now
  49.   while (fmm.getResult() >= 0) {
  50.     // Do we need to evaluate objective function again?
  51.     if ((fmm.getResult() == 0) || (fmm.getResult() == 2)) {
  52.       cout << "About to trace the objective (model)" << endl;
  53.       trace_on(0);
  54.  
  55.       // declare our dependent variables
  56.       for (unsigned i = 0; i < candidates.size(); ++i)
  57.         scaled_candidates[i] <<= scaled_candidates[i].value();
  58.  
  59.       // Reset Variables
  60.       penalty = 0.0;
  61.  
  62.       // unscale candidates
  63.       cout << "candidates (unscaled): ";
  64.       for (unsigned i = 0; i < parameter_count; ++i) {
  65.         if (dc::IsEqual(lower_bounds[i], upper_bounds[i]))
  66.           candidates[i] = lower_bounds[i];
  67.         else
  68.           candidates[i] = math::unscale_value(scaled_candidates[i], penalty, lower_bounds[i], upper_bounds[i]);
  69.         cout << candidates[i] << ", ";
  70.       }
  71.       cout << endl;
  72.  
  73.       cout << "Running Model: Start -->";
  74.       aobj_score = objective(candidates);
  75.       cout << " End" << endl;
  76.       aobj_score += penalty; // penalty for breaking bounds
  77.       aobj_score >>= obj_score;
  78.       trace_off();
  79.       cout << "Finished objective function call with score = " << obj_score << " (inc Penalty: " << penalty << ")" << endl;
  80.     }
  81.  
  82.     // Gradient Required
  83.     // This will loop through each variable changing it once
  84.     // to see how the other variables change.
  85.     // There-by generating our co-variance
  86.     if (fmm.getResult() >= 1) { // 1 = Gradient Required
  87.       cout << "About to eval gradient" << endl;
  88.  
  89.       double* adolc_x = new double[parameter_count];
  90.       double* adolc_g = new double[parameter_count];
  91.  
  92.       cout << "adolc_x: ";
  93.       for (unsigned i = 0; i < parameter_count; ++i) {
  94.         adolc_x[i] = scaled_candidates[i].value();
  95.         cout << adolc_x[i] << ", ";
  96.       }
  97.       cout << endl;
  98.  
  99.       int g_status = gradient(0, parameter_count, adolc_x, adolc_g);
  100. //      double one = 1.0;
  101. //      int g_status = fos_reverse(0, 1, parameter_count, &one, adolc_g);
  102.       cout << "Finished gradient call with status: " << g_status << endl;
  103.  
  104.       cout << "gradient: ";
  105.       for (unsigned i = 0; i < parameter_count; ++i) {
  106.         gradient_values[i] = adolc_g[i];
  107.         cout << gradient_values[i] << ", ";
  108.       }
  109.       cout << endl;
  110.  
  111.       delete [] adolc_x;
  112.       delete [] adolc_g;
  113.       // Gradient Finished
  114.     }
  115.     /**
  116.      * Scale our candidates in to candidate_values so we can
  117.      * pass them to fmin() which will build use a new set of
  118.      * scaled candidates.
  119.      *
  120.      * Then we assign them back so they can be unscaled during
  121.      * the trace
  122.      */
  123.     cout << "before: ";
  124.     for (unsigned i = 0; i < candidates.size(); ++i) {
  125.       scaled_candidate_values[i] = scaled_candidates[i].value();
  126.       cout << scaled_candidate_values[i] << ",";
  127.     }
  128.     cout << endl;
  129.  
  130.     fmm.fMin(scaled_candidate_values, obj_score, gradient_values);
  131.  
  132.     cout << "after: ";
  133.     for (unsigned i = 0; i < candidates.size(); ++i) {
  134.       scaled_candidates[i] = scaled_candidate_values[i];
  135.       cout << scaled_candidates[i] << ", ";
  136.     }
  137.     cout << endl;
  138.     cout << "FMM score: " << obj_score << endl;
  139.     cout << "FMM Result: " << fmm.getResult() << endl;
  140.   }
  141.  
  142.   if (fmm.getResult() == -3)
  143.     cout << "Convergence Unclear" << endl;
  144.   else if (fmm.getResult() == -2)
  145.     cout << "Max Evaluations" << endl;
  146.   else if (fmm.getResult() == -1)
  147.     cout << "BAM CONVERGENCE" << endl;
  148.   else
  149.     cout << "UNKNOWN RETURN VALUE: " << fmm.getResult() << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement