Commit ec9025ff authored by Jason Rhinelander's avatar Jason Rhinelander

Poll: throw exception in election periods

parent f3a356d0
......@@ -14,13 +14,8 @@ class Party : public PositionalAgent {
public:
using PositionalAgent::PositionalAgent;
/** The ID if the pollster. If this is not set when needed, it'll be updated to the first
* Poll agent returned from the simulation. If you want to do anything with multipler
* pollsters, you'll have to make sure this gets set to the right one rather than rely on
* the automatic updating.
/** Overridden to make movements bind to the position boundaries.
*/
eris_id_t pollster = 0;
virtual bool moveToBoundary() const noexcept override { return true; }
protected:
......
#include "voting/PartyMover.hpp"
#include "voting/Election.hpp"
#include <random>
#include <eris/Random.hpp>
#include "voting/Party.hpp"
......@@ -24,19 +25,23 @@ void PartyMover::optimize() const {
double PartyMover::should_move() const {
auto party = simAgent<Party>(party_);
double step_size = step_gen_();
if (not party->pollster) {
auto pollsters = simulation()->agentFilter<Poll>();
if (pollsters.empty())
throw std::runtime_error("PartyMover optimizer failure: simulation has no Poll agent\n");
party->pollster = pollsters.begin()->first;
for (auto &election : simulation()->agentFilter<Election>()) {
if (election.second->electionPeriod()) {
// No movement during an election period.
return 0.0;
}
}
auto pollster = simAgent<Poll>(party->pollster);
auto pollsters = simulation()->agentFilter<Poll>();
if (pollsters.empty())
throw std::runtime_error("PartyMover optimizer failure: simulation has no Poll agent\n");
auto pollster = pollsters.begin()->second;
std::unordered_map<double, int> polls;
double step_size = step_gen_();
// Conduct three polls: a "don't move", one step left, and one step right.
polls[0.0] = pollster->conductPoll().closest_party[party];
if (!party->bindingLower())
polls[-step_size] = pollster->conductPollIf(
......@@ -68,8 +73,10 @@ double PartyMover::should_move() const {
}
void PartyMover::apply() {
auto party = simAgent<Party>(party_);
party->moveBy(move_);
if (move_ != 0.0) {
auto party = simAgent<Party>(party_);
party->moveBy(move_);
}
}
}
#include "voting/Poll.hpp"
#include "voting/Election.hpp"
#include "voting/Party.hpp"
#include "voting/Voter.hpp"
#include <eris/Random.hpp>
......@@ -9,6 +10,11 @@ namespace voting {
Poll::poll Poll::conductPollIf(eris_id_t party_id, Position try_pos) {
// First check to make sure this isn't an election period:
for (auto &election : simulation()->agentFilter<Election>()) {
if (election.second->electionPeriod())
throw std::runtime_error("Cannot poll during an election period");
}
auto parties = simulation()->agentFilter<Party>();
auto voters = simulation()->agentFilter<Voter>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment