SGSolve
sgsolverworker.hpp
1 // This file is part of the SGSolve library for stochastic games
2 // Copyright (C) 2019 Benjamin A. Brooks
3 //
4 // SGSolve free software: you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // SGSolve is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see
16 // <http://www.gnu.org/licenses/>.
17 //
18 // Benjamin A. Brooks
19 // ben@benjaminbrooks.net
20 // Chicago, IL
21 
22 #ifndef SGSOLVERWORKER_V2_HPP
23 #define SGSOLVERWORKER_V2_HPP
24 
25 #include <QtWidgets>
26 #include "sg.hpp"
27 #include "sgsolver_maxminmax.hpp"
28 
30 
46 class SGSolverWorker : public QObject
47 {
48  Q_OBJECT;
49 
50 private:
52  const SGEnv & env;
56  QTextEdit * logTextEdit;
58  int numIter;
60  QString exceptionMsg;
61 
62 public:
64  enum STATUS
65  {
66  FAILED,
68  CONVERGED,
72  } status;
74 
77  SGSolverWorker(const SGEnv & _env,
78  const SGGame & game,
79  QTextEdit * _logTextEdit):
80  env(_env), solver(env,game),
81  logTextEdit(_logTextEdit)
82  {
83  numIter = 0;
85  } // constructor
86 
88  STATUS getStatus() const { return status; }
90  const QString & getExceptionMsg() const { return exceptionMsg; }
91 
92 public slots:
93 
95 
97  void iterate()
98  {
99  try
100  {
102  {
103  numIter++;
105  {
107  emit resultReady(false);
108  }
109  else
110  {
111  status = CONVERGED;
112  emit resultReady(true);
113  }
114  }
115  else
117  }
118  catch (exception & e)
119  {
120  // Add the extreme tuples array to soln.
121  qDebug() << "solve failed" << endl;
122 
123  exceptionMsg = QString(e.what());
124 
125  status = FAILED;
126  emit resultReady(true);
127  }
128  } // iterate
129 
132  { return solver.getSolution(); }
133 
136  { return solver; }
137 
138 signals:
140  void resultReady(bool);
142 
144 };
145 
146 #endif
SGSolver_MaxMinMax::getSolution
const SGSolution_MaxMinMax & getSolution() const
Definition: sgsolver_maxminmax.hpp:191
SGEnv::getParam
double getParam(SG::DBL_PARAM param) const
Method for getting double parameters.
Definition: sgenv.cpp:96
SGSolverWorker::status
enum SGSolverWorker::STATUS status
SGSolverWorker::solver
SGSolver_MaxMinMax solver
The main object for performing calculations.
Definition: sgsolverworker.hpp:54
SGSolverWorker
Class for implementing the twist algorithm within SGViewer.
Definition: sgsolverworker.hpp:47
SGGame
Describes a stochastic game.
Definition: sggame.hpp:40
SGSolverWorker::getSolver
const SGSolver_MaxMinMax & getSolver() const
Returns the solver object.
Definition: sgsolverworker.hpp:135
SGSolverWorker::exceptionMsg
QString exceptionMsg
Exception message caught by solver.
Definition: sgsolverworker.hpp:60
SGException
SGSolve specific exceptions.
Definition: sgexception.hpp:36
SGSolverWorker::getStatus
STATUS getStatus() const
Returns the status of the worker.
Definition: sgsolverworker.hpp:88
SGSolverWorker::resultReady
void resultReady(bool)
Signal that gets emitted when iteration finishes.
SGSolverWorker::exceptionCaught
void exceptionCaught()
Signal that gets emitted when an exception is caught.
SG::MAXITERATIONS
@ MAXITERATIONS
Definition: sgnamespace.hpp:143
SGSolver_MaxMinMax::iterate
double iterate()
One iteration of the endogenous algorith.
Definition: sgsolver_maxminmax.cpp:270
SGSolverWorker::FAILED
@ FAILED
Definition: sgsolverworker.hpp:66
SGSolverWorker::getExceptionMsg
const QString & getExceptionMsg() const
Returns the exception message (if one is caught)
Definition: sgsolverworker.hpp:90
SGSolver_MaxMinMax
Class for solving stochastic games.
Definition: sgsolver_maxminmax.hpp:43
SG::ERRORTOL
@ ERRORTOL
Definition: sgnamespace.hpp:69
SGSolution_MaxMinMax
Records the progress of SGSolver_MaxMinMax::solve().
Definition: sgsolution_maxminmax.hpp:40
SGEnv
Manages parameters for algorithm behavior.
Definition: sgenv.hpp:35
SGSolverWorker::CONVERGED
@ CONVERGED
Definition: sgsolverworker.hpp:68
SGSolverWorker::logTextEdit
QTextEdit * logTextEdit
A pointer to the text edit in which to report progress.
Definition: sgsolverworker.hpp:56
SGSolverWorker::getSolution
const SGSolution_MaxMinMax & getSolution() const
Returns the SGSolution object.
Definition: sgsolverworker.hpp:131
SGSolverWorker::env
const SGEnv & env
An environment object to hold settings.
Definition: sgsolverworker.hpp:48
SGSolverWorker::iterate
void iterate()
Iterates.
Definition: sgsolverworker.hpp:97
SG::MAX_ITERATIONS_REACHED
@ MAX_ITERATIONS_REACHED
Definition: sgnamespace.hpp:59
SGSolverWorker::NOTCONVERGED
@ NOTCONVERGED
Definition: sgsolverworker.hpp:70
SGSolverWorker::SGSolverWorker
SGSolverWorker(const SGEnv &_env, const SGGame &game, QTextEdit *_logTextEdit)
Constructor.
Definition: sgsolverworker.hpp:77
SGSolver_MaxMinMax::initialize
void initialize()
Initializes the solve routines.
Definition: sgsolver_maxminmax.cpp:470
SGSolverWorker::numIter
int numIter
Number of iterations.
Definition: sgsolverworker.hpp:58
SGSolverWorker::STATUS
STATUS
Code for status at the end of the iteration.
Definition: sgsolverworker.hpp:65