Skip to content

Commit

Permalink
Solvers constructor now returns a unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
trolando committed Jul 24, 2024
1 parent 05e7b79 commit e9b7dd9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 56 deletions.
40 changes: 18 additions & 22 deletions include/oink/solvers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <vector>
#include <map>
#include <set>
#include <memory>

#ifndef SOLVERS_HPP
#define SOLVERS_HPP
Expand All @@ -29,42 +30,45 @@ class Oink;
class Game;
class Solver;

class Solvers
class Solvers final
{
public:
using SolverConstructor = std::function<Solver*(Oink&, Game&)>;
using SolverConstructor = std::function<std::unique_ptr<Solver>(Oink&, Game&)>;

~Solvers() = default;
Solvers(const Solvers&) = delete;
Solvers(const Solvers&&) = delete;
Solvers& operator=(const Solvers&) = delete;
Solvers& operator=(const Solvers&&) = delete;

/**
* Get number of solvers
*/
static unsigned count() { return instance().solvers.size(); }
static unsigned count()
{
return instance().solvers.size();
}

/**
* Get description of solver <id>
*/
static std::string desc(const std::string& id)
{
return instance().solvers[id].description;
{
return instance().solvers[id].description;
}

/**
* Get whether solver <id> can be run in parallel
*/
static bool isParallel(const std::string& id)
static bool isParallel(const std::string& id)
{
return instance().solvers[id].isParallel;
return instance().solvers[id].isParallel;
}

/**
* Construct solver with the given parameters
*/
static Solver* construct(const std::string& id, Oink& oink, Game& game)
{
return instance().solvers[id].constructor(oink, game);
}
static std::unique_ptr<Solver> construct(const std::string& id, Oink& oink, Game& game);

/**
* Write a formatted list of all solvers to the given ostream
Expand All @@ -74,16 +78,8 @@ class Solvers
/**
* Add a solver to the set of solvers
*/
static void add(const std::string& id, const std::string& description, bool isParallel, const SolverConstructor& constructor)
{
instance().solvers[id] = {description, isParallel, constructor};
}
static void add(const std::string& id, const std::string& description, bool isParallel, const SolverConstructor& constructor);

static SolverConstructor get(const std::string& id)
{
return instance().solvers[id].constructor;
}

static std::set<std::string> getSolverIDs()
{
std::set<std::string> ids;
Expand Down Expand Up @@ -112,12 +108,12 @@ class Solvers
/**
* Add a solver to the set of solvers
*/
void _add(const std::string& id, const std::string& description, bool isParallel, const SolverConstructor& constructor)
void _add(const std::string& id, const std::string& description, bool isParallel, const SolverConstructor& constructor)
{
solvers[id] = {description, isParallel, constructor};
}
};

}

#endif
#endif
2 changes: 0 additions & 2 deletions src/oink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ Oink::solveLoop()
exit(-1);
}
s->run();
delete s;

// flush the todo buffer
flush();
Expand All @@ -413,7 +412,6 @@ Oink::solveLoop()
exit(-1);
}
s->run();
delete s;

