Skip to content

Commit

Permalink
small changes in cross validation
Browse files Browse the repository at this point in the history
  • Loading branch information
conradhuebler committed Jul 14, 2019
1 parent 2b4034c commit 0184432
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 56 deletions.
2 changes: 1 addition & 1 deletion external/CuteChart
7 changes: 5 additions & 2 deletions src/capabilities/abstractsearchclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include "src/core/jsonhandler.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QHash>

#include "abstractsearchclass.h"

AbstractSearchClass::AbstractSearchClass(QObject* parent)
Expand Down Expand Up @@ -76,12 +79,12 @@ QJsonObject AbstractSearchClass::Result() const
return result;
}

QVector<Pair> AbstractSearchClass::DemandCalc()
QHash<int, Pair> AbstractSearchClass::DemandCalc()
{
QMutexLocker lock(&mutex);

if (m_batch.isEmpty())
return QVector<Pair>();
return QHash<int, Pair>();
else
return m_batch.dequeue();
}
18 changes: 5 additions & 13 deletions src/capabilities/abstractsearchclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "src/core/AbstractModel.h"

#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QPointF>
#include <QtCore/QQueue>
Expand All @@ -32,18 +33,7 @@
typedef QPair<QPointer<DataTable>, QPointer<DataTable>> Pair;

