Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cost Opimization #29

Merged
merged 17 commits into from
Aug 15, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Config file can be changed without rebuilding, allowing for iterative…
… testing
walkeri committed Aug 15, 2018
commit c06960c54f42bfd7e28fcdda6b720e33e7b919a0
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ message(WARNING ${LIBRARIES})

# Add the sources to the executables
add_library(Routes STATIC ${HS}
${SOURCES})
${SOURCES} src/routes-lib/configure/configure.cpp src/routes-lib/configure/configure.h)

IF (WIN32)

@@ -80,7 +80,7 @@ FILE(GLOB_RECURSE HS ${CMAKE_SOURCE_DIR}/src/routes-exec/*.h*)
FILE(GLOB_RECURSE SOURCES ${CMAKE_SOURCE_DIR}/src/routes-exec/*.cpp)

add_executable(Routes-Exec ${HS}
${SOURCES})
${SOURCES} src/routes-lib/configure/configure.cpp src/routes-lib/configure/configure.h)

add_dependencies(Routes-Exec Routes)

@@ -112,7 +112,7 @@ FILE(GLOB_RECURSE HS ${CMAKE_SOURCE_DIR}/src/routes-server/*.h*)
FILE(GLOB_RECURSE SOURCES ${CMAKE_SOURCE_DIR}/src/routes-server/*.cpp)

add_executable(Routes-Server ${HS}
${SOURCES})
${SOURCES} src/routes-lib/configure/configure.cpp src/routes-lib/configure/configure.h)

add_dependencies(Routes-Server Routes)

@@ -146,7 +146,7 @@ FILE(GLOB_RECURSE HS ${CMAKE_SOURCE_DIR}/src/routes-tests/*.h*)
FILE(GLOB_RECURSE SOURCES ${CMAKE_SOURCE_DIR}/src/routes-tests/*.cpp)

add_executable(Routes-Tests ${HS}
${SOURCES})
${SOURCES} src/routes-lib/configure/configure.cpp src/routes-lib/configure/configure.h)

add_dependencies(Routes-Tests Routes)

17 changes: 17 additions & 0 deletions params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"reload": 0,

"routes": {
"population-size": 1000,
"num-generations": 300
},

"population": {
"initial-sigma-divisor": 10.0,
"initial-sigma-xy": 2500.0,
"step-dampening": 0.75,
"alpha": 1.5,
"num-sample-threads": 10,
"num-route-workers": 100
}
}
77 changes: 77 additions & 0 deletions src/routes-lib/configure/configure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// Created by isaac on 8/14/18.
//

#include "configure.h"

Configuration Configure::_config;

Configure::Configure() {

loadConfig();

}

void Configure::loadConfig() {


boost::property_tree::ptree root;

boost::property_tree::read_json(FILE_PATH, root);

// if 0, then reload, if 1 then don't
int reload = root.get<int>("reload", 0);

int population_size = root.get<int>("routes.population-size", 0);
int num_generations = root.get<int>("routes.num-generations", 0);
float initial_sigma_divisor = root.get<float>("population.initial-sigma-divisor", 0);
float initial_sigma_xy = root.get<float>("population.initial-sigma-xy", 0);
float step_dampening = root.get<float>("population.step-dampening", 0);
float alpha = root.get<float>("population.alpha", 0);
int num_sample_threads = root.get<int>("population.num-sample-threads", 0);
int num_route_workers = root.get<int>("population.num-route-workers", 0);

_config = {reload, population_size, num_generations,
initial_sigma_divisor, initial_sigma_xy,
step_dampening, alpha, num_sample_threads, num_route_workers};



}

int Configure::getReload() {
return _config.reload;
}

int Configure::getPopulationSize() {
return _config.population_size;
}

int Configure::getNumGenerations() {
return _config.num_generations;
}

float Configure::getInitialSigmaDivisor() {
return _config.initial_sigma_divisor;
}

float Configure::getInitialSigmaXY() {
return _config.initial_sigma_xy;
}

float Configure::getStepDampening() {
return _config.step_dampening;
}

float Configure::getAlpha() {
return _config.alpha;
}

int Configure::getNumSampleThreads() {
return _config.num_sample_threads;
}

int Configure::getNumRouteWorkers() {
return _config.num_route_workers;
}

176 changes: 176 additions & 0 deletions src/routes-lib/configure/configure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//
// Created by isaac on 8/14/18.
//

#ifndef ROUTES_CONFIGURE_H
#define ROUTES_CONFIGURE_H

#ifdef _MSC_VER
#include <boost/config/compiler/visualc.hpp>
#endif
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <cassert>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>

#define FILE_PATH "../params.json"


#endif //ROUTES_CONFIGURE_H

/**
* A struct for representing the parameter information.
*/
struct Configuration {

/**
* This is 0 if the data should be reloaded, and 1 if not
*/
int reload;

/**
* The default size of the population for calculating a route
*/
int population_size;

/**
* The default number of generations that the population should be bred
*/
int num_generations;

/**
* This value is used to help calculate the initial step size of the population.
* and the Z sigma is the max elevation delta / INITIAL_SIGMA_DIVISOR.
*/
float initial_sigma_divisor;

/**
* This serves as the initial value for the X and Y of all the points for sigma.
* We use 5km as a pretty tight bounding around the straight line (initial mean)
*/
float initial_sigma_xy;

/**
* Represents the dampening parameter for the step size. This value should be close to 1
*/
float step_dampening;

/**
* The interval multiplier for the square root of _genome_size * 3 for the indicator function.
*/
float alpha;

/**
* The number of threads that are used to sample from the multivariate normal distribution
*/
int num_sample_threads;

/**
* The number of divisions that the route is split up into for evaluation on the GPU
*/
int num_route_workers;


};

class Configure {

public:

/**
* Construct the configuration for this run.
*/
Configure();

/**
* Gets the reload flag
*
* @return
* returns the reload flag
*/
int getReload();

/**
* Gets the population size
*
* @return
* returns the population size
*
*/
int getPopulationSize();

/**
* Gets the number of generations
*
* @return
* returns the number of generations
*/
int getNumGenerations();

/**
* Gets the initial Sigma Divisor for calculating the step size
*
* @return
* returns the initial sigma divisor
*/
float getInitialSigmaDivisor();

/**
* Gets the initial sigma for calculating the initial X and Y values for sigma
*
* @return
* returns the initial sigma xy
*/
float getInitialSigmaXY();

/**
* Gets the dampening parameter for step size
*
* @return
* returns the step dampening parameter
*/
static float getStepDampening();

/**
* Gets the interval multiplier for the indicator function
*
* @return
* returns alpha
*/
float getAlpha();

/**
* Gets the number of threads for sampling the multivariate normal distribution
*
* @return
* returns the number of sample threads
*/
int getNumSampleThreads();

/**
* Gets the number of workers for GPU eval
*
* @return
* returns the number of route workers
*/
int getNumRouteWorkers();

private:

/**
* Loads the params json file
*/
void loadConfig();

/**
* The loaded configuration
*/
static Configuration _config;

};


47 changes: 26 additions & 21 deletions src/routes-lib/genetics/population.cpp
Original file line number Diff line number Diff line change
@@ -4,8 +4,11 @@

#include "population.h"

Population::Population(int pop_size, glm::vec4 start, glm::vec4 dest, const ElevationData& data) : _pop_size(pop_size), _start(start),
_dest(dest), _direction(_dest - _start), _data(data) {
Population::Population(int pop_size, glm::vec4 start, glm::vec4 dest, const ElevationData& data, Configure conf) : _pop_size(pop_size), _start(start),
_dest(dest), _direction(_dest - _start), _data(data), _reload(conf.getReload()), _initial_sigma_divisor(conf.getInitialSigmaDivisor()),
_initial_sigma_xy(conf.getInitialSigmaXY()), _step_dampening(conf.getStepDampening()), _alpha(conf.getAlpha()), _num_sample_threads(conf.getNumSampleThreads()),
_num_route_workers(conf.getNumRouteWorkers()) {


// Figure out how many points we need for this route
calcGenomeSize();
@@ -25,7 +28,7 @@ Population::Population(int pop_size, glm::vec4 start, glm::vec4 dest, const Elev
// We also make sure it is a multiple of workers
glm::dvec2 cropped_size = data.getCroppedSizeMeters();
_num_evaluation_points = glm::max((int)ceil(glm::max(cropped_size.x / METERS_TO_POINT_CONVERSION,
cropped_size.y / METERS_TO_POINT_CONVERSION) / (float)NUM_ROUTE_WORKERS) * NUM_ROUTE_WORKERS, 2400);
cropped_size.y / METERS_TO_POINT_CONVERSION) / (float)_num_route_workers) * _num_route_workers, 2400);

std::cout << "Using " << _num_evaluation_points << " points of evaluation" << std::endl;
_num_evaluation_points_1 = (float)_num_evaluation_points - 1.0f;
@@ -43,12 +46,13 @@ Population::Population(int pop_size, glm::vec4 start, glm::vec4 dest, const Elev
// Create the best sample vector
_best_samples = std::vector<Eigen::VectorXf>((size_t)_mu);


}

Population::~Population() {

// Delete the sample generators
for (int i = 0; i < NUM_SAMPLE_THREADS; i++)
for (int i = 0; i < _num_sample_threads; i++)
delete _sample_gens[i];

}
@@ -201,15 +205,15 @@ void Population::evaluateCost(const Pod& pod, std::string objectiveType) {
kernel.setArgs(_data.getOpenCLImage(), _opencl_individuals.get_buffer(), _genome_size + 2,
MAX_SLOPE_GRADE, pod.minCurveRadius(), EXCAVATION_DEPTH, _data_size.x,
_data_size.y, _opencl_binomials.get_buffer(),
_num_evaluation_points_1, _num_evaluation_points / NUM_ROUTE_WORKERS, _data_origin.x, _data_origin.y, glm::length(_direction));
_num_evaluation_points_1, _num_evaluation_points / _num_route_workers, _data_origin.x, _data_origin.y, glm::length(_direction));

// Upload the data
boost::compute::copy(_individuals.begin(),_individuals.end(), _opencl_individuals.begin(), queue);

// Execute the 2D kernel with a work size of NUM_ROUTE_WORKERS. NUM_ROUTE_WORKERS threads will work on a single individual
kernel.execute2D(glm::vec<2, size_t>(0, 0),
glm::vec<2, size_t>(_pop_size, NUM_ROUTE_WORKERS),
glm::vec<2, size_t>(1, NUM_ROUTE_WORKERS));
glm::vec<2, size_t>(_pop_size, _num_route_workers),
glm::vec<2, size_t>(1, _num_route_workers));

