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

Feature SAMO-IS - Improve the optimization #40

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "lib/cxxopts"]
path = lib/cxxopts
url = https://github.com/tud-zih-energy/FIRESTARTER.git
[submodule "lib/SOT"]
path = lib/SOT
url = https://github.com/dme65/SOT.git
8 changes: 2 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,11 @@ if(NOT DEFINED ASMJIT_STATIC)
set(ASMJIT_STATIC TRUE)
endif()

add_subdirectory(lib/asmjit)
add_subdirectory(lib/nitro)
add_subdirectory(lib)

include_directories(include)
include_directories(lib/cxxopts/include)

set(JSON_BuildTests OFF CACHE INTERNAL "")
set(JSON_Install OFF CACHE INTERNAL "")
add_subdirectory(lib/json)
include_directories(lib/SOT/include)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
Expand Down
7 changes: 5 additions & 2 deletions derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
, git
, pkgconfig
, cudatoolkit
, armadillo
, withCuda ? false
, linuxPackages
}:
Expand Down Expand Up @@ -53,14 +54,16 @@ stdenv.mkDerivation rec {
nativeBuildInputs = [ cmake git pkgconfig ];

buildInputs = if withCuda then
[ glibc_multi cudatoolkit linuxPackages.nvidia_x11 hwloc ]
[ glibc_multi cudatoolkit linuxPackages.nvidia_x11 hwloc armadillo ]
else
[ glibc.static hwloc ];
[ glibc_multi hwloc armadillo ];
# [ glibc.static hwloc armadillo ];

cmakeFlags = [
"-DFIRESTARTER_BUILD_HWLOC=OFF"
"-DCMAKE_C_COMPILER_WORKS=1"
"-DCMAKE_CXX_COMPILER_WORKS=1"
"-DFIRESTARTER_LINK_STATIC=OFF"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why disabling static?

] ++ lib.optionals withCuda [
"-DFIRESTARTER_BUILD_TYPE=FIRESTARTER_CUDA"
];
Expand Down
4 changes: 3 additions & 1 deletion include/firestarter/Firestarter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class Firestarter {
std::vector<std::string> const &optimizationMetrics,
std::chrono::seconds const &evaluationDuration,
unsigned individuals, std::string const &optimizeOutfile,
unsigned generations, double nsga2_cr, double nsga2_m);
unsigned generations, unsigned maxEvaluations, double nsga2_cr,
double nsga2_m);

~Firestarter();

