1#ifndef CYCLUS_SRC_CONTEXT_H_
2#define CYCLUS_SRC_CONTEXT_H_
13#include <boost/uuid/uuid_generators.hpp>
41class RandomNumberGenerator;
68 std::string
handle, std::string d);
147 friend class ::SimInitTest;
161 boost::uuids::uuid
sim_id();
186 inline const std::set<Trader*>&
traders()
const {
196 if (protos_.count(proto_name) == 0) {
197 throw KeyError(
"Invalid prototype name " + proto_name);
200 Agent* m = protos_[proto_name];
202 throw KeyError(
"Null prototype for " + proto_name);
207 throw StateError(
"Clone operation failed for " + proto_name);
209 casted =
dynamic_cast<T*
>(clone);
210 if (casted == NULL) {
213 throw CastError(
"Invalid cast for prototype " + proto_name);
260 void AddPackage(std::string name,
double fill_min = 0,
261 double fill_max = std::numeric_limits<double>::max(),
262 std::string strategy =
"first");
274 int fill_max = std::numeric_limits<int>::max(),
275 std::string strategy =
"first");
297 double high=std::numeric_limits<double>::max());
301 int high=std::numeric_limits<int>::max());
304 inline uint64_t
dt() {
return si_.dt;};
307 inline uint64_t
seed() {
return si_.seed;};
310 inline uint64_t
stride() {
return si_.stride;};
334 if (solver_ == NULL) {
343 solver_->sim_ctx(
this);
349 return n_prototypes_[type];
355 return n_specs_[impl];
360 inline void RegisterAgent(
Agent* a) {
362 n_specs_[a->
spec()]++;
366 inline void UnregisterAgent(
Agent* a) {
367 n_prototypes_[a->prototype()]--;
368 n_specs_[a->spec()]--;
373 std::set<std::string> rec_ver_;
375 std::map<std::string, Agent*> protos_;
376 std::map<std::string, Composition::Ptr> recipes_;
377 std::map<std::string, Package::Ptr> packages_;
378 std::map<std::string, TransportUnit::Ptr> transport_units_;
379 std::set<Agent*> agent_list_;
380 std::set<Trader*> traders_;
381 std::map<std::string, int> n_prototypes_;
382 std::map<std::string, int> n_specs_;
386 ExchangeSolver* solver_;
389 RandomNumberGenerator* rng_;
The abstract base class used by all types of agents that live and interact in a simulation.
const std::string prototype() const
Returns the agent's prototype.
std::string spec()
The string representation of the agent spec that uniquely identifies the concrete agent class/module.
virtual Agent * Clone()=0
Returns a newly created/allocated prototype that is an exact copy of this.
virtual const int id() const
The agent instance's unique ID within a simulation.
For failed casts that shouldn't.
boost::shared_ptr< Composition > Ptr
void UnregisterTrader(Trader *e)
Unregisters an agent as a participant in resource exchanges.
Context(Timer *ti, Recorder *rec)
Creates a new context working with the specified timer and datum manager.
const std::set< Trader * > & traders() const
T * CreateAgent(std::string proto_name)
Create a new agent by cloning the named prototype.
void SchedBuild(Agent *parent, std::string proto_name, int t=-1)
Schedules the named prototype to be built for the specified parent at timestep t.
void KillSim()
Schedules the simulation to be terminated at the end of this timestep.
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
void RegisterTrader(Trader *e)
Registers an agent as a participant in resource exchanges.
boost::uuids::uuid sim_id()
See Recorder::sim_id documentation.
void Snapshot()
Schedules a snapshot of simulation state to output database to occur at the beginning of the next tim...
uint64_t stride()
Returns the stride for the random number generator.
ExchangeSolver * solver()
Returns the exchange solver associated with this context.
void AddPrototype(std::string name, Agent *m)
Adds a prototype to a simulation-wide accessible list, a prototype can not be added more than once.
void DelAgent(Agent *m)
Destructs and cleans up m (and it's children recursively).
Composition::Ptr GetRecipe(std::string name)
Retrieve a registered recipe.
void solver(ExchangeSolver *solver)
sets the solver associated with this context
SimInfo sim_info() const
Return static simulation info.
uint64_t dt()
Returns the duration of a single time step in seconds.
Package::Ptr GetPackage(std::string name)
Retrieve a registered package.
double random_01()
Generates a random number on the range [0,1)].
int random_normal_int(double mean, double std_dev, int low=0, int high=std::numeric_limits< int >::max())
Returns a random number from a lognormal distribution.
double random_normal_real(double mean, double std_dev, double low=0, double high=std::numeric_limits< double >::max())
Returns a random number from a normal distribution.
uint64_t seed()
Returns the seed for the random number generator.
int n_prototypes(std::string type)
int random_uniform_int(int low, int high)
Returns a random number from a uniform integer distribution.
~Context()
Clean up resources including destructing the solver and all agents the context is aware of.
void AddRecipe(std::string name, Composition::Ptr c)
Adds a composition recipe to a simulation-wide accessible list.
void AddTransportUnit(std::string name, int fill_min=0, int fill_max=std::numeric_limits< int >::max(), std::string strategy="first")
Adds a transport unit type to a simulation-wide accessible list.
void RecordTransportUnit(TransportUnit::Ptr)
Records transport unit information.
TransportUnit::Ptr GetTransportUnit(std::string name)
Retrieve a registered transport unit.
void AddPackage(std::string name, double fill_min=0, double fill_max=std::numeric_limits< double >::max(), std::string strategy="first")
Adds a package type to a simulation-wide accessible list.
virtual int time()
Returns the current simulation timestep.
void RegisterTimeListener(TimeListener *tl)
Registers an agent to receive tick/tock notifications every timestep.
double random_uniform_real(double low, double high)
Returns a random number from a uniform real distribution.
void RecordPackage(Package::Ptr)
Records package information.
int n_specs(std::string impl)
void SchedDecom(Agent *m, int time=-1)
Schedules the given Agent to be decommissioned at the specified timestep t.
void InitSim(SimInfo si)
Initializes the simulation time parameters.
void UnregisterTimeListener(TimeListener *tl)
Removes an agent from receiving tick/tock notifications.
Used to specify and send a collection of key-value pairs to the Recorder for recording.
a very simple interface for solving translated resource exchanges
The GreedySolver provides the implementation for a "greedy" solution to a resource exchange graph.
For failed retrieval/insertion of key-based data into/from data structures.
boost::shared_ptr< Package > Ptr
Collects and manages output data generation for the cyclus core and agents during a simulation.
Container for a static simulation-global parameters that both describe the simulation and affect its ...
double eps_rsrc
Epsilon for resources in the simulation.
int y0
start year for the simulation (e.g. 1973);
int m0
start month for the simulation: Jan = 1, ..., Dec = 12
bool explicit_inventory_compact
True if per-agent inventories should be explicitly queried/recorded every time step in a table (i....
boost::uuids::uuid parent_sim
id for the parent simulation if any
SimInfo()
constructs a SimInfo instance with default variables
double eps
Epsilon in the simulation.
std::string handle
user-defined label associated with a particular simulation
int duration
length of the simulation in timesteps (months)
uint64_t stride
Stride length.
uint64_t dt
Duration in seconds of a single time step in the simulation.
uint64_t seed
Seed for random number generator.
std::string decay
"manual" if use of the decay function is allowed, "never" otherwise
std::string parent_type
One of "init", "branch", "restart" indicating the relationship of this simulation to its parent simul...
int branch_time
timestep at which simulation branching occurs if any
bool explicit_inventory
True if per-agent inventories should be explicitly queried/recorded every time step in a table (i....
For failed object state expectations.
The TimeListener class is an inheritable class for any Agent that requires knowlege of ticks and tock...
A simple API for agents that wish to exchange resources in the simulation.
boost::shared_ptr< TransportUnit > Ptr
const uint64_t kDefaultSeed
const uint64_t kDefaultStride
const uint64_t kDefaultTimeStepDur
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
void PyDelAgent(int i)
Removes a single Python agent from the reference cache.