File indexing completed on 2024-05-05 04:48:47
0001 /**************************************************************************************** 0002 * Copyright (c) 2008-2012 Soren Harward <stharward@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) any later * 0007 * version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU General Public License along with * 0014 * this program. If not, see <http://www.gnu.org/licenses/>. * 0015 ****************************************************************************************/ 0016 0017 #ifndef APG_CONSTRAINTSOLVER 0018 #define APG_CONSTRAINTSOLVER 0019 0020 #include "core/meta/forward_declarations.h" 0021 0022 #include <QHash> 0023 #include <QMutex> 0024 #include <QString> 0025 0026 #include <ThreadWeaver/Job> 0027 0028 class ConstraintNode; 0029 0030 namespace Collections { 0031 class QueryMaker; 0032 } 0033 0034 namespace APG { 0035 class ConstraintSolver : public QObject, public ThreadWeaver::Job 0036 { 0037 Q_OBJECT 0038 0039 public: 0040 typedef QHash<Meta::TrackList*, double> Population; 0041 0042 static const int QUALITY_RANGE; // allows used to adjust speed/quality tradeoff 0043 0044 ConstraintSolver( ConstraintNode*, int ); 0045 ~ConstraintSolver() override; 0046 0047 Meta::TrackList getSolution() const; 0048 bool satisfied() const; 0049 int serial() const { return m_serialNumber; } 0050 int iterationCount() const { return m_maxGenerations; } 0051 0052 // overloaded ThreadWeaver::Job functions 0053 bool canBeExecuted(); 0054 bool success() const override; 0055 void requestAbort() override; 0056 0057 Q_SIGNALS: 0058 void readyToRun(); 0059 void incrementProgress(); 0060 void totalSteps( int ); 0061 void endProgressOperation( QObject* ); 0062 0063 /** This signal is emitted when this job is being processed by a thread. */ 0064 void started(ThreadWeaver::JobPointer); 0065 /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */ 0066 void done(ThreadWeaver::JobPointer); 0067 /** This job has failed. 0068 * This signal is emitted when success() returns false after the job is executed. */ 0069 void failed(ThreadWeaver::JobPointer); 0070 0071 protected: 0072 void defaultBegin(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override; 0073 void defaultEnd(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override; 0074 void run(ThreadWeaver::JobPointer self = QSharedPointer<ThreadWeaver::Job>(), ThreadWeaver::Thread *thread = nullptr) override; // from ThreadWeaver::Job 0075 0076 private Q_SLOTS: 0077 void receiveQueryMakerData( const Meta::TrackList &); 0078 void receiveQueryMakerDone(); 0079 0080 private: 0081 int m_serialNumber; // a randomly-generated serial number to help with debugging 0082 double m_satisfactionThreshold; 0083 double m_finalSatisfaction; 0084 0085 ConstraintNode* const m_constraintTreeRoot; 0086 Collections::QueryMaker* m_qm; 0087 0088 mutable Meta::TrackList m_domain; // tracks available to solver 0089 QMutex m_domainMutex; 0090 bool m_domainReductionFailed; 0091 Meta::TrackList m_solvedPlaylist; // playlist generated by solver 0092 0093 bool m_readyToRun; // job execution depends on QueryMaker finishing 0094 bool m_abortRequested; 0095 0096 // internal mathematical functions 0097 void fill_population( Population& ); 0098 Meta::TrackList* find_best( const Population& ) const; 0099 void select_population( Population&, Meta::TrackList* ); 0100 void mutate_population( Population& ); 0101 0102 Meta::TrackList* crossover( Meta::TrackList*, Meta::TrackList* ) const; 0103 static bool pop_comp( double, double ); 0104 Meta::TrackPtr random_track_from_domain() const; 0105 Meta::TrackList sample( Meta::TrackList, const int ) const; 0106 0107 double rng_gaussian( const double, const double ) const; 0108 quint32 rng_poisson( const double ) const; 0109 double rng_uniform() const; 0110 0111 quint32 playlist_size() const; 0112 bool select( const double ) const; 0113 0114 // convenience function 0115 void dump_population( const Population& ) const; 0116 0117 // internal mathematical parameters 0118 quint32 m_maxGenerations; 0119 quint32 m_populationSize; 0120 quint32 m_suggestedPlaylistSize; 0121 }; 0122 } // namespace APG 0123 0124 #endif