mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] AlgorithmRunner: Population autosave
This commit is contained in:
@@ -17,16 +17,50 @@
|
||||
|
||||
#include <tools/yulPhaser/AlgorithmRunner.h>
|
||||
|
||||
#include <tools/yulPhaser/Exceptions.h>
|
||||
|
||||
#include <libsolutil/Assertions.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
void AlgorithmRunner::run(GeneticAlgorithm& _algorithm)
|
||||
{
|
||||
populationAutosave();
|
||||
|
||||
for (size_t round = 0; !m_options.maxRounds.has_value() || round < m_options.maxRounds.value(); ++round)
|
||||
{
|
||||
m_population = _algorithm.runNextRound(m_population);
|
||||
|
||||
m_outputStream << "---------- ROUND " << round + 1 << " ----------" << endl;
|
||||
m_outputStream << m_population;
|
||||
|
||||
populationAutosave();
|
||||
}
|
||||
}
|
||||
|
||||
void AlgorithmRunner::populationAutosave() const
|
||||
{
|
||||
if (!m_options.populationAutosaveFile.has_value())
|
||||
return;
|
||||
|
||||
ofstream outputStream(m_options.populationAutosaveFile.value(), ios::out | ios::trunc);
|
||||
assertThrow(
|
||||
outputStream.is_open(),
|
||||
FileOpenError,
|
||||
"Could not open file '" + m_options.populationAutosaveFile.value() + "': " + strerror(errno)
|
||||
);
|
||||
|
||||
for (auto& individual: m_population.individuals())
|
||||
outputStream << individual.chromosome << endl;
|
||||
|
||||
assertThrow(
|
||||
!outputStream.bad(),
|
||||
FileWriteError,
|
||||
"Error while writing to file '" + m_options.populationAutosaveFile.value() + "': " + strerror(errno)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user