core package
Submodules
core.agent module
- class core.agent.AbstractAgent(name='', objectives=None, inequations=None, equations=None, functions=None, params=None, dim_dict=None)[source]
Bases:
AgentValidatorCore methods for Agent models.
Methods:
Constructors:
__init__: Init Agent from its parts: see method args.
read_from_tex: Parse Agent model from .tex file written in YAML (json-like) format. See examples at /models/inputs/.
Private:
__generate_duals: Generate dual variables and functions due to Lagrange principum.
Public:
Additional:
Support system
Methods that helps system to understand which variables are phase, which are controls etc.
time : extract time variable in agent model
time_horizon : extract time horizon boundaries from integral part of objective functions
transitions : extract first-order differential equations from all boundaries
phases : extract phase variables from all functions
external : extract exogenous constants from all functions
controls : extract control variables from all functions
variables : extract all model variable (not-functions)
expr : extract all expressions (with objectives)
boundaries : all equations and inequations (no differential equations)
constant_ineqs : inequations with no terminal values
diff_degree : Divide all expressions over differential equation degrees
validate : provide Agent model validation from subclass
Misc
Basic class methods to provide comfort.
args : Args to re-init
kwargs : Kwargs to re-init
process : Pre-process model to be used in core methods
compress : Evaluate all Pontragin Principle conditions
dump : Process model to PDF file with optimal conditions (Pontryagin Principle) [compress + dump]
Core
Methods that conduct Maximum Principle Conditions due to Lagrange principum.
Lagrangian : integral part of L
lagrangian : termination part of L
euler_equations : Euler-Lagrange equations
transversality_conditions : Transversality conditions
control_optimality : Control optimality conditions
KKT: Dual-feasibility and Complementary Slackness conditions
- property Hamiltonian_Gamkrelidze
not tested yet
- property Lagrangian
Intergal part of L :return: Expr
- property args
Args, to re-init :return:
- property boundaries
Expressions with these types:
Differential degree == 0
Constant inequalities
- Returns
List[Expr]
- compress(to_tex=False, headers=True)[source]
- Parameters
to_tex – bool. If True – .tex file will be generated
headers – If True – .tex file will contain headers
- Returns
Union[None, RenderedJinjaTemplate]
- property constant_ineqs
Constant inequalities: those which contains only functions in terminant (constant) values. :return:
- control_optimality()[source]
Control optimality conditions, using smoothness of L function wrt control variables :return: List[Eq]
- property controls
Extract all controls: controls = functions - external - phases :return: List[Symbol]
- diff_degree(deg=None)[source]
Returns all expr with Derivative degree == deg. :param deg: int. Derivative degree of returning equations :return: Dict[Expr->deg] if None else Dict[Expr->`deg`]
- dump(destination=None, is_absolute=False)[source]
Create .tex and PDF files with Agent optimal conditions. :param destination: filepath or fd. :return: PDF, .tex files saved in destination
- emitent = False
- property expr
All expressions in model :return: List[Expr]
- property external
Extract exogenous functions, they must be substricted :return: List[Symbol]
- property kwargs
Kwargs, to re-init :return:
- property lagrangian
Terminant part of L :return: Expr
- property phases
Extract phase variables from model: functions which appears as arguments in Differential operators in transitions :return: Union[List[Symbol], AnyPropertyNotFound]
- process(skip_validation=False)[source]
Process model to be used in core methods. :param skip_validation: bool – If True validation process will be skipped :return: Union[None, AnyError]
- classmethod read_from_tex(f)[source]
Contructor from .tex files. See Knowledge base to get guidelines. :param f: filename or fd :return: Agent
- property time
- Extract time variable from:
Integral in objectives
Differential variable in boundaries
- Returns
Union[Symbol, TimeVariableNotFound]
- property time_horizon
If time is extracted from objective integrals then collect integral boundaries. :return: Union[Tuple, AnyPropertyNotFound]
- property transitions
Extract linear differential equations from model. :return: List[Eq]
- transversality_conditions()[source]
Transversality conditions: L_{x’}(t_i) = (-1)^i l_{x(t_i)} :return: List[Eq]
- property variables
All symbols in model :return: List[Symbol]
- class core.agent.AgentValidator[source]
Bases:
objectValidation class for Agent class. Provides Agent validation and correction to be processed by ECOMOD Core.
Methods:
Private:
- __dimension_check
Uses KV-storage gained from input Agent model with variables -> dimensions to proceed Dimension check in Agent equations and inequations (expressions).
- __variable_completeness
Checks if there are no extra initialized Agent variables in model, which are unused in model Expressions and if there are uninitialized variables in model.
Open:
- validate
Provides full blackbox checks + emitent check
Class variables:
emitent = None: bool Shows that this Validation process is for Simple Agents – with objective functional.
- emitent = None
- validate(objectives, inequations, equations, functions, params, dim_dict)[source]
See __variable_completeness and __dimension_check docstrings. :param objectives: Agent objectives functions :param inequations: Agent inequation boundaries :param equations: Agent equation boundaries :param functions: Agent phase variables, controls and exogenous constants (but those are functions of time) :param params: Agent not-functional variables :param dim_dict: Union[List, Dict] – KV-storage: variable -> dimension :return: Union[AnyError, NonSympyfiableError, ExtraVariableError, DimensionCheckingFailed]
- class core.agent.LinkedAgent(*args)[source]
Bases:
AbstractAgent- Methods:
- Constructors:
- __init__
Basic contructor
- from_abstract
Init LinkedAgent from Agent instance
- Private:
- __merge_prepare
Use Agent model output to gain tagged Agent model output.
- Public:
add_flow
delete_flow
print_flows
- emitent = False
- static from_abstract(a: AbstractAgent)[source]
- core.agent.create_empty_agents(names, cls=<class 'core.agent.AbstractAgent'>)[source]
Additional function to provide test cases in scenarios/debug_scenarios.py :param names: Agent tags separated by space ‘ ‘. :param cls: Default=AbstractAgent. Class for returned agents. :return: List[Union[AbstractAgent, LinkedAgent]]
core.datamodel module
core.deserialiser module
- core.deserialiser.parse_yaml(stream: str)[source]
Simple YAML decoder, to distinguish errors. :param stream: string value after reducing tex-header :return: dict: decoded yaml
core.ecomod_utils module
- core.ecomod_utils.KKT_mask(dual: dict)[source]
Returns Dual Feasibility and Complementary Slackness for dual dict :param dual: Dict[Union[datamodel.Parameter, datamodel.Phase] –> Expr] :return: List[Expr] = [K*V]
- core.ecomod_utils.add_subscript(symb, tag)[source]
Add subscript tag to symbol :param symb: Union[datamodel.Parameter, datamodel.Phase] - untagged :param tag: str :return: Union[datamodel.Parameter, datamodel.Phase] – tagged
- core.ecomod_utils.deriv_degree(bc)[source]
Returns maximum degree of differential operator inside bc expression. :param bc: Expr :return: int
- core.ecomod_utils.eq2func(eq)[source]
Return Expr:= lhs - rhs from Eq :param eq: Eq to be transformed :return: Expr
- core.ecomod_utils.euler_mask(L, x, t)[source]
Provide Euler-Lagrange equations for passed Lagrangian and variables [phase, time] :param L: Expr – Lagrangian :param x: datamodel.Phase – Phase variables :param t: datamodel.Parameter – Time variable :return: List[Expr]
- core.ecomod_utils.generate_symbols(tag, count, cls)[source]
Generate symbols function. Used for dual variables and parameters generation in Agent.process :param tag: str – Alias for generated symbols :param count: int – count of symbols :param cls: Class[Union[datamodel.Parameter, datamodel.Phase]] :return: Iter[Symbol]
- core.ecomod_utils.is_spec_function(func)[source]
Bool version of spec_funcs. Return true if func is listed in spec_funcs, otherwise else. :param func: function to be checked :return: bool
- core.ecomod_utils.is_substricted(symb, tag=None)[source]
Check if symbol is substricted with tag. :param symb: Union[datamodel.Parameter, datamodel.Phase] :param tag: Union[string, None] :return: bool
- core.ecomod_utils.latexify(exprs: list, to_str=False)[source]
Provide latex codesnippets to expressions. Can be returned as joined string or as List[str] :param exprs: List[Expr] :param to_str: bool :return: Union[List[Expr], str]
- core.ecomod_utils.pi_theorem(vars, eq)[source]
Deprecated method for dimension checking in equations. :param vars: :param eq: :return:
- core.ecomod_utils.remove_subscript(symb)[source]
Firstly check if symbol is substricted, then if true remove !ANY! substricted. :param symb: Union[datamodel.Parameter, datamodel.Phase] :return: Union[datamodel.Parameter, datamodel.Phase] – unsubscripted.
- core.ecomod_utils.span(coefs, variables)[source]
Return linear span of KV-storage. ret = sum_0^N[L_1[i] * L_2[i]] :param coefs: List[Expr] :param variables: List[Expr] :return: Expr
- core.ecomod_utils.span_dict(d: dict)[source]
Return linear span of KV-storage. ret = sum_0^N[K[i] * V[i]] :param d: Dict[Expr -> Expr] :return: Expr
- core.ecomod_utils.spec_funcs()[source]
Set of special analytical functions in sympy. Used to dimension check of its args. :return: set
- core.ecomod_utils.transversality_mask(L, x, t, l, t0, t1)[source]
Transveraslity condition for agent optimal control problem :param L: Expr – Lagrangian :param x: datamodel.Phase – phase variable :param t: datamodel.Parameter – time variable :param l: Expr – termination lagrangian :param t0: datamodel.Parameter – time horizon[0] :param t1: datamodel.Parameter – time horizon[1] :return: List[Eq]
core.logger module
core.market module
core.model module
- class core.model.AgentMerger[source]
Bases:
object- merge(lagents: List[AbstractAgent])[source]
- class core.model.Model(name, balances=None, lagents: Optional[List[LinkedAgent]] = None)[source]
Bases:
ModelValidatorMerged model with several agents and markets or agents and balances or agents and flows
- add_agent(lagent: LinkedAgent)[source]
- class core.model.ModelValidator[source]
Bases:
MarketValidator,AgentValidator,AgentMerger- validate(markets, lagents)[source]
See __variable_completeness and __dimension_check docstrings. :param objectives: Agent objectives functions :param inequations: Agent inequation boundaries :param equations: Agent equation boundaries :param functions: Agent phase variables, controls and exogenous constants (but those are functions of time) :param params: Agent not-functional variables :param dim_dict: Union[List, Dict] – KV-storage: variable -> dimension :return: Union[AnyError, NonSympyfiableError, ExtraVariableError, DimensionCheckingFailed]