simple_optimization_task.h
Go to the documentation of this file.
00001 
00027 #ifndef EXAMPLES_SIMPLE_OPTIMIZATION_TASK_H_
00028 #define EXAMPLES_SIMPLE_OPTIMIZATION_TASK_H_
00029 
00030 #include <stomp_core/task.h>
00031 
00032 namespace stomp_core_examples
00033 {
00034 
00036 
00037 class SimpleOptimizationTask: public stomp_core::Task
00038 {
00039 public:
00046   SimpleOptimizationTask(const Eigen::MatrixXd& parameters_bias,
00047             const std::vector<double>& bias_thresholds,
00048             const std::vector<double>& std_dev):
00049               parameters_bias_(parameters_bias),
00050               bias_thresholds_(bias_thresholds),
00051               std_dev_(std_dev)
00052   {
00053 
00054     // generate smoothing matrix
00055     int num_timesteps = parameters_bias.cols();
00056     stomp_core::generateSmoothingMatrix(num_timesteps,1.0,smoothing_M_);
00057     srand(time(0));
00058 
00059   }
00060 
00062 
00073   bool generateNoisyParameters(const Eigen::MatrixXd& parameters,
00074                                std::size_t start_timestep,
00075                                std::size_t num_timesteps,
00076                                int iteration_number,
00077                                int rollout_number,
00078                                Eigen::MatrixXd& parameters_noise,
00079                                Eigen::MatrixXd& noise) override
00080   {
00081     double rand_noise;
00082     for(std::size_t d = 0; d < parameters.rows(); d++)
00083     {
00084       for(std::size_t t = 0; t < parameters.cols(); t++)
00085       {
00086         rand_noise = static_cast<double>(rand()%RAND_MAX)/static_cast<double>(RAND_MAX - 1); // 0 to 1
00087         rand_noise = 2*(0.5 - rand_noise);
00088         noise(d,t) =  rand_noise*std_dev_[d];
00089       }
00090     }
00091 
00092     parameters_noise = parameters + noise;
00093 
00094     return true;
00095   }
00096 
00107   bool computeCosts(const Eigen::MatrixXd& parameters,
00108                     std::size_t start_timestep,
00109                     std::size_t num_timesteps,
00110                     int iteration_number,
00111                     Eigen::VectorXd& costs,
00112                     bool& validity) override
00113   {
00114     return computeNoisyCosts(parameters,start_timestep,num_timesteps,iteration_number,-1,costs,validity);
00115   }
00116 
00128   bool computeNoisyCosts(const Eigen::MatrixXd& parameters,
00129                          std::size_t start_timestep,
00130                          std::size_t num_timesteps,
00131                          int iteration_number,
00132                          int rollout_number,
00133                          Eigen::VectorXd& costs,
00134                          bool& validity) override
00135   {
00136     costs.setZero(num_timesteps);
00137     double diff;
00138     double cost = 0.0;
00139     validity = true;
00140 
00141     for(std::size_t t = 0u; t < num_timesteps; t++)
00142     {
00143       cost = 0;
00144       for(std::size_t d = 0u; d < parameters.rows() ; d++)
00145       {
00146 
00147         diff = std::abs(parameters(d,t) - parameters_bias_(d,t));
00148         if( diff > std::abs(bias_thresholds_[d]))
00149         {
00150           cost += diff;
00151           validity = false;
00152         }
00153       }
00154 
00155       costs(t) = cost;
00156     }
00157 
00158     return true;
00159   }
00160 
00172   bool filterParameterUpdates(std::size_t start_timestep,
00173                               std::size_t num_timesteps,
00174                               int iteration_number,
00175                               const Eigen::MatrixXd& parameters,
00176                               Eigen::MatrixXd& updates) override
00177   {
00178     return smoothParameterUpdates(start_timestep,num_timesteps,iteration_number,updates);
00179   }
00180 
00181 
00182 protected:
00183 
00192   bool smoothParameterUpdates(std::size_t start_timestep,
00193                               std::size_t num_timesteps,
00194                               int iteration_number,
00195                               Eigen::MatrixXd& updates)
00196   {
00197 
00198     for(auto d = 0u; d < updates.rows(); d++)
00199     {
00200       updates.row(d).transpose() = smoothing_M_*(updates.row(d).transpose());
00201     }
00202 
00203     return true;
00204   }
00205 
00206 protected:
00207 
00208   Eigen::MatrixXd parameters_bias_;          
00209   std::vector<double> bias_thresholds_; 
00210   std::vector<double> std_dev_;         
00211   Eigen::MatrixXd smoothing_M_;         
00212 };
00213 
00214 }
00215 
00216 
00217 #endif /* EXAMPLES_SIMPLE_OPTIMIZATION_TASK_H_ */


stomp_core
Author(s): Jorge Nicho
autogenerated on Sat Jun 8 2019 19:23:57