Auto reformatted the base, ref #470
This commit is contained in:
@@ -53,7 +53,7 @@ namespace lms
|
||||
core::Service<cover::ICoverService>::get()->getFromTrack(trackId, width);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace lms
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
@@ -66,14 +66,7 @@ int main(int argc, char* argv[])
|
||||
core::Service<core::logging::ILogger> logger{ std::make_unique<core::logging::StreamLogger>(std::cout) };
|
||||
|
||||
po::options_description desc{ "Allowed options" };
|
||||
desc.add_options()
|
||||
("help,h", "print usage message")
|
||||
("conf,c", po::value<std::string>()->default_value("/etc/lms.conf"), "LMS config file")
|
||||
("default-cover,d", po::value<std::string>(), "Default cover path")
|
||||
("tracks,t", "dump covers for tracks")
|
||||
("size,s", po::value<unsigned>()->default_value(512), "Requested cover size")
|
||||
("quality,q", po::value<unsigned>()->default_value(75), "JPEG quality (1-100)")
|
||||
;
|
||||
desc.add_options()("help,h", "print usage message")("conf,c", po::value<std::string>()->default_value("/etc/lms.conf"), "LMS config file")("default-cover,d", po::value<std::string>(), "Default cover path")("tracks,t", "dump covers for tracks")("size,s", po::value<unsigned>()->default_value(512), "Requested cover size")("quality,q", po::value<unsigned>()->default_value(75), "JPEG quality (1-100)");
|
||||
|
||||
po::variables_map vm;
|
||||
po::store(po::parse_command_line(argc, argv, desc), vm);
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
*/
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <filesystem>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <stdlib.h>
|
||||
@@ -28,20 +28,19 @@
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include "database/Db.hpp"
|
||||
#include "core/IConfig.hpp"
|
||||
#include "core/Random.hpp"
|
||||
#include "core/Service.hpp"
|
||||
#include "core/StreamLogger.hpp"
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/Cluster.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/MediaLibrary.hpp"
|
||||
#include "database/Release.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/Track.hpp"
|
||||
#include "database/TrackArtistLink.hpp"
|
||||
|
||||
#include "core/IConfig.hpp"
|
||||
#include "core/Random.hpp"
|
||||
#include "core/StreamLogger.hpp"
|
||||
#include "core/Service.hpp"
|
||||
|
||||
namespace lms
|
||||
{
|
||||
struct GeneratorParameters
|
||||
@@ -64,7 +63,8 @@ namespace lms
|
||||
std::vector<db::MediaLibrary::pointer> mediaLibraries;
|
||||
std::vector<db::Cluster::pointer> genres;
|
||||
std::vector<db::Cluster::pointer> moods;
|
||||
GenerationContext(db::Session& _session) : session{ _session } {}
|
||||
GenerationContext(db::Session& _session)
|
||||
: session{ _session } {}
|
||||
};
|
||||
|
||||
db::Cluster::pointer generateCluster(db::Session& session, db::ClusterType::pointer clusterType)
|
||||
@@ -162,7 +162,7 @@ namespace lms
|
||||
context.moods.push_back(generateCluster(context.session, mood));
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace lms
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
@@ -177,25 +177,13 @@ int main(int argc, char* argv[])
|
||||
const GeneratorParameters defaultParams;
|
||||
|
||||
program_options::options_description options{ "Options" };
|
||||
options.add_options()
|
||||
("conf,c", program_options::value<std::string>()->default_value("/etc/lms.conf"), "lms config file")
|
||||
("media-library-count", program_options::value<unsigned>()->default_value(defaultParams.mediaLibraryCount), "Number of media libraries to use")
|
||||
("release-count-per-batch", program_options::value<unsigned>()->default_value(defaultParams.releaseCountPerBatch), "Number of releases to generate before committing transaction")
|
||||
("release-count", program_options::value<unsigned>()->default_value(defaultParams.releaseCount), "Number of releases to generate")
|
||||
("track-count-per-release", program_options::value<unsigned>()->default_value(defaultParams.trackCountPerRelease), "Number of tracks per release")
|
||||
("compilation-ratio", program_options::value<float>()->default_value(defaultParams.compilationRatio), "Compilation ratio (compilation means all tracks have a different artist)")
|
||||
("track-path", program_options::value<std::string>()->required(), "Path of a valid track file, that will be used for all generated tracks")
|
||||
("genre-count", program_options::value<unsigned>()->default_value(defaultParams.genreCount), "Number of genres to generate")
|
||||
("genre-count-per-track", program_options::value<unsigned>()->default_value(defaultParams.genreCountPerTrack), "Number of genres to assign to each track")
|
||||
("mood-count", program_options::value<unsigned>()->default_value(defaultParams.moodCount), "Number of moods to generate")
|
||||
("mood-count-per-track", program_options::value<unsigned>()->default_value(defaultParams.moodCountPerTrack), "Number of moods to assign to each track")
|
||||
("help,h", "produce help message")
|
||||
;
|
||||
options.add_options()("conf,c", program_options::value<std::string>()->default_value("/etc/lms.conf"), "lms config file")("media-library-count", program_options::value<unsigned>()->default_value(defaultParams.mediaLibraryCount), "Number of media libraries to use")("release-count-per-batch", program_options::value<unsigned>()->default_value(defaultParams.releaseCountPerBatch), "Number of releases to generate before committing transaction")("release-count", program_options::value<unsigned>()->default_value(defaultParams.releaseCount), "Number of releases to generate")("track-count-per-release", program_options::value<unsigned>()->default_value(defaultParams.trackCountPerRelease), "Number of tracks per release")("compilation-ratio", program_options::value<float>()->default_value(defaultParams.compilationRatio), "Compilation ratio (compilation means all tracks have a different artist)")("track-path", program_options::value<std::string>()->required(), "Path of a valid track file, that will be used for all generated tracks")("genre-count", program_options::value<unsigned>()->default_value(defaultParams.genreCount), "Number of genres to generate")("genre-count-per-track", program_options::value<unsigned>()->default_value(defaultParams.genreCountPerTrack), "Number of genres to assign to each track")("mood-count", program_options::value<unsigned>()->default_value(defaultParams.moodCount), "Number of moods to generate")("mood-count-per-track", program_options::value<unsigned>()->default_value(defaultParams.moodCountPerTrack), "Number of moods to assign to each track")("help,h", "produce help message");
|
||||
|
||||
program_options::variables_map vm;
|
||||
program_options::store(program_options::parse_command_line(argc, argv, options), vm);
|
||||
|
||||
if (vm.count("help")) {
|
||||
if (vm.count("help"))
|
||||
{
|
||||
std::cout << options << "\n";
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -18,17 +18,17 @@
|
||||
*/
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <Wt/WDate.h>
|
||||
|
||||
#include "core/StreamLogger.hpp"
|
||||
#include "metadata/Exception.hpp"
|
||||
#include "metadata/IParser.hpp"
|
||||
#include "core/StreamLogger.hpp"
|
||||
|
||||
namespace lms::metadata
|
||||
{
|
||||
@@ -118,7 +118,8 @@ namespace lms::metadata
|
||||
|
||||
std::cout << "Parsing time: " << std::fixed << std::setprecision(2) << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000. << "ms" << std::endl;
|
||||
|
||||
std::cout << "Audio properties:\n" << track->audioProperties << std::endl;
|
||||
std::cout << "Audio properties:\n"
|
||||
<< track->audioProperties << std::endl;
|
||||
|
||||
std::cout << "Parsed metadata:" << std::endl;
|
||||
|
||||
@@ -220,7 +221,7 @@ namespace lms::metadata
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace lms::metadata
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
@@ -233,7 +234,7 @@ int main(int argc, char* argv[])
|
||||
try
|
||||
{
|
||||
using namespace lms;
|
||||
|
||||
|
||||
// log to stdout
|
||||
core::Service<core::logging::ILogger> logger{ std::make_unique<core::logging::StreamLogger>(std::cout, core::logging::StreamLogger::allSeverities) };
|
||||
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include "core/IConfig.hpp"
|
||||
#include "core/Service.hpp"
|
||||
#include "core/StreamLogger.hpp"
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/Cluster.hpp"
|
||||
#include "database/Db.hpp"
|
||||
@@ -32,9 +35,6 @@
|
||||
#include "database/Track.hpp"
|
||||
#include "database/Types.hpp"
|
||||
#include "services/recommendation/IRecommendationService.hpp"
|
||||
#include "core/IConfig.hpp"
|
||||
#include "core/Service.hpp"
|
||||
#include "core/StreamLogger.hpp"
|
||||
|
||||
namespace lms
|
||||
{
|
||||
@@ -42,31 +42,29 @@ namespace lms
|
||||
|
||||
void dumpTracksRecommendation(Session session, recommendation::IRecommendationService& recommendationService, unsigned maxSimilarityCount)
|
||||
{
|
||||
const RangeResults<TrackId> trackIds{ [&]
|
||||
{
|
||||
auto transaction {session.createReadTransaction()};
|
||||
return Track::findIds(session, Track::FindParameters{});
|
||||
}() };
|
||||
const RangeResults<TrackId> trackIds{ [&] {
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
return Track::findIds(session, Track::FindParameters{});
|
||||
}() };
|
||||
|
||||
std::cout << "*** Tracks (" << trackIds.results.size() << ") ***" << std::endl;
|
||||
for (const TrackId trackId : trackIds.results)
|
||||
{
|
||||
auto trackToString = [&](const TrackId trackId)
|
||||
{
|
||||
std::string res;
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
const Track::pointer track{ Track::find(session, trackId) };
|
||||
auto trackToString = [&](const TrackId trackId) {
|
||||
std::string res;
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
const Track::pointer track{ Track::find(session, trackId) };
|
||||
|
||||
res += track->getName();
|
||||
if (track->getRelease())
|
||||
res += " [" + std::string{ track->getRelease()->getName() } + "]";
|
||||
for (auto artist : track->getArtists({ TrackArtistLinkType::Artist }))
|
||||
res += " - " + artist->getName();
|
||||
for (auto cluster : track->getClusters())
|
||||
res += " {" + std::string{ cluster->getType()->getName() } + "-" + std::string{ cluster->getName() } + "}";
|
||||
res += track->getName();
|
||||
if (track->getRelease())
|
||||
res += " [" + std::string{ track->getRelease()->getName() } + "]";
|
||||
for (auto artist : track->getArtists({ TrackArtistLinkType::Artist }))
|
||||
res += " - " + artist->getName();
|
||||
for (auto cluster : track->getClusters())
|
||||
res += " {" + std::string{ cluster->getType()->getName() } + "-" + std::string{ cluster->getName() } + "}";
|
||||
|
||||
return res;
|
||||
};
|
||||
return res;
|
||||
};
|
||||
|
||||
std::cout << "Processing track '" << trackToString(trackId) << std::endl;
|
||||
for (TrackId similarTrackId : recommendationService.findSimilarTracks({ trackId }, maxSimilarityCount))
|
||||
@@ -76,22 +74,20 @@ namespace lms
|
||||
|
||||
void dumpReleasesRecommendation(Session session, recommendation::IRecommendationService& recommendationService, unsigned maxSimilarityCount)
|
||||
{
|
||||
const RangeResults<ReleaseId> releaseIds{ std::invoke([&]
|
||||
{
|
||||
auto transaction {session.createReadTransaction()};
|
||||
return Release::findIds(session, Release::FindParameters {});
|
||||
}) };
|
||||
const RangeResults<ReleaseId> releaseIds{ std::invoke([&] {
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
return Release::findIds(session, Release::FindParameters{});
|
||||
}) };
|
||||
|
||||
std::cout << "*** Releases ***" << std::endl;
|
||||
for (const ReleaseId releaseId : releaseIds.results)
|
||||
{
|
||||
auto releaseToString = [&](ReleaseId releaseId) -> std::string
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
auto releaseToString = [&](ReleaseId releaseId) -> std::string {
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
|
||||
Release::pointer release{ Release::find(session, releaseId) };
|
||||
return std::string{ release->getName() };
|
||||
};
|
||||
Release::pointer release{ Release::find(session, releaseId) };
|
||||
return std::string{ release->getName() };
|
||||
};
|
||||
|
||||
std::cout << "Processing release '" << releaseToString(releaseId) << "'" << std::endl;
|
||||
for (const ReleaseId similarReleaseId : recommendationService.getSimilarReleases(releaseId, maxSimilarityCount))
|
||||
@@ -101,22 +97,20 @@ namespace lms
|
||||
|
||||
void dumpArtistsRecommendation(Session session, recommendation::IRecommendationService& recommendationService, unsigned maxSimilarityCount)
|
||||
{
|
||||
const RangeResults<ArtistId> artistIds = std::invoke([&]()
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
return Artist::findIds(session, Artist::FindParameters{});
|
||||
});
|
||||
const RangeResults<ArtistId> artistIds = std::invoke([&]() {
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
return Artist::findIds(session, Artist::FindParameters{});
|
||||
});
|
||||
|
||||
std::cout << "*** Artists ***" << std::endl;
|
||||
for (ArtistId artistId : artistIds.results)
|
||||
{
|
||||
auto artistToString = [&](ArtistId artistId)
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
auto artistToString = [&](ArtistId artistId) {
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
|
||||
Artist::pointer artist{ Artist::find(session, artistId) };
|
||||
return artist->getName();
|
||||
};
|
||||
Artist::pointer artist{ Artist::find(session, artistId) };
|
||||
return artist->getName();
|
||||
};
|
||||
|
||||
std::cout << "Processing artist '" << artistToString(artistId) << "'" << std::endl;
|
||||
for (ArtistId similarArtistId : recommendationService.getSimilarArtists(artistId, { TrackArtistLinkType::Artist, TrackArtistLinkType::ReleaseArtist }, maxSimilarityCount))
|
||||
@@ -125,7 +119,7 @@ namespace lms
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace lms
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
@@ -138,14 +132,7 @@ int main(int argc, char* argv[])
|
||||
core::Service<core::logging::ILogger> logger{ std::make_unique<core::logging::StreamLogger>(std::cout) };
|
||||
|
||||
po::options_description desc{ "Allowed options" };
|
||||
desc.add_options()
|
||||
("help,h", "print usage message")
|
||||
("conf,c", po::value<std::string>()->default_value("/etc/lms.conf"), "LMS config file")
|
||||
("artists,a", "Display recommendation for artists")
|
||||
("releases,r", "Display recommendation for releases")
|
||||
("tracks,t", "Display recommendation for tracks")
|
||||
("max,m", po::value<unsigned>()->default_value(3), "Max similarity result count")
|
||||
;
|
||||
desc.add_options()("help,h", "print usage message")("conf,c", po::value<std::string>()->default_value("/etc/lms.conf"), "LMS config file")("artists,a", "Display recommendation for artists")("releases,r", "Display recommendation for releases")("tracks,t", "Display recommendation for tracks")("max,m", po::value<unsigned>()->default_value(3), "Max similarity result count");
|
||||
|
||||
po::variables_map vm;
|
||||
po::store(po::parse_command_line(argc, argv, desc), vm);
|
||||
@@ -189,4 +176,3 @@ int main(int argc, char* argv[])
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,89 +26,88 @@
|
||||
template<typename Individual>
|
||||
class GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
using Score = float;
|
||||
public:
|
||||
using Score = float;
|
||||
|
||||
using BreedFunction = std::function<Individual(const Individual&, const Individual&)>;
|
||||
using MutateFunction = std::function<void(Individual&)>;
|
||||
using ScoreFunction = std::function<Score(const Individual&)>;
|
||||
using BreedFunction = std::function<Individual(const Individual&, const Individual&)>;
|
||||
using MutateFunction = std::function<void(Individual&)>;
|
||||
using ScoreFunction = std::function<Score(const Individual&)>;
|
||||
|
||||
struct Params
|
||||
{
|
||||
std::size_t nbWorkers {1};
|
||||
std::size_t nbGenerations;
|
||||
float crossoverRatio {0.5};
|
||||
float mutationProbability {0.05};
|
||||
BreedFunction breedFunction;
|
||||
MutateFunction mutateFunction;
|
||||
ScoreFunction scoreFunction;
|
||||
};
|
||||
struct Params
|
||||
{
|
||||
std::size_t nbWorkers{ 1 };
|
||||
std::size_t nbGenerations;
|
||||
float crossoverRatio{ 0.5 };
|
||||
float mutationProbability{ 0.05 };
|
||||
BreedFunction breedFunction;
|
||||
MutateFunction mutateFunction;
|
||||
ScoreFunction scoreFunction;
|
||||
};
|
||||
|
||||
GeneticAlgorithm(const Params& params);
|
||||
GeneticAlgorithm(const Params& params);
|
||||
|
||||
// Returns the individual that has the maximum score after processing the requested generations
|
||||
Individual simulate(const std::vector<Individual>& initialPopulation);
|
||||
// Returns the individual that has the maximum score after processing the requested generations
|
||||
Individual simulate(const std::vector<Individual>& initialPopulation);
|
||||
|
||||
private:
|
||||
struct ScoredIndividual
|
||||
{
|
||||
Individual individual;
|
||||
std::optional<Score> score {};
|
||||
};
|
||||
private:
|
||||
struct ScoredIndividual
|
||||
{
|
||||
Individual individual;
|
||||
std::optional<Score> score{};
|
||||
};
|
||||
|
||||
void scoreAndSortPopulation(std::vector<ScoredIndividual>& population);
|
||||
Score getTotalScore(const std::vector<ScoredIndividual>& population) const;
|
||||
typename std::vector<ScoredIndividual>::const_iterator pickRandomRouletteWheel(const std::vector<ScoredIndividual>& population, Score totalScore);
|
||||
void scoreAndSortPopulation(std::vector<ScoredIndividual>& population);
|
||||
Score getTotalScore(const std::vector<ScoredIndividual>& population) const;
|
||||
typename std::vector<ScoredIndividual>::const_iterator pickRandomRouletteWheel(const std::vector<ScoredIndividual>& population, Score totalScore);
|
||||
|
||||
Params _params;
|
||||
Params _params;
|
||||
};
|
||||
|
||||
template<typename Individual>
|
||||
GeneticAlgorithm<Individual>::GeneticAlgorithm(const Params& params)
|
||||
: _params {params}
|
||||
: _params{ params }
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template<typename Individual>
|
||||
Individual
|
||||
GeneticAlgorithm<Individual>::simulate(const std::vector<Individual>& initialPopulation)
|
||||
{
|
||||
const std::size_t childrenCountPerGeneration {static_cast<std::size_t>(initialPopulation.size() * _params.crossoverRatio)};
|
||||
const std::size_t childrenCountPerGeneration{ static_cast<std::size_t>(initialPopulation.size() * _params.crossoverRatio) };
|
||||
if (initialPopulation.size() < 10)
|
||||
throw std::runtime_error("Initial population must has at least 10 elements");
|
||||
|
||||
std::vector<ScoredIndividual> scoredPopulation;
|
||||
scoredPopulation.reserve(initialPopulation.size());
|
||||
|
||||
std::transform(std::cbegin(initialPopulation), std::cend(initialPopulation), std::back_inserter(scoredPopulation ),
|
||||
[](const Individual& individual) { return ScoredIndividual {individual};});
|
||||
std::transform(std::cbegin(initialPopulation), std::cend(initialPopulation), std::back_inserter(scoredPopulation),
|
||||
[](const Individual& individual) { return ScoredIndividual{ individual }; });
|
||||
|
||||
scoreAndSortPopulation(scoredPopulation);
|
||||
|
||||
for (std::size_t currentGeneration {}; currentGeneration < _params.nbGenerations; ++currentGeneration)
|
||||
for (std::size_t currentGeneration{}; currentGeneration < _params.nbGenerations; ++currentGeneration)
|
||||
{
|
||||
assert(scoredPopulation.size() == initialPopulation.size());
|
||||
std::cout << "Processing generation " << currentGeneration << "..." << std::endl;
|
||||
std::cout << "Need to create " << childrenCountPerGeneration << " new children" << std::endl;
|
||||
|
||||
// breed
|
||||
const Score populationTotalScore {getTotalScore(scoredPopulation)};
|
||||
const Score populationTotalScore{ getTotalScore(scoredPopulation) };
|
||||
std::vector<ScoredIndividual> children;
|
||||
children.reserve(childrenCountPerGeneration);
|
||||
|
||||
while (children.size() < childrenCountPerGeneration)
|
||||
{
|
||||
// Select two random parents using their score as weight
|
||||
const auto itParent1 {pickRandomRouletteWheel(scoredPopulation, populationTotalScore)};
|
||||
const auto itParent2 {pickRandomRouletteWheel(scoredPopulation, populationTotalScore)};
|
||||
const auto itParent1{ pickRandomRouletteWheel(scoredPopulation, populationTotalScore) };
|
||||
const auto itParent2{ pickRandomRouletteWheel(scoredPopulation, populationTotalScore) };
|
||||
|
||||
if (itParent1 == itParent2)
|
||||
continue;
|
||||
|
||||
ScoredIndividual child {_params.breedFunction(itParent1->individual, itParent2->individual)};
|
||||
|
||||
if (core::random::getRealRandom(float {}, float {1}) <= _params.mutationProbability)
|
||||
ScoredIndividual child{ _params.breedFunction(itParent1->individual, itParent2->individual) };
|
||||
|
||||
if (core::random::getRealRandom(float{}, float{ 1 }) <= _params.mutationProbability)
|
||||
_params.mutateFunction(child.individual);
|
||||
|
||||
children.emplace_back(std::move(child));
|
||||
@@ -130,17 +129,14 @@ GeneticAlgorithm<Individual>::simulate(const std::vector<Individual>& initialPop
|
||||
return scoredPopulation.front().individual;
|
||||
}
|
||||
|
||||
|
||||
template<typename Individual>
|
||||
void
|
||||
GeneticAlgorithm<Individual>::scoreAndSortPopulation(std::vector<ScoredIndividual>& scoredPopulation)
|
||||
void GeneticAlgorithm<Individual>::scoreAndSortPopulation(std::vector<ScoredIndividual>& scoredPopulation)
|
||||
{
|
||||
parallel_foreach(_params.nbWorkers, std::begin(scoredPopulation), std::end(scoredPopulation),
|
||||
[&](ScoredIndividual& scoredIndividual)
|
||||
{
|
||||
if (!scoredIndividual.score)
|
||||
scoredIndividual.score = _params.scoreFunction(scoredIndividual.individual);
|
||||
});
|
||||
[&](ScoredIndividual& scoredIndividual) {
|
||||
if (!scoredIndividual.score)
|
||||
scoredIndividual.score = _params.scoreFunction(scoredIndividual.individual);
|
||||
});
|
||||
|
||||
std::sort(std::begin(scoredPopulation), std::end(scoredPopulation), [](const ScoredIndividual& a, const ScoredIndividual& b) { return a.score > b.score; });
|
||||
}
|
||||
@@ -149,17 +145,17 @@ template<typename Individual>
|
||||
typename GeneticAlgorithm<Individual>::Score
|
||||
GeneticAlgorithm<Individual>::getTotalScore(const std::vector<ScoredIndividual>& scoredPopulation) const
|
||||
{
|
||||
return std::accumulate(std::cbegin(scoredPopulation), std::cend(scoredPopulation), Score {}, [](Score score, const ScoredIndividual& individual) { return score + *individual.score; });
|
||||
return std::accumulate(std::cbegin(scoredPopulation), std::cend(scoredPopulation), Score{}, [](Score score, const ScoredIndividual& individual) { return score + *individual.score; });
|
||||
}
|
||||
|
||||
template<typename Individual>
|
||||
typename std::vector<typename GeneticAlgorithm<Individual>::ScoredIndividual>::const_iterator
|
||||
GeneticAlgorithm<Individual>::pickRandomRouletteWheel(const std::vector<ScoredIndividual>& population, Score totalScore)
|
||||
{
|
||||
const Score randomScore {core::random::getRealRandom(Score {}, totalScore)};
|
||||
const Score randomScore{ core::random::getRealRandom(Score{}, totalScore) };
|
||||
|
||||
Score curScore{};
|
||||
for (auto itScoredIndividual {std::cbegin(population)}; itScoredIndividual != std::cend(population); ++itScoredIndividual )
|
||||
for (auto itScoredIndividual{ std::cbegin(population) }; itScoredIndividual != std::cend(population); ++itScoredIndividual)
|
||||
{
|
||||
if (curScore + *itScoredIndividual->score > randomScore)
|
||||
return itScoredIndividual;
|
||||
|
||||
@@ -17,10 +17,13 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "core/Config.hpp"
|
||||
#include "core/Service.hpp"
|
||||
#include "core/StreamLogger.hpp"
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/Cluster.hpp"
|
||||
#include "database/Db.hpp"
|
||||
@@ -29,9 +32,6 @@
|
||||
#include "database/Track.hpp"
|
||||
#include "database/TrackFeatures.hpp"
|
||||
#include "similarity/features/SimilarityFeaturesSearcher.hpp"
|
||||
#include "core/Config.hpp"
|
||||
#include "core/Service.hpp"
|
||||
#include "core/StreamLogger.hpp"
|
||||
|
||||
#include "GeneticAlgorithm.hpp"
|
||||
|
||||
@@ -40,128 +40,126 @@ using SimilarityScore = GeneticAlgorithm<FeatureSettingsMap>::Score;
|
||||
|
||||
// An individual is just a FeatureSettingsMap
|
||||
// The goal is to get the FeatureSettingsMap that maximize the score
|
||||
const FeatureSettingsMap featuresSettings
|
||||
{
|
||||
{ "lowlevel.average_loudness", {1}},
|
||||
{ "lowlevel.barkbands.mean", {1}},
|
||||
{ "lowlevel.barkbands.median", {1}},
|
||||
{ "lowlevel.barkbands.var", {1}},
|
||||
{ "lowlevel.barkbands_crest.mean", {1}},
|
||||
{ "lowlevel.barkbands_crest.median", {1}},
|
||||
{ "lowlevel.barkbands_crest.var", {1}},
|
||||
{ "lowlevel.barkbands_flatness_db.mean", {1}},
|
||||
{ "lowlevel.barkbands_flatness_db.median", {1}},
|
||||
{ "lowlevel.barkbands_flatness_db.var", {1}},
|
||||
{ "lowlevel.barkbands_kurtosis.mean", {1}},
|
||||
{ "lowlevel.barkbands_kurtosis.median", {1}},
|
||||
{ "lowlevel.barkbands_kurtosis.var", {1}},
|
||||
{ "lowlevel.barkbands_skewness.mean", {1}},
|
||||
{ "lowlevel.barkbands_skewness.median", {1}},
|
||||
{ "lowlevel.barkbands_skewness.var", {1}},
|
||||
{ "lowlevel.barkbands_spread.mean", {1}},
|
||||
{ "lowlevel.barkbands_spread.median", {1}},
|
||||
{ "lowlevel.barkbands_spread.var", {1}},
|
||||
{ "lowlevel.dissonance.mean", {1}},
|
||||
{ "lowlevel.dissonance.median", {1}},
|
||||
{ "lowlevel.dissonance.var", {1}},
|
||||
{ "lowlevel.dynamic_complexity", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.mean", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.median", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.var", {1}},
|
||||
{ "lowlevel.erbbands.mean", {1}},
|
||||
{ "lowlevel.erbbands.median", {1}},
|
||||
{ "lowlevel.erbbands.var", {1}},
|
||||
{ "lowlevel.gfcc.mean", {1}},
|
||||
{ "lowlevel.hfc.mean", {1}},
|
||||
{ "lowlevel.hfc.median", {1}},
|
||||
{ "lowlevel.hfc.var", {1}},
|
||||
{ "tonal.hpcp.median", {1}},
|
||||
{ "lowlevel.melbands.mean", {1}},
|
||||
{ "lowlevel.melbands.median", {1}},
|
||||
{ "lowlevel.melbands.var", {1}},
|
||||
{ "lowlevel.melbands_crest.mean", {1}},
|
||||
{ "lowlevel.melbands_crest.median", {1}},
|
||||
{ "lowlevel.melbands_crest.var", {1}},
|
||||
{ "lowlevel.melbands_flatness_db.mean", {1}},
|
||||
{ "lowlevel.melbands_flatness_db.median", {1}},
|
||||
{ "lowlevel.melbands_flatness_db.var", {1}},
|
||||
{ "lowlevel.melbands_kurtosis.mean", {1}},
|
||||
{ "lowlevel.melbands_kurtosis.median", {1}},
|
||||
{ "lowlevel.melbands_kurtosis.var", {1}},
|
||||
{ "lowlevel.melbands_skewness.mean", {1}},
|
||||
{ "lowlevel.melbands_skewness.median", {1}},
|
||||
{ "lowlevel.melbands_skewness.var", {1}},
|
||||
{ "lowlevel.melbands_spread.mean", {1}},
|
||||
{ "lowlevel.melbands_spread.median", {1}},
|
||||
{ "lowlevel.melbands_spread.var", {1}},
|
||||
{ "lowlevel.mfcc.mean", {1}},
|
||||
{ "lowlevel.pitch_salience.mean", {1}},
|
||||
{ "lowlevel.pitch_salience.median", {1}},
|
||||
{ "lowlevel.pitch_salience.var", {1}},
|
||||
{ "lowlevel.silence_rate_30dB.mean", {1}},
|
||||
{ "lowlevel.silence_rate_30dB.median", {1}},
|
||||
{ "lowlevel.silence_rate_30dB.var", {1}},
|
||||
{ "lowlevel.silence_rate_60dB.mean", {1}},
|
||||
{ "lowlevel.silence_rate_60dB.median", {1}},
|
||||
{ "lowlevel.silence_rate_60dB.var", {1}},
|
||||
{ "lowlevel.spectral_centroid.mean", {1}},
|
||||
{ "lowlevel.spectral_centroid.median", {1}},
|
||||
{ "lowlevel.spectral_centroid.var", {1}},
|
||||
{ "lowlevel.spectral_complexity.mean", {1}},
|
||||
{ "lowlevel.spectral_complexity.median", {1}},
|
||||
{ "lowlevel.spectral_complexity.var", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.mean", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.median", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.var", {1}},
|
||||
{ "lowlevel.spectral_contrast_valleys.mean", {1}},
|
||||
{ "lowlevel.spectral_contrast_valleys.median", {1}},
|
||||
{ "lowlevel.spectral_contrast_valleys.var", {1}},
|
||||
{ "lowlevel.spectral_decrease.mean", {1}},
|
||||
{ "lowlevel.spectral_decrease.median", {1}},
|
||||
{ "lowlevel.spectral_decrease.var", {1}},
|
||||
{ "lowlevel.spectral_energy.mean", {1}},
|
||||
{ "lowlevel.spectral_energy.median", {1}},
|
||||
{ "lowlevel.spectral_energy.var", {1}},
|
||||
{ "lowlevel.spectral_energyband_high.mean", {1}},
|
||||
{ "lowlevel.spectral_energyband_high.median", {1}},
|
||||
{ "lowlevel.spectral_energyband_high.var", {1}},
|
||||
{ "lowlevel.spectral_energyband_low.mean", {1}},
|
||||
{ "lowlevel.spectral_energyband_low.median", {1}},
|
||||
{ "lowlevel.spectral_energyband_low.var", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_high.mean", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_high.median", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_high.var", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_low.mean", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_low.median", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_low.var", {1}},
|
||||
{ "lowlevel.spectral_entropy.mean", {1}},
|
||||
{ "lowlevel.spectral_entropy.median", {1}},
|
||||
{ "lowlevel.spectral_entropy.var", {1}},
|
||||
{ "lowlevel.spectral_flux.mean", {1}},
|
||||
{ "lowlevel.spectral_flux.median", {1}},
|
||||
{ "lowlevel.spectral_flux.var", {1}},
|
||||
{ "lowlevel.spectral_kurtosis.mean", {1}},
|
||||
{ "lowlevel.spectral_kurtosis.median", {1}},
|
||||
{ "lowlevel.spectral_kurtosis.var", {1}},
|
||||
{ "lowlevel.spectral_rms.mean", {1}},
|
||||
{ "lowlevel.spectral_rms.median", {1}},
|
||||
{ "lowlevel.spectral_rms.var", {1}},
|
||||
{ "lowlevel.spectral_rolloff.mean", {1}},
|
||||
{ "lowlevel.spectral_rolloff.median", {1}},
|
||||
{ "lowlevel.spectral_rolloff.var", {1}},
|
||||
{ "lowlevel.spectral_skewness.mean", {1}},
|
||||
{ "lowlevel.spectral_skewness.median", {1}},
|
||||
{ "lowlevel.spectral_skewness.var", {1}},
|
||||
{ "lowlevel.spectral_spread.mean", {1}},
|
||||
{ "lowlevel.spectral_spread.median", {1}},
|
||||
{ "lowlevel.spectral_spread.var", {1}},
|
||||
{ "lowlevel.zerocrossingrate.mean", {1}},
|
||||
{ "lowlevel.zerocrossingrate.median", {1}},
|
||||
{ "lowlevel.zerocrossingrate.var", {1}},
|
||||
const FeatureSettingsMap featuresSettings{
|
||||
{ "lowlevel.average_loudness", { 1 } },
|
||||
{ "lowlevel.barkbands.mean", { 1 } },
|
||||
{ "lowlevel.barkbands.median", { 1 } },
|
||||
{ "lowlevel.barkbands.var", { 1 } },
|
||||
{ "lowlevel.barkbands_crest.mean", { 1 } },
|
||||
{ "lowlevel.barkbands_crest.median", { 1 } },
|
||||
{ "lowlevel.barkbands_crest.var", { 1 } },
|
||||
{ "lowlevel.barkbands_flatness_db.mean", { 1 } },
|
||||
{ "lowlevel.barkbands_flatness_db.median", { 1 } },
|
||||
{ "lowlevel.barkbands_flatness_db.var", { 1 } },
|
||||
{ "lowlevel.barkbands_kurtosis.mean", { 1 } },
|
||||
{ "lowlevel.barkbands_kurtosis.median", { 1 } },
|
||||
{ "lowlevel.barkbands_kurtosis.var", { 1 } },
|
||||
{ "lowlevel.barkbands_skewness.mean", { 1 } },
|
||||
{ "lowlevel.barkbands_skewness.median", { 1 } },
|
||||
{ "lowlevel.barkbands_skewness.var", { 1 } },
|
||||
{ "lowlevel.barkbands_spread.mean", { 1 } },
|
||||
{ "lowlevel.barkbands_spread.median", { 1 } },
|
||||
{ "lowlevel.barkbands_spread.var", { 1 } },
|
||||
{ "lowlevel.dissonance.mean", { 1 } },
|
||||
{ "lowlevel.dissonance.median", { 1 } },
|
||||
{ "lowlevel.dissonance.var", { 1 } },
|
||||
{ "lowlevel.dynamic_complexity", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_coeffs.mean", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_coeffs.median", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_coeffs.var", { 1 } },
|
||||
{ "lowlevel.erbbands.mean", { 1 } },
|
||||
{ "lowlevel.erbbands.median", { 1 } },
|
||||
{ "lowlevel.erbbands.var", { 1 } },
|
||||
{ "lowlevel.gfcc.mean", { 1 } },
|
||||
{ "lowlevel.hfc.mean", { 1 } },
|
||||
{ "lowlevel.hfc.median", { 1 } },
|
||||
{ "lowlevel.hfc.var", { 1 } },
|
||||
{ "tonal.hpcp.median", { 1 } },
|
||||
{ "lowlevel.melbands.mean", { 1 } },
|
||||
{ "lowlevel.melbands.median", { 1 } },
|
||||
{ "lowlevel.melbands.var", { 1 } },
|
||||
{ "lowlevel.melbands_crest.mean", { 1 } },
|
||||
{ "lowlevel.melbands_crest.median", { 1 } },
|
||||
{ "lowlevel.melbands_crest.var", { 1 } },
|
||||
{ "lowlevel.melbands_flatness_db.mean", { 1 } },
|
||||
{ "lowlevel.melbands_flatness_db.median", { 1 } },
|
||||
{ "lowlevel.melbands_flatness_db.var", { 1 } },
|
||||
{ "lowlevel.melbands_kurtosis.mean", { 1 } },
|
||||
{ "lowlevel.melbands_kurtosis.median", { 1 } },
|
||||
{ "lowlevel.melbands_kurtosis.var", { 1 } },
|
||||
{ "lowlevel.melbands_skewness.mean", { 1 } },
|
||||
{ "lowlevel.melbands_skewness.median", { 1 } },
|
||||
{ "lowlevel.melbands_skewness.var", { 1 } },
|
||||
{ "lowlevel.melbands_spread.mean", { 1 } },
|
||||
{ "lowlevel.melbands_spread.median", { 1 } },
|
||||
{ "lowlevel.melbands_spread.var", { 1 } },
|
||||
{ "lowlevel.mfcc.mean", { 1 } },
|
||||
{ "lowlevel.pitch_salience.mean", { 1 } },
|
||||
{ "lowlevel.pitch_salience.median", { 1 } },
|
||||
{ "lowlevel.pitch_salience.var", { 1 } },
|
||||
{ "lowlevel.silence_rate_30dB.mean", { 1 } },
|
||||
{ "lowlevel.silence_rate_30dB.median", { 1 } },
|
||||
{ "lowlevel.silence_rate_30dB.var", { 1 } },
|
||||
{ "lowlevel.silence_rate_60dB.mean", { 1 } },
|
||||
{ "lowlevel.silence_rate_60dB.median", { 1 } },
|
||||
{ "lowlevel.silence_rate_60dB.var", { 1 } },
|
||||
{ "lowlevel.spectral_centroid.mean", { 1 } },
|
||||
{ "lowlevel.spectral_centroid.median", { 1 } },
|
||||
{ "lowlevel.spectral_centroid.var", { 1 } },
|
||||
{ "lowlevel.spectral_complexity.mean", { 1 } },
|
||||
{ "lowlevel.spectral_complexity.median", { 1 } },
|
||||
{ "lowlevel.spectral_complexity.var", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_coeffs.mean", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_coeffs.median", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_coeffs.var", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_valleys.mean", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_valleys.median", { 1 } },
|
||||
{ "lowlevel.spectral_contrast_valleys.var", { 1 } },
|
||||
{ "lowlevel.spectral_decrease.mean", { 1 } },
|
||||
{ "lowlevel.spectral_decrease.median", { 1 } },
|
||||
{ "lowlevel.spectral_decrease.var", { 1 } },
|
||||
{ "lowlevel.spectral_energy.mean", { 1 } },
|
||||
{ "lowlevel.spectral_energy.median", { 1 } },
|
||||
{ "lowlevel.spectral_energy.var", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_high.mean", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_high.median", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_high.var", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_low.mean", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_low.median", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_low.var", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_middle_high.mean", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_middle_high.median", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_middle_high.var", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_middle_low.mean", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_middle_low.median", { 1 } },
|
||||
{ "lowlevel.spectral_energyband_middle_low.var", { 1 } },
|
||||
{ "lowlevel.spectral_entropy.mean", { 1 } },
|
||||
{ "lowlevel.spectral_entropy.median", { 1 } },
|
||||
{ "lowlevel.spectral_entropy.var", { 1 } },
|
||||
{ "lowlevel.spectral_flux.mean", { 1 } },
|
||||
{ "lowlevel.spectral_flux.median", { 1 } },
|
||||
{ "lowlevel.spectral_flux.var", { 1 } },
|
||||
{ "lowlevel.spectral_kurtosis.mean", { 1 } },
|
||||
{ "lowlevel.spectral_kurtosis.median", { 1 } },
|
||||
{ "lowlevel.spectral_kurtosis.var", { 1 } },
|
||||
{ "lowlevel.spectral_rms.mean", { 1 } },
|
||||
{ "lowlevel.spectral_rms.median", { 1 } },
|
||||
{ "lowlevel.spectral_rms.var", { 1 } },
|
||||
{ "lowlevel.spectral_rolloff.mean", { 1 } },
|
||||
{ "lowlevel.spectral_rolloff.median", { 1 } },
|
||||
{ "lowlevel.spectral_rolloff.var", { 1 } },
|
||||
{ "lowlevel.spectral_skewness.mean", { 1 } },
|
||||
{ "lowlevel.spectral_skewness.median", { 1 } },
|
||||
{ "lowlevel.spectral_skewness.var", { 1 } },
|
||||
{ "lowlevel.spectral_spread.mean", { 1 } },
|
||||
{ "lowlevel.spectral_spread.median", { 1 } },
|
||||
{ "lowlevel.spectral_spread.var", { 1 } },
|
||||
{ "lowlevel.zerocrossingrate.mean", { 1 } },
|
||||
{ "lowlevel.zerocrossingrate.median", { 1 } },
|
||||
{ "lowlevel.zerocrossingrate.var", { 1 } },
|
||||
};
|
||||
|
||||
static
|
||||
std::unordered_map<db::IdType, FeatureValuesMap>
|
||||
static std::unordered_map<db::IdType, FeatureValuesMap>
|
||||
constructFeaturesCache(db::Session& session, const FeatureSettingsMap& featureSettings)
|
||||
{
|
||||
std::unordered_map<db::IdType, FeatureValuesMap> cache;
|
||||
@@ -183,8 +181,7 @@ constructFeaturesCache(db::Session& session, const FeatureSettingsMap& featureSe
|
||||
return cache;
|
||||
}
|
||||
|
||||
static
|
||||
std::optional<FeatureValuesMap>
|
||||
static std::optional<FeatureValuesMap>
|
||||
getFeaturesFromCache(const std::unordered_map<db::IdType, FeatureValuesMap>& cache, db::IdType trackId, const FeatureNames& names)
|
||||
{
|
||||
std::optional<FeatureValuesMap> res;
|
||||
@@ -211,8 +208,7 @@ getFeaturesFromCache(const std::unordered_map<db::IdType, FeatureValuesMap>& cac
|
||||
return res;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
static void
|
||||
printFeatureSettingsMap(const FeatureSettingsMap& featureSettings)
|
||||
{
|
||||
std::cout << "FeatureSettingsMap: (" << featureSettings.size() << " features)" << std::endl;
|
||||
@@ -220,8 +216,7 @@ printFeatureSettingsMap(const FeatureSettingsMap& featureSettings)
|
||||
std::cout << "\t" << name << std::endl;
|
||||
}
|
||||
|
||||
static
|
||||
std::string
|
||||
static std::string
|
||||
trackToString(db::Session& session, db::IdType trackId)
|
||||
{
|
||||
std::string res;
|
||||
@@ -239,8 +234,7 @@ trackToString(db::Session& session, db::IdType trackId)
|
||||
return res;
|
||||
}
|
||||
|
||||
static
|
||||
SimilarityScore
|
||||
static SimilarityScore
|
||||
computeTrackScore(db::Session& session, db::IdType track1Id, db::IdType track2Id)
|
||||
{
|
||||
SimilarityScore score{};
|
||||
@@ -282,8 +276,7 @@ computeTrackScore(db::Session& session, db::IdType track1Id, db::IdType track2Id
|
||||
return score;
|
||||
}
|
||||
|
||||
static
|
||||
SimilarityScore
|
||||
static SimilarityScore
|
||||
computeSimilarityScore(db::Session& session, FeaturesSearcher::TrainSettings trainSettings)
|
||||
{
|
||||
std::cout << "Compute score of: ";
|
||||
@@ -292,11 +285,10 @@ computeSimilarityScore(db::Session& session, FeaturesSearcher::TrainSettings tra
|
||||
|
||||
FeaturesSearcher searcher{ session, trainSettings };
|
||||
|
||||
const std::vector<db::IdType> trackIds = std::invoke([&]()
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
return db::Track::getAllIdsWithFeatures(session);
|
||||
});
|
||||
const std::vector<db::IdType> trackIds = std::invoke([&]() {
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
return db::Track::getAllIdsWithFeatures(session);
|
||||
});
|
||||
|
||||
SimilarityScore score{};
|
||||
for (db::IdType trackId : trackIds)
|
||||
@@ -320,17 +312,15 @@ computeSimilarityScore(db::Session& session, FeaturesSearcher::TrainSettings tra
|
||||
return score;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
static void
|
||||
printBadlyClassifiedTracks(db::Session& session, FeaturesSearcher::TrainSettings trainSettings)
|
||||
{
|
||||
FeaturesSearcher searcher{ session, trainSettings };
|
||||
|
||||
const std::vector<db::IdType> trackIds = std::invoke([&]()
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
return db::Track::getAllIdsWithFeatures(session);
|
||||
});
|
||||
const std::vector<db::IdType> trackIds = std::invoke([&]() {
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
return db::Track::getAllIdsWithFeatures(session);
|
||||
});
|
||||
|
||||
for (db::IdType trackId : trackIds)
|
||||
{
|
||||
@@ -344,9 +334,7 @@ printBadlyClassifiedTracks(db::Session& session, FeaturesSearcher::TrainSettings
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
FeatureSettingsMap
|
||||
static FeatureSettingsMap
|
||||
breedFeatureSettingsMap(const FeatureSettingsMap& a, const FeatureSettingsMap& b)
|
||||
{
|
||||
FeatureSettingsMap res;
|
||||
@@ -364,8 +352,7 @@ breedFeatureSettingsMap(const FeatureSettingsMap& a, const FeatureSettingsMap& b
|
||||
return res;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
static void
|
||||
mutateFeatureSettingsMap(FeatureSettingsMap& a)
|
||||
{
|
||||
const std::size_t size{ a.size() };
|
||||
@@ -384,7 +371,7 @@ int main(int argc, char* argv[])
|
||||
try
|
||||
{
|
||||
// log to stdout
|
||||
// ServiceProvider<Logger>::create<StreamLogger>(std::cout);
|
||||
// ServiceProvider<Logger>::create<StreamLogger>(std::cout);
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
@@ -402,12 +389,11 @@ int main(int argc, char* argv[])
|
||||
|
||||
std::cout << "Caching all features..." << std::endl;
|
||||
// Cache all the features of all the music in order to speed up the multiple trainings
|
||||
const auto cachedFeatures{ constructFeaturesCache(db::SessionPool::ScopedSession {sessionPool}.get(), featuresSettings) };
|
||||
const auto cachedFeatures{ constructFeaturesCache(db::SessionPool::ScopedSession{ sessionPool }.get(), featuresSettings) };
|
||||
std::cout << "Caching all features DONE" << std::endl;
|
||||
|
||||
FeaturesSearcher::setFeaturesFetchFunc(
|
||||
[&](db::IdType trackId, const FeatureNames& featureNames)
|
||||
{
|
||||
[&](db::IdType trackId, const FeatureNames& featureNames) {
|
||||
return getFeaturesFromCache(cachedFeatures, trackId, featureNames);
|
||||
});
|
||||
|
||||
@@ -442,8 +428,7 @@ int main(int argc, char* argv[])
|
||||
params.breedFunction = breedFeatureSettingsMap;
|
||||
params.mutateFunction = mutateFeatureSettingsMap;
|
||||
params.scoreFunction =
|
||||
[&](const FeatureSettingsMap& featureSettings)
|
||||
{
|
||||
[&](const FeatureSettingsMap& featureSettings) {
|
||||
FeaturesSearcher::TrainSettings settings{ trainSettings };
|
||||
settings.featureSettingsMap = featureSettings;
|
||||
|
||||
@@ -454,13 +439,13 @@ int main(int argc, char* argv[])
|
||||
GeneticAlgorithm<FeatureSettingsMap> geneticAlgorithm{ params };
|
||||
|
||||
std::cout << "Parameters:\n"
|
||||
<< "\tnb total settings = " << featuresSettings.size() << "\n"
|
||||
<< "\tnb generations = " << params.nbGenerations << "\n"
|
||||
<< "\tpopulationSize = " << populationSize << "\n"
|
||||
<< "\tnbFeatures = " << nbFeatures << "\n"
|
||||
<< "\tcrossoverRatio = " << params.crossoverRatio << "\n"
|
||||
<< "\tmutationProbability = " << params.mutationProbability << "\n"
|
||||
<< std::endl;
|
||||
<< "\tnb total settings = " << featuresSettings.size() << "\n"
|
||||
<< "\tnb generations = " << params.nbGenerations << "\n"
|
||||
<< "\tpopulationSize = " << populationSize << "\n"
|
||||
<< "\tnbFeatures = " << nbFeatures << "\n"
|
||||
<< "\tcrossoverRatio = " << params.crossoverRatio << "\n"
|
||||
<< "\tmutationProbability = " << params.mutationProbability << "\n"
|
||||
<< std::endl;
|
||||
|
||||
std::cout << "Starting simulation..." << std::endl;
|
||||
const FeatureSettingsMap selectedSettings{ geneticAlgorithm.simulate(initialPopulation) };
|
||||
@@ -483,5 +468,3 @@ int main(int argc, char* argv[])
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -19,29 +19,29 @@
|
||||
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
|
||||
template <typename It, typename Func>
|
||||
template<typename It, typename Func>
|
||||
void parallel_foreach(std::size_t nbWorkers, It begin, It end, Func&& func)
|
||||
{
|
||||
if (nbWorkers == 0)
|
||||
throw std::runtime_error("Invalid worker count");
|
||||
if (nbWorkers == 0)
|
||||
throw std::runtime_error("Invalid worker count");
|
||||
|
||||
boost::asio::io_context ioContext;
|
||||
boost::asio::io_context ioContext;
|
||||
|
||||
for (It it {begin}; it != end; ++it)
|
||||
{
|
||||
auto refValue {std::ref<typename It::value_type>(*it)};
|
||||
ioContext.post([refValue, &func]() { std::cout << "EXEC FROM WORKER" << std::endl; func(refValue); std::cout << "END EXEC FROM WORKER" << std::endl; });
|
||||
}
|
||||
for (It it{ begin }; it != end; ++it)
|
||||
{
|
||||
auto refValue{ std::ref<typename It::value_type>(*it) };
|
||||
ioContext.post([refValue, &func]() { std::cout << "EXEC FROM WORKER" << std::endl; func(refValue); std::cout << "END EXEC FROM WORKER" << std::endl; });
|
||||
}
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
for (std::size_t i {}; i < nbWorkers - 1; ++i)
|
||||
threads.emplace_back([&]() { ioContext.run(); });
|
||||
std::vector<std::thread> threads;
|
||||
for (std::size_t i{}; i < nbWorkers - 1; ++i)
|
||||
threads.emplace_back([&]() { ioContext.run(); });
|
||||
|
||||
ioContext.run();
|
||||
ioContext.run();
|
||||
|
||||
for (std::thread& t : threads)
|
||||
t.join();
|
||||
for (std::thread& t : threads)
|
||||
t.join();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user