Expand Down Expand Up @@ -116,6 +117,7 @@ class Firestarter {
const unsigned _individuals;
const std::string _optimizeOutfile;
const unsigned _generations;
const unsigned _maxEvaluations;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary here? can it be held in Optimizer/Algorithm/SAMO_IS.hpp only?

const double _nsga2_cr;
const double _nsga2_m;

Expand Down
24 changes: 24 additions & 0 deletions include/firestarter/Optimizer/Algorithm/SAMO_IS.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <firestarter/Optimizer/Algorithm.hpp>

namespace firestarter::optimizer::algorithm {

class SAMO_IS : public Algorithm {
public:
SAMO_IS(unsigned maxEvaluations, double cr, double m);
~SAMO_IS() {}

void checkPopulation(firestarter::optimizer::Population const &pop,
std::size_t populationSize) override;

firestarter::optimizer::Population
evolve(firestarter::optimizer::Population &pop) override;

private:
unsigned _maxEvaluations;
double _cr;
double _m;
};

} // namespace firestarter::optimizer::algorithm
10 changes: 10 additions & 0 deletions include/firestarter/Optimizer/History.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ struct History {
_f = {};

public:
inline static std::size_t size() { return _x.size(); }

inline static std::vector<Individual> const &x() { return _x; }

inline static std::vector<
std::map<std::string, firestarter::measurement::Summary>> const &
f() {
return _f;
}

inline static void append(
std::vector<unsigned> const &ind,
std::map<std::string, firestarter::measurement::Summary> const &metric) {
Expand Down
10 changes: 7 additions & 3 deletions include/firestarter/Optimizer/Population.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ class Population {
// Construct a population from a problem.
Population() = default;

Population(std::shared_ptr<Problem> &&problem)
: _problem(std::move(problem)), gen(rd()) {}
Population(std::shared_ptr<Problem> &&problem, bool saveHistory = true)
: _problem(std::move(problem)), _saveHistory(saveHistory), gen(rd()) {}

Population(Population &pop)
: _problem(pop._problem), _x(pop._x), _f(pop._f), gen(rd()) {}
: _problem(pop._problem), _x(pop._x), _f(pop._f),
_saveHistory(pop._saveHistory), gen(rd()) {}

Population &operator=(Population const &pop) {
_problem = std::move(pop._problem);
_x = pop._x;
_f = pop._f;
_saveHistory = pop._saveHistory;
gen = pop.gen;

return *this;
Expand Down Expand Up @@ -89,6 +91,8 @@ class Population {
std::vector<Individual> _x;
std::vector<std::vector<double>> _f;

bool _saveHistory;

std::random_device rd;
std::mt19937 gen;
};
Expand Down
24 changes: 23 additions & 1 deletion include/firestarter/Optimizer/Problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,27 @@ class Problem {

virtual std::vector<double>
fitness(std::map<std::string, firestarter::measurement::Summary> const
&summaries) = 0;
&summaries) {
std::vector<double> values = {};

for (auto const &metricName : this->metrics()) {
auto findName = [metricName](auto const &summary) {
return metricName.compare(summary.first) == 0;
};

auto it = std::find_if(summaries.begin(), summaries.end(), findName);

if (it == summaries.end()) {
continue;
}

// round to two decimal places after the comma
auto value = std::round(it->second.average * 100.0) / 100.0;
values.push_back(value);
}

return values;
}

// get the bounds of the problem
virtual std::vector<std::tuple<unsigned, unsigned>> getBounds() const = 0;
Expand All @@ -59,6 +79,8 @@ class Problem {
// get the number of fitness evaluations
unsigned long long getFevals() const { return _fevals; };

virtual std::vector<std::string> metrics() const = 0;

protected:
// number of fitness evaluations
unsigned long long _fevals;
Expand Down
34 changes: 2 additions & 32 deletions include/firestarter/Optimizer/Problem/CLIArgumentProblem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,38 +87,6 @@ class CLIArgumentProblem final : public firestarter::optimizer::Problem {
return _measurementWorker->getValues(_startDelta, _stopDelta);
}

std::vector<double> fitness(
std::map<std::string, firestarter::measurement::Summary> const &summaries)
override {
std::vector<double> values = {};

for (auto const &metricName : _metrics) {
auto findName = [metricName](auto const &summary) {
auto invertedName = "-" + summary.first;
return metricName.compare(summary.first) == 0 ||
metricName.compare(invertedName) == 0;
};

auto it = std::find_if(summaries.begin(), summaries.end(), findName);

if (it == summaries.end()) {
continue;
}

// round to two decimal places after the comma
auto value = std::round(it->second.average * 100.0) / 100.0;

// invert metric
if (metricName[0] == '-') {
value *= -1.0;
}

values.push_back(value);
}

return values;
}

// get the bounds of the problem
std::vector<std::tuple<unsigned, unsigned>> getBounds() const override {
std::vector<std::tuple<unsigned, unsigned>> vec(
Expand All @@ -130,6 +98,8 @@ class CLIArgumentProblem final : public firestarter::optimizer::Problem {
// get the number of objectives.
std::size_t getNobjs() const override { return _metrics.size(); }

std::vector<std::string> metrics() const override { return _metrics; }

private:
std::function<void(std::vector<std::pair<std::string, unsigned>> const &)>
_changePayloadFunction;
Expand Down
135 changes: 135 additions & 0 deletions include/firestarter/Optimizer/Problem/SurrogateProblem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#pragma once

#include <firestarter/Optimizer/Problem.hpp>
#include <firestarter/Optimizer/Surrogate/SurrogateSelector.hpp>

#include <cassert>
#include <cmath>
#include <functional>
#include <memory>
#include <thread>
#include <tuple>
#include <utility>

namespace firestarter::optimizer::problem {

class SurrogateProblem final : public firestarter::optimizer::Problem {
public:
SurrogateProblem(std::vector<std::string> const &metrics,
std::vector<std::tuple<unsigned, unsigned>> const &bounds)
: _metrics(metrics), _bounds(bounds) {
assert(_metrics.size() != 0);

auto const &hist_x = History::x();
auto const dims = hist_x[0].size();

arma::vec boundsLow(dims);
arma::vec boundsUp(dims);
arma::mat x(dims, hist_x.size());

{
for (std::size_t i = 0; i < dims; ++i) {
boundsLow(i) = std::get<0>(bounds[i]);
boundsUp(i) = std::get<1>(bounds[i]);
}

for (std::size_t i = 0; i < hist_x.size(); ++i) {
x.col(i) = arma::conv_to<arma::vec>::from(hist_x[i]);
}
}

for (auto const &metric : metrics) {
std::string strippedMetricName;
arma::vec y(hist_x.size());

if (metric[0] == '-') {
strippedMetricName = metric.substr(1);
} else {
strippedMetricName = metric;
}

for (std::size_t i = 0; i < hist_x.size(); ++i) {
// fill y with 3 digits precision
y(i) = (double)((int)(History::find(hist_x[i])
.value()[strippedMetricName]
.average *
1000)) /
1000.0;
}

auto model = std::make_unique<
firestarter::optimizer::surrogate::SurrogateSelector>(boundsLow,
boundsUp, x, y);
log::info() << "Using surrogate model " << model->name() << " for metric "
<< metric;
_models.push_back(std::move(model));
}
}

~SurrogateProblem() {}

// return all available metrics for the individual
std::map<std::string, firestarter::measurement::Summary>
metrics(std::vector<unsigned> const &individual) override {
std::map<std::string, firestarter::measurement::Summary> metrics = {};
for (std::size_t i = 0; i < _metrics.size(); ++i) {
auto name = _metrics[i];
auto value = _models[i]->eval(arma::conv_to<arma::vec>::from(individual));
firestarter::measurement::Summary summary;
summary.average = value;
metrics[name] = summary;
}
return metrics;
}

std::vector<double> fitness(
std::map<std::string, firestarter::measurement::Summary> const &summaries)
override {
std::vector<double> values = {};

for (auto const &metricName : _metrics) {
auto findName = [metricName](auto const &summary) {
auto invertedName = "-" + summary.first;
return metricName.compare(summary.first) == 0 ||
metricName.compare(invertedName) == 0;
};

auto it = std::find_if(summaries.begin(), summaries.end(), findName);

if (it == summaries.end()) {
continue;
}

// round to two decimal places after the comma
auto value = std::round(it->second.average * 100.0) / 100.0;

// invert metric
if (metricName[0] == '-') {
value *= -1.0;
}

values.push_back(value);
}

return values;
}

// get the bounds of the problem
std::vector<std::tuple<unsigned, unsigned>> getBounds() const override {
return _bounds;
}

// get the number of objectives.
std::size_t getNobjs() const override { return _metrics.size(); }

std::vector<std::string> metrics() const override { return _metrics; }

private:
std::vector<std::string> _metrics;
std::vector<std::tuple<unsigned, unsigned>> _bounds;
std::vector<
std::unique_ptr<firestarter::optimizer::surrogate::SurrogateSelector>>
_models;
};

} // namespace firestarter::optimizer::problem
Loading