class AbstractModel;
/*
class AbstractConfig {

public:
inline AbstractConfig(QJsonObject config = OptimConfigBlock)
: optimizer_config(config)
{
}
inline ~AbstractConfig() {}
QJsonObject optimizer_config;
};
*/
class AbstractSearchThread : public QObject, public QRunnable {
Q_OBJECT

Expand All @@ -56,6 +46,7 @@ class AbstractSearchThread : public QObject, public QRunnable {
inline ~AbstractSearchThread() { m_model.clear(); }
inline void setModel(const QSharedPointer<AbstractModel> model) { m_model = model->Clone(); }
inline void setController(const QJsonObject& controller) { m_controller = controller; }
inline QHash<int, QJsonObject> Models() const { return m_models; }

public slots:
inline virtual void Interrupt() { m_interrupt = true; }
Expand All @@ -64,6 +55,7 @@ public slots:
QSharedPointer<AbstractModel> m_model;
bool m_interrupt;
QJsonObject m_controller;
QHash<int, QJsonObject> m_models;

signals:
void IncrementProgress(int msecs);
Expand All @@ -90,7 +82,7 @@ class AbstractSearchClass : public QObject {
QJsonObject Result() const;
void ExportResults(const QString& filename);

QVector<Pair> DemandCalc();
QHash<int, Pair> DemandCalc();

virtual void clear() {}
public slots:
Expand All @@ -105,7 +97,7 @@ public slots:
QThreadPool* m_threadpool;
QList<QList<QPointF>> m_series;
bool m_interrupt;
QQueue<QVector<Pair>> m_batch;
QQueue<QHash<int, Pair>> m_batch;

virtual QJsonObject Controller() const { return m_controller; }
QMutex mutex;
Expand Down
3 changes: 0 additions & 3 deletions src/capabilities/globalsearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ class SearchBatch : public AbstractSearchThread {
QPointer<GlobalSearch> m_parent;
QList<QJsonObject> m_result;
bool m_finished, m_checked;
/*
protected:
QSharedPointer<AbstractModel> m_model; */
};

class GlobalSearch : public AbstractSearchClass {
Expand Down
14 changes: 7 additions & 7 deletions src/capabilities/montecarlostatistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void MonteCarloBatch::run()
while (true) {
if (m_interrupt)
break;
QVector<QPair<QPointer<DataTable>, QPointer<DataTable>>> tables = m_parent->DemandCalc();
QHash<int, Pair> tables = m_parent->DemandCalc();
int counter = 0;
for (const QPair<QPointer<DataTable>, QPointer<DataTable>>& table : tables) {
if (table.first && table.second) {
Expand All @@ -111,7 +111,7 @@ void MonteCarloBatch::run()
else
m_model->OverrideCheckedTable(table.second);

optimise();
optimise(tables.key(table));
counter++;
} else
continue;
Expand All @@ -122,7 +122,7 @@ void MonteCarloBatch::run()
delete m_fit_thread;
}

void MonteCarloBatch::optimise()
void MonteCarloBatch::optimise(int key)
{
if (!m_model || m_interrupt) {
qDebug() << "no model set";
Expand All @@ -144,7 +144,7 @@ void MonteCarloBatch::optimise()
m_model->Calculate();

m_model->setConverged(m_finished);
m_models << m_model->ExportModel(false, false); //;
m_models.insert(key, m_model->ExportModel(false, false)); //;

qint64 t1 = QDateTime::currentMSecsSinceEpoch();
emit IncrementProgress(t1 - t0);
Expand Down Expand Up @@ -238,7 +238,7 @@ QVector<QPointer<MonteCarloBatch>> MonteCarloStatistics::GenerateData()
qDebug() << "Starting MC Simulation with" << MaxSteps << "steps";
#endif

QVector<Pair> block;
QHash<int, Pair> block;
for (int step = 0; step < MaxSteps; ++step) {
QPointer<DataTable> dep_table;
if (original)
Expand Down Expand Up @@ -268,7 +268,7 @@ QVector<QPointer<MonteCarloBatch>> MonteCarloStatistics::GenerateData()
}
}

block << Pair(indep_table, dep_table);
block.insert(step, Pair(indep_table, dep_table));
if (block.size() == blocksize) {
m_batch.enqueue(block);
block.clear();
Expand All @@ -291,7 +291,7 @@ void MonteCarloStatistics::Collect(const QVector<QPointer<MonteCarloBatch>>& thr
m_steps = 0;
for (int i = 0; i < threads.size(); ++i) {
if (threads[i]) {
m_models << threads[i]->Models();
m_models << threads[i]->Models().values();
m_steps++;
delete threads[i];
}
Expand Down
4 changes: 1 addition & 3 deletions src/capabilities/montecarlostatistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,16 @@ class MonteCarloBatch : public AbstractSearchThread {
MonteCarloBatch(QPointer<AbstractSearchClass> parent);
virtual ~MonteCarloBatch() override;
virtual void run() override;
inline QList<QJsonObject> Models() const { return m_models; }
inline bool Finished() const { return m_finished; }
inline void setChecked(bool checked) { m_checked = checked; }

private:
void optimise();
void optimise(int key = 0);
NonLinearFitThread* m_fit_thread;

QPointer<AbstractSearchClass> m_parent;
bool m_finished, m_checked;
QJsonObject m_controller;
QList<QJsonObject> m_models;
};

class MonteCarloStatistics : public AbstractSearchClass {
Expand Down
70 changes: 55 additions & 15 deletions src/capabilities/resampleanalyse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*
*/

#include <QtCore/QCoreApplication>
#include <QtCore/QHash>
#include <QtCore/QMutexLocker>

#include <QtWidgets/QApplication>

#include "src/core/AbstractModel.h"
#include "src/core/minimizer.h"
#include "src/core/toolset.h"
Expand Down Expand Up @@ -65,43 +65,58 @@ bool ResampleAnalyse::Pending() const
void ResampleAnalyse::CrossValidation()
{
int type = m_controller["CXO"].toInt();
QVector<Pair> block;
QHash<int, Pair> block;
int blocksize;
int maxthreads = qApp->instance()->property("threads").toInt();
QPointer<DataTable> table = new DataTable(m_model->DependentModel());
QVector<QPointer<MonteCarloBatch>> threads;
QVector<QVector<qreal>> individual_results(m_model->DataPoints());
QList<qreal> x;

int index = 0;
switch (type) {
case 1:
emit setMaximumSteps(m_model->DataPoints());
blocksize = 1;
for (int i = m_model->DataPoints() - 1; i >= 0; --i) {
for (int i = 0; i < m_model->DataPoints(); ++i) {
QPointer<DataTable> dep_table = new DataTable(table);
QVector<int> indicies = QVector<int>() << i;
Vector vector = dep_table->DisableRow(i);
std::cout << vector.transpose() << " " << i << std::endl;
block << Pair(m_model->IndependentModel(), dep_table);
x << m_model->PrintOutIndependent(i);
// std::cout << vector.transpose() << " " << i << std::endl;

block.insert(index, Pair(m_model->IndependentModel(), dep_table));
m_job.insert(index, indicies);
if (block.size() == blocksize) {
m_batch.enqueue(block);
block.clear();
}
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
index++;
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
break;
case 2:
emit setMaximumSteps(m_model->DataPoints() * (m_model->DataPoints() - 1) / 2);
blocksize = 25;
for (int i = 0; i < m_model->DataPoints(); ++i)
for (int i = 0; i < m_model->DataPoints(); ++i) {
x << m_model->PrintOutIndependent(i);
for (int j = i + 1; j < m_model->DataPoints(); ++j) {
QPointer<DataTable> dep_table = new DataTable(table);
dep_table->DisableRow(i);
dep_table->DisableRow(j);
block << Pair(m_model->IndependentModel(), dep_table);
QVector<int> indicies = QVector<int>() << i << j;

block.insert(index, Pair(m_model->IndependentModel(), dep_table));
m_job.insert(index, indicies);

if (block.size() == blocksize) {
m_batch.enqueue(block);
block.clear();
}
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
index++;
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
}
break;

// case CVType::LeaveManyOut:
Expand All @@ -123,14 +138,39 @@ void ResampleAnalyse::CrossValidation()
}

while (Pending()) {
QApplication::processEvents();
QCoreApplication::processEvents();
}
QJsonObject chart_block;
for (int i = 0; i < threads.size(); ++i) {
if (threads[i]) {
m_models << threads[i]->Models();

QHash<int, QJsonObject> models = threads[i]->Models();

for (const QJsonObject& model : models) {
int index = models.key(model);
QVector<int> indicies = m_job.value(index);

m_model->ImportModel(model);
m_model->Calculate();
QString points = QString();
for (int j : indicies) {
points = points + ToolSet::DoubleList2String(m_model->ModelTable()->Row(j)) + "|";
}
points.truncate(points.size() - 1);
chart_block[ToolSet::IntVec2String(indicies)] = points;
m_models << model;
}
delete threads[i];
}
}

m_controller["chart"] = chart_block;
m_controller["xlabel"] = m_model.data()->XLabel();
m_controller["ylabel"] = m_model.data()->YLabel();
m_controller["series_count"] = m_model->SeriesCount();
m_controller["x"] = ToolSet::DoubleList2String(x);
m_controller["DependentModel"] = m_model->DependentModel()->ExportTable(true);

if (m_models.size()) {
m_results = ToolSet::Model2Parameter(m_models);
ToolSet::Parameter2Statistic(m_results, m_model.data());
Expand Down Expand Up @@ -160,7 +200,7 @@ void ResampleAnalyse::PlainReduction()
model->detach();
thread->setModel(model);
addThread(thread);
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
} else if (m_controller["ReductionRuntype"].toInt() == 2 || m_controller["runtype"].toInt() == 3) {
for (int i = 1; i < m_model->DataPoints() - 3; ++i) {
Expand All @@ -173,12 +213,12 @@ void ResampleAnalyse::PlainReduction()
model->detach();
thread->setModel(model);
addThread(thread);
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
}

while (Pending()) {
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
QList<qreal> x;
for (int i = 0; i < m_threads.size(); ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/capabilities/resampleanalyse.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public slots:
private:
void addThread(QPointer<MonteCarloThread> thread);
bool Pending() const;

QHash<int, QVector<int>> m_job;
QVector<QPointer<MonteCarloThread>> m_threads;
QJsonObject m_model_data;

Expand Down
3 changes: 1 addition & 2 deletions src/capabilities/weakenedgridsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
#include "src/core/minimizer.h"
#include "src/core/toolset.h"

#include <QCoreApplication>

#include <QtCore/QCoreApplication>
#include <QtCore/QDateTime>

#include <QtCore/QObject>
Expand Down
3 changes: 1 addition & 2 deletions src/core/minimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
#include "src/core/libmath.h"
#include "src/core/toolset.h"

#include <QCoreApplication>

#include <QtCore/QCoreApplication>
#include <QtCore/QDateTime>
#include <QtCore/QTimer>

Expand Down
Loading

0 comments on commit 0184432

Please sign in to comment.