if (fullSolver) {
// trash the todo buffer
Expand Down
79 changes: 47 additions & 32 deletions src/solvers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,38 @@ namespace pg {

Solvers::Solvers()
{
_add("zlkq", "qpt Zielonka", 0, [] (Oink& oink, Game& game) { return new ZLKQSolver(&oink, &game); });
_add("zlk", "parallel Zielonka", 1, [] (Oink& oink, Game& game) { return new ZLKSolver(&oink, &game); });
_add("uzlk", "unoptimized Zielonka", 0, [] (Oink& oink, Game& game) { return new UnoptimizedZLKSolver(&oink, &game); });
_add("zlkpp-std", "Zielonka (implementation by Paweł Parys)", 0, [] (Oink& oink, Game& game) { return new ZLKPPSolver(&oink, &game, ZLK_STANDARD); });
_add("zlkpp-waw", "Warsaw quasipolynomial Zielonka (implementation by Paweł Parys)", 0, [] (Oink& oink, Game& game) { return new ZLKPPSolver(&oink, &game, ZLK_WARSAW); });
_add("zlkpp-liv", "Liverpool quasipolynomial Zielonka (implementation by Paweł Parys)", 0, [] (Oink& oink, Game& game) { return new ZLKPPSolver(&oink, &game, ZLK_LIVERPOOL); });
_add("npp", "priority promotion NPP", 0, [] (Oink& oink, Game& game) { return new NPPSolver(&oink, &game); });
_add("pp", "priority promotion PP", 0, [] (Oink& oink, Game& game) { return new PPSolver(&oink, &game); });
_add("ppp", "priority promotion PP+", 0, [] (Oink& oink, Game& game) { return new PPPSolver(&oink, &game); });
_add("rr", "priority promotion RR", 0, [] (Oink& oink, Game& game) { return new RRSolver(&oink, &game); });
_add("dp", "priority promotion PP+ with DP strategy", 0, [] (Oink& oink, Game& game) { return new DPSolver(&oink, &game); });
_add("rrdp", "priority promotion RR with DP strategy", 0, [] (Oink& oink, Game& game) { return new RRDPSolver(&oink, &game); });
_add("ppq", "qpt Zielonka accelerated by priority promotion", 0, [] (Oink& oink, Game& game) { return new PPQSolver(&oink, &game); });
_add("fpi", "fixpoint iteration", 1, [] (Oink& oink, Game& game) { return new FPISolver(&oink, &game); });
_add("fpj", "fixpoint iteration with justifications", 0, [] (Oink& oink, Game& game) { return new FPJSolver(&oink, &game); });
_add("fpjg", "greedy fixpoint iteration with justifications", 1, [] (Oink& oink, Game& game) { return new FPJGSolver(&oink, &game); });
_add("psi", "parallel strategy improvement", 1, [] (Oink& oink, Game& game) { return new PSISolver(&oink, &game); });
_add("ssi", "symmetric strategy improvement", 0, [] (Oink& oink, Game& game) { return new SSISolver(&oink, &game); });
_add("spm", "accelerated small progress measures", 0, [] (Oink& oink, Game& game) { return new SPMSolver(&oink, &game); });
_add("tspm", "traditional small progress measures", 0, [] (Oink& oink, Game& game) { return new TSPMSolver(&oink, &game); });
_add("mspm", "Maciej' modified small progress measures", 0, [] (Oink& oink, Game& game) { return new MSPMSolver(&oink, &game); });
_add("sspm", "succinct small progress measures", 0, [] (Oink& oink, Game& game) { return new SSPMSolver(&oink, &game); });
_add("bsspm", "bounded succinct small progress measures", 0, [] (Oink& oink, Game& game) { return new BoundedSSPMSolver(&oink, &game); });
_add("qpt", "quasi-polynomial time progress measures", 0, [] (Oink& oink, Game& game) { return new QPTSolver(&oink, &game); });
_add("bqpt", "bounded quasi-polynomial time progress measures", 0, [] (Oink& oink, Game& game) { return new BoundedQPTSolver(&oink, &game); });
_add("ptl", "progressive tangle learning", 0, [] (Oink& oink, Game& game) { return new PTLSolver(&oink, &game); });
_add("spptl", "single-player progressive tangle learning", 0, [] (Oink& oink, Game& game) { return new SPPTLSolver(&oink, &game); });
_add("dtl", "distance tangle learning", 0, [] (Oink& oink, Game& game) { return new DTLSolver(&oink, &game); });
_add("idtl", "interleaved distance tangle learning", 0, [] (Oink& oink, Game& game) { return new IDTLSolver(&oink, &game); });
_add("rtl", "recursive tangle learning", 0, [] (Oink& oink, Game& game) { return new RTLSolver(&oink, &game); });
_add("ortl", "one-sided recursive tangle learning", 0, [] (Oink& oink, Game& game) { return new ORTLSolver(&oink, &game); });
_add("tl", "tangle learning", 0, [] (Oink& oink, Game& game) { return new TLSolver(&oink, &game); });
_add("zlkq", "qpt Zielonka", 0, [] (Oink& oink, Game& game) { return std::make_unique<ZLKQSolver>(&oink, &game); });
_add("zlk", "parallel Zielonka", 1, [] (Oink& oink, Game& game) { return std::make_unique<ZLKSolver>(&oink, &game); });
_add("uzlk", "unoptimized Zielonka", 0, [] (Oink& oink, Game& game) { return std::make_unique<UnoptimizedZLKSolver>(&oink, &game); });
_add("zlkpp-std", "Zielonka (implementation by Paweł Parys)", 0, [] (Oink& oink, Game& game) { return std::make_unique<ZLKPPSolver>(&oink, &game, ZLK_STANDARD); });
_add("zlkpp-waw", "Warsaw quasipolynomial Zielonka (implementation by Paweł Parys)", 0, [] (Oink& oink, Game& game) { return std::make_unique<ZLKPPSolver>(&oink, &game, ZLK_WARSAW); });
_add("zlkpp-liv", "Liverpool quasipolynomial Zielonka (implementation by Paweł Parys)", 0, [] (Oink& oink, Game& game) { return std::make_unique<ZLKPPSolver>(&oink, &game, ZLK_LIVERPOOL); });
_add("npp", "priority promotion NPP", 0, [] (Oink& oink, Game& game) { return std::make_unique<NPPSolver>(&oink, &game); });
_add("pp", "priority promotion PP", 0, [] (Oink& oink, Game& game) { return std::make_unique<PPSolver>(&oink, &game); });
_add("ppp", "priority promotion PP+", 0, [] (Oink& oink, Game& game) { return std::make_unique<PPPSolver>(&oink, &game); });
_add("rr", "priority promotion RR", 0, [] (Oink& oink, Game& game) { return std::make_unique<RRSolver>(&oink, &game); });
_add("dp", "priority promotion PP+ with DP strategy", 0, [] (Oink& oink, Game& game) { return std::make_unique<DPSolver>(&oink, &game); });
_add("rrdp", "priority promotion RR with DP strategy", 0, [] (Oink& oink, Game& game) { return std::make_unique<RRDPSolver>(&oink, &game); });
_add("ppq", "qpt Zielonka accelerated by priority promotion", 0, [] (Oink& oink, Game& game) { return std::make_unique<PPQSolver>(&oink, &game); });
_add("fpi", "fixpoint iteration", 1, [] (Oink& oink, Game& game) { return std::make_unique<FPISolver>(&oink, &game); });
_add("fpj", "fixpoint iteration with justifications", 0, [] (Oink& oink, Game& game) { return std::make_unique<FPJSolver>(&oink, &game); });
_add("fpjg", "greedy fixpoint iteration with justifications", 1, [] (Oink& oink, Game& game) { return std::make_unique<FPJGSolver>(&oink, &game); });
_add("psi", "parallel strategy improvement", 1, [] (Oink& oink, Game& game) { return std::make_unique<PSISolver>(&oink, &game); });
_add("ssi", "symmetric strategy improvement", 0, [] (Oink& oink, Game& game) { return std::make_unique<SSISolver>(&oink, &game); });
_add("spm", "accelerated small progress measures", 0, [] (Oink& oink, Game& game) { return std::make_unique<SPMSolver>(&oink, &game); });
_add("tspm", "traditional small progress measures", 0, [] (Oink& oink, Game& game) { return std::make_unique<TSPMSolver>(&oink, &game); });
_add("mspm", "Maciej' modified small progress measures", 0, [] (Oink& oink, Game& game) { return std::make_unique<MSPMSolver>(&oink, &game); });
_add("sspm", "succinct small progress measures", 0, [] (Oink& oink, Game& game) { return std::make_unique<SSPMSolver>(&oink, &game); });
_add("bsspm", "bounded succinct small progress measures", 0, [] (Oink& oink, Game& game) { return std::make_unique<BoundedSSPMSolver>(&oink, &game); });
_add("qpt", "quasi-polynomial time progress measures", 0, [] (Oink& oink, Game& game) { return std::make_unique<QPTSolver>(&oink, &game); });
_add("bqpt", "bounded quasi-polynomial time progress measures", 0, [] (Oink& oink, Game& game) { return std::make_unique<BoundedQPTSolver>(&oink, &game); });
_add("ptl", "progressive tangle learning", 0, [] (Oink& oink, Game& game) { return std::make_unique<PTLSolver>(&oink, &game); });
_add("spptl", "single-player progressive tangle learning", 0, [] (Oink& oink, Game& game) { return std::make_unique<SPPTLSolver>(&oink, &game); });
_add("dtl", "distance tangle learning", 0, [] (Oink& oink, Game& game) { return std::make_unique<DTLSolver>(&oink, &game); });
_add("idtl", "interleaved distance tangle learning", 0, [] (Oink& oink, Game& game) { return std::make_unique<IDTLSolver>(&oink, &game); });
_add("rtl", "recursive tangle learning", 0, [] (Oink& oink, Game& game) { return std::make_unique<RTLSolver>(&oink, &game); });
_add("ortl", "one-sided recursive tangle learning", 0, [] (Oink& oink, Game& game) { return std::make_unique<ORTLSolver>(&oink, &game); });
_add("tl", "tangle learning", 0, [] (Oink& oink, Game& game) { return std::make_unique<TLSolver>(&oink, &game); });
}

void
Expand All @@ -89,4 +89,19 @@ Solvers::list(std::ostream &out)
}
}

/**
* Construct solver with the given parameters
*/
std::unique_ptr<Solver>
Solvers::construct(const std::string& id, Oink& oink, Game& game)
{
return instance().solvers[id].constructor(oink, game);
}

void
Solvers::add(const std::string& id, const std::string& description, bool isParallel, const SolverConstructor& constructor)
{
instance().solvers[id] = {description, isParallel, constructor};
}

}

0 comments on commit e9b7dd9

Please sign in to comment.