-
Notifications
You must be signed in to change notification settings - Fork 26
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
marenz2569
wants to merge
2
commits into
tud-zih-energy:master
Choose a base branch
from
marenz2569:samo_is
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
||
|
@@ -116,6 +117,7 @@ class Firestarter { | |
const unsigned _individuals; | ||
const std::string _optimizeOutfile; | ||
const unsigned _generations; | ||
const unsigned _maxEvaluations; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
include/firestarter/Optimizer/Problem/SurrogateProblem.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why disabling static?