// Download the data
boost::compute::copy(_opencl_individuals.begin(), _opencl_individuals.end(), _individuals.begin(), queue);
@@ -244,6 +248,7 @@ double Population::totalFitness(glm::vec4 costs) {

}


void Population::calcGenomeSize() {

// The genome size has a square root relationship with the length of the route
@@ -260,7 +265,7 @@ void Population::determineEvalPoints() {
// We also make sure it is a multiple of workers
_num_evaluation_points = glm::max((int)ceil(glm::max(_data_size.x / METERS_TO_POINT_CONVERSION,
_data_size.y / METERS_TO_POINT_CONVERSION)
/ (float)NUM_ROUTE_WORKERS) * NUM_ROUTE_WORKERS,
/ (float)_num_route_workers) * _num_route_workers,
2400);

_num_evaluation_points_1 = (float)_num_evaluation_points - 1.0f;
@@ -302,10 +307,10 @@ void Population::initParams() {
void Population::initSamplers() {

// Create the standard normal samplers
_sample_gens = std::vector<SampleGenerator*>(NUM_SAMPLE_THREADS);
_sample_gens = std::vector<SampleGenerator*>(_num_sample_threads);

for (int i = 0; i < NUM_SAMPLE_THREADS; i++)
_sample_gens[i] = new SampleGenerator(_genome_size * 3, _pop_size / NUM_SAMPLE_THREADS);
for (int i = 0; i < _num_sample_threads; i++)
_sample_gens[i] = new SampleGenerator(_genome_size * 3, _pop_size / _num_sample_threads);

}

@@ -394,9 +399,9 @@ void Population::calcInitialSigma() {
_sigma = Eigen::VectorXf(_genome_size * 3);

// First we figure out what the actual values should be
glm::vec3 sigma_parts = glm::vec3(INITIAL_SIGMA_XY,
INITIAL_SIGMA_XY,
(_data.getMaxElevation() - _data.getMinElevation()) / INITIAL_SIGMA_DIVISOR);
glm::vec3 sigma_parts = glm::vec3(_initial_sigma_xy,
_initial_sigma_xy,
(_data.getMaxElevation() - _data.getMinElevation()) / _initial_sigma_divisor);

// Apply it to the X Y and Z for each point
for (int i = 0; i < _genome_size; i++) {
@@ -433,10 +438,10 @@ void Population::samplePopulation() {

// Convert the _samples over to a set of glm vectors and update the population
// Do so with multiple threads
std::vector<std::thread> threads = std::vector<std::thread>(NUM_SAMPLE_THREADS);
int worker_size = _pop_size / NUM_SAMPLE_THREADS;
std::vector<std::thread> threads = std::vector<std::thread>(_num_sample_threads);
int worker_size = _pop_size / _num_sample_threads;

for (int thread = 0; thread < NUM_SAMPLE_THREADS; thread++) {
for (int thread = 0; thread < _num_sample_threads; thread++) {

threads[thread] = std::thread([this, thread, worker_size] {

@@ -461,7 +466,7 @@ void Population::samplePopulation() {
}

// Make sure all of the threads finish
for (int i = 0; i < NUM_SAMPLE_THREADS; i++)
for (int i = 0; i < _num_sample_threads; i++)
threads[i].join();

}
@@ -528,7 +533,7 @@ void Population::updatePCovar() {

// Figure out the indicator function
float indicator = 0.0f;
if (_p_sigma.norm() <= sqrtf((float)_mean.size()) * ALPHA)
if (_p_sigma.norm() <= sqrtf((float)_mean.size()) * _alpha)
indicator = 1.0;

_p_covar = discount * _p_covar + indicator * discount_comp * _mu_weight_sqrt * _mean_displacement;
@@ -557,7 +562,7 @@ void Population::updateCovar() {

// Calculate cs
float indicator = 0.0f;
if (_p_sigma.norm() * _p_sigma.norm() <= sqrtf((float)_mean.size()) * ALPHA)
if (_p_sigma.norm() * _p_sigma.norm() <= sqrtf((float)_mean.size()) * _alpha)
indicator = 1.0;

float cs = (1.0f - indicator) * _c1 * _c_covar * (2.0f - _c_covar);
@@ -573,7 +578,7 @@ void Population::updateCovar() {
void Population::updateSigma() {

// Calculate ratio between the decay and the dampening
float ratio = _c_sigma / STEP_DAMPENING;
float ratio = _c_sigma / _step_dampening;

// Get the magnitude of the sigma path
float mag = _p_sigma.norm();
62 changes: 37 additions & 25 deletions src/routes-lib/genetics/population.h
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
#include "../elevation/elevation.h"
#include "../normal/multinormal.h"
#include "../pod/pod.h"
#include "../configure/configure.h"

// Ensure that E is defined on Windows
#ifndef M_E
@@ -35,30 +36,6 @@
*/
#define LENGTH_TO_GENOME 0.0274360619f

/**
* This value is used to help calculate the initial step size of the population.
* and the Z sigma is the max elevation delta / INITIAL_SIGMA_DIVISOR.
*/
#define INITIAL_SIGMA_DIVISOR 10.0f

/**
* This serves as the initial value for the X and Y of all the points for sigma.
* We use 5km as a pretty tight bounding around the straight line (initial mean)
*/
#define INITIAL_SIGMA_XY 2500.0f

/** Represents the dampening parameter for the step size. This value should be close to 1 */
#define STEP_DAMPENING .75f

/** The interval multiplier for the square root of _genome_size * 3 for the indicator function. */
#define ALPHA 1.5f

/** The number of threads that are used to sample from the multivariate normal distribution */
#define NUM_SAMPLE_THREADS 10

/** The number of divisions that the route is split up into for evaluation on the GPU */
#define NUM_ROUTE_WORKERS 100

/**
* Individual is a convenience so that individuals can be treated as units rather than
* as a single float vector, which is how they are stored.
@@ -142,7 +119,7 @@ class Population {
* @param data
* The elevation data that this population is path-finding on
*/
Population(int pop_size, glm::vec4 start, glm::vec4 dest, const ElevationData& data);
Population(int pop_size, glm::vec4 start, glm::vec4 dest, const ElevationData& data, Configure conf);

/** Simple destructor to delete heap allocated things */
~Population();
@@ -231,6 +208,11 @@ class Population {

private:

/**
* Reloads the paramaters if needed
*/
void configureParams();

/**
* This function calculates the size of the genome.
* This is based off the length of the route that this population represent and has a square root relationship.
@@ -482,6 +464,36 @@ class Population {
*/
std::vector<std::vector<double>> _mo_fitness_over_generations;

/**
* 0: reload params
* 1: don't reload params
*/
const int _reload;

/**
* This value is used to help calculate the initial step size of the population.
* and the Z sigma is the max elevation delta / INITIAL_SIGMA_DIVISOR.
*/
const float _initial_sigma_divisor;

/**
* This serves as the initial value for the X and Y of all the points for sigma.
* We use 5km as a pretty tight bounding around the straight line (initial mean)
*/
const float _initial_sigma_xy;

/** Represents the dampening parameter for the step size. This value should be close to 1 */
const float _step_dampening;

/** The interval multiplier for the square root of _genome_size * 3 for the indicator function. */
const float _alpha;

/** The number of threads that are used to sample from the multivariate normal distribution */
const int _num_sample_threads;

/** The number of divisions that the route is split up into for evaluation on the GPU */
const int _num_route_workers;

};

#endif //ROUTES_POPULATION_H
20 changes: 17 additions & 3 deletions src/routes-lib/routes.cpp
Original file line number Diff line number Diff line change
@@ -12,9 +12,14 @@ std::vector<glm::vec2> Routes::_speeds;
std::vector<glm::vec2> Routes::_grades;
int Routes::_route_id;
std::string Routes::_solutions;
int Routes::_pop_size;
int Routes::_num_generations;
Configure Routes::_config;

std::vector<glm::vec3> Routes::calculateRoute(glm::vec2 start, glm::vec2 dest) {

configureParams();

time_t now = time(0);

std::cout << "Calculating a route\n";
@@ -33,12 +38,14 @@ std::vector<glm::vec3> Routes::calculateRoute(glm::vec2 start, glm::vec2 dest) {

// Create a pod and a population
Pod pod = Pod(DEFAULT_POD_MAX_SPEED);
Population pop = Population(POP_SIZE, glm::vec4(start_meter.x, start_meter.y, start_meter.z + 10.0, 0.0),
glm::vec4(dest_meter.x, dest_meter.y, dest_meter.z + 10.0, 0.0), data);

_config = Configure();
Population pop = Population(_pop_size, glm::vec4(start_meter.x, start_meter.y, start_meter.z + 10.0, 0.0),
glm::vec4(dest_meter.x, dest_meter.y, dest_meter.z + 10.0, 0.0), data, _config);

// Solve!
// These points will be in meters so we need to convert them
std::vector<glm::vec3> computed = Genetics::solve(pop, pod, NUM_GENERATIONS, start, dest, "single");
std::vector<glm::vec3> computed = Genetics::solve(pop, pod, _num_generations, start, dest, "single");

std::vector<glm::vec3> points = Bezier::evaluateEntireBezierCurve(computed, 100);

@@ -94,6 +101,13 @@ std::vector<glm::vec3> Routes::calculateRoute(glm::vec2 start, glm::vec2 dest) {

}

void Routes::configureParams() {

_pop_size = _config.getPopulationSize();
_num_generations = _config.getNumGenerations();

}

float Routes::getTime() {
return _time;
}
25 changes: 16 additions & 9 deletions src/routes-lib/routes.h
Original file line number Diff line number Diff line change
@@ -7,15 +7,7 @@

#include "genetics/genetics.h"

/** */

/** The default size of the population for calculating a route */
#define POP_SIZE 1000

/** The default number of generations that the population should be bred for */
#define NUM_GENERATIONS 600

/** This is a simplelass to handle the complete calculation of a route. */
/** This is a simple class to handle the complete calculation of a route. */
class Routes {

public:
@@ -149,6 +141,11 @@ class Routes {

private:

/**
* Reloads the paramaters if needed
*/
static void configureParams();

/**
* In some areas we have a no data value in the elevation data. In this case we have no idea what to compute
* the Z of the route to be. For now we check for this case and throw a std::runtime_error if this happens.
@@ -226,6 +223,16 @@ class Routes {
*/
static std::string length_fitness;

/** The default size of the population for calculating a route */
static int _pop_size;

/** This is a simplelass to handle the complete calculation of a route. */
static int _num_generations;

/**
* The current configuration
*/
static Configure _config;
};

#endif //ROUTES_ROUTES_H