engforge.solver.SolverMixin

class SolverMixin(name='')[source]

Bases: SolveableMixin

A base class inherited by solveable items providing the ability to solve itself

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

Methods

add_fields

Overwrite this to modify logging fields

change_all_log_lvl

check_ref_slot_type

recursively checks class slots for the key, and returns the slot type

collect_all_attributes

collects all the attributes for a system

collect_comp_refs

collects all the references for the system grouped by component

collect_dynamic_refs

collects the dynamics of the systems 1.

collect_inst_attributes

collects all the attributes for a system

collect_post_update_refs

checks all methods and creates ref's to execute them later

collect_solver_refs

collects all the references for the system grouped by function and prepended with the system key

collect_update_refs

checks all methods and creates ref's to execute them later

comp_references

A cached set of recursive references to any slot component #FIXME: by instance recache on iterative component change or other signals

critical

A routine to communicate to the root of the server network that there is an issue

debug

Writes at a low level to the log file.

difference

a context manager that will allow you to dynamically change any information, then will change it back in a fail safe way.

error

Writes to log as a error

eval

Evaluates the system with pre/post execute methodology :type kw: :param kw: kwargs come from sys_kw input in run ect.

execute

Solves the system's system of constraints and integrates transients if any exist

extract_message

filter

This acts as the interface for logging.Filter Don't overwrite this, use add_fields instead.

get_system_input_refs

Get the references to system input based on the specified criteria.

hash

hash by parm or by input_kw, only input can be hashed by lookup as system properties can create a recursive loop and should be deterministic from input

hash_numeric_with

hash_with

Generates a hash for the object's dictionary representation, updated with additional keyword arguments.

info

Writes to log but with info category, these are important typically and inform about progress of process in general

input_attrs

Lists all input attributes for class

input_fields

no attr base types, no tuples, no lists, no dicts

installSTDLogger

We only want std logging to start

internal_components

get all the internal components

internal_references

get references to all internal attributes and values, only saving when complete cache info is requested (vs numeric only)

internal_systems

get all the internal components

internal_tabulations

get all the internal tabulations

locate

locate_ref

Pass a string of a relative var or property on this system or pass a callable to get a reference to a function.

message_with_identiy

converts to color and string via the termcolor library :type message: str :param message: a string convertable entity :type color: :param color: a color in [grey,red,green,yellow,blue,magenta,cyan,white]

msg

Writes to log.

numeric_fields

no tuples,lists, dicts, strings, or attr base types

parse_run_kwargs

ensures correct input for simulation.

parse_simulation_input

parses the simulation input

plot_attributes

Lists all plot attributes for class

post_run_callback

user callback for when run is complete

post_update

Kwargs comes from eval_kw in solver

pre_run_callback

user callback for when run is beginning

resetLog

reset log

resetSystemLogs

resets log on all internal instance LoggingMixins

run

the steady state run the solver for the system.

run_internal_systems

runs internal systems with potentially scoped kwargs

setattrs

sets attributes from a dictionary

signals_attributes

Lists all signals attributes for class

slack_notification

slot_refs

returns all slot references in this configuration

slots_attributes

Lists all slots attributes for class

solver

runs the system solver using the current system state and modifying it.

solver_vars

applies the default combo filter, and your keyword arguments to the collect_solver_refs to test the ref / vars creations

solvers_attributes

Lists all signals attributes for class

system_references

gather a list of references to attributes and

table_fields

the table attributes corresponding to

trace_attributes

Lists all trace attributes for class

transients_attributes

Lists all signals attributes for class

update

Kwargs comes from eval_kw in solver

warning

Writes to log as a warning

Attributes

as_dict

returns values as they are in the class instance

attrs_fields

identity

input_as_dict

returns values as they are in the class instance, but converts classes inputs to their input_as_dict

input_hash

log_fmt

log_level

log_on

logger

numeric_as_dict

recursively gets internal components numeric_as_dict as well as its own numeric values

numeric_hash

slack_webhook_url

solved

table_hash

table_row_dict

returns values as they would be put in a table row from this instance ignoring any sub components

unique_hash

parent

add_fields(record)

Overwrite this to modify logging fields

property as_dict

returns values as they are in the class instance

classmethod check_ref_slot_type(sys_key)

recursively checks class slots for the key, and returns the slot type

Return type:

list

Parameters:

sys_key (str)

classmethod collect_all_attributes()

collects all the attributes for a system

collect_comp_refs(conf=None, **kw)

collects all the references for the system grouped by component

Parameters:

conf (Configuration | None)

collect_dynamic_refs(conf=None, **kw)

collects the dynamics of the systems 1. Time.integrate 2. Dynamic Instances

Return type:

dict

Parameters:

conf (Configuration | None)

collect_inst_attributes(**kw)

collects all the attributes for a system

collect_post_update_refs(eval_kw=None, ignore=None)

checks all methods and creates ref’s to execute them later

collect_solver_refs(conf=None, check_atr_f=None, check_kw=None, check_dynamics=True, **kw)

collects all the references for the system grouped by function and prepended with the system key

Parameters:

conf (Configuration | None)

collect_update_refs(eval_kw=None, ignore=None)

checks all methods and creates ref’s to execute them later

comp_references(ignore_none_comp=True, **kw)

A cached set of recursive references to any slot component #FIXME: by instance recache on iterative component change or other signals

critical(*args)

A routine to communicate to the root of the server network that there is an issue

debug(*args)

Writes at a low level to the log file… usually this should be detailed messages about what exactly is going on

difference(**kwargs)

a context manager that will allow you to dynamically change any information, then will change it back in a fail safe way.

with self.difference(name=’new_name’, value = new_value) as new_config:

#do stuff with config, ok to fail

you may not access any “private” variable that starts with an _ as in _whatever

difference is useful for saving slight differences in configuration in conjunction with solve you might create wrappers for eval, or implement a strategy pattern.

only attributes may be changed.

#TODO: allow recursive operation with sub comps or systems. #TODO: make a full system copy so the system can be reverted later

error(error, msg='')

Writes to log as a error

eval(Xo=None, eval_kw=None, sys_kw=None, cb=None, **kw)[source]

Evaluates the system with pre/post execute methodology :type kw: :param kw: kwargs come from sys_kw input in run ect. :type cb: :param cb: an optional callback taking the system as an argument of the form (self,eval_kw,sys_kw,**kw)

Parameters:
  • eval_kw (dict | None)

  • sys_kw (dict | None)

execute(**kw)[source]

Solves the system’s system of constraints and integrates transients if any exist

Override this function for custom solving functions, and call solver to use default solver functionality.

Returns:

the result of this function is returned from solver()

filter(record)

This acts as the interface for logging.Filter Don’t overwrite this, use add_fields instead.

get_system_input_refs(strings=False, numeric=True, misc=False, all=False, boolean=False, **kw)

Get the references to system input based on the specified criteria.

Parameters:
  • strings – Include system properties of string type.

  • numeric – Include system properties of numeric type (float, int).

  • misc – Include system properties of miscellaneous type.

  • all – Include all system properties regardless of type.

  • boolean – Include system properties of boolean type.

  • kw – Additional keyword arguments passed to recursive config loop

Returns:

A dictionary of system property references.

Return type:

dict

hash(*args, **input_kw)

hash by parm or by input_kw, only input can be hashed by lookup as system properties can create a recursive loop and should be deterministic from input

hash_with(**input_kw)

Generates a hash for the object’s dictionary representation, updated with additional keyword arguments. :type **input_kw: :param **input_kw: Arbitrary keyword arguments to update the object’s dictionary representation.

Returns:

The hash value of the updated dictionary.

Raises:

Any exceptions raised by deepdiff.DeepHash if hashing fails.

info(*args)

Writes to log but with info category, these are important typically and inform about progress of process in general

property input_as_dict

returns values as they are in the class instance, but converts classes inputs to their input_as_dict

classmethod input_attrs()

Lists all input attributes for class

classmethod input_fields(add_ign_types=None)

no attr base types, no tuples, no lists, no dicts

Parameters:

add_ign_types (list | None)

installSTDLogger()

We only want std logging to start

internal_components(recache=False)

get all the internal components

Return type:

dict

internal_references(recache=False, numeric_only=False)

get references to all internal attributes and values, only saving when complete cache info is requested (vs numeric only)

Return type:

dict

internal_systems(recache=False)

get all the internal components

Return type:

dict

internal_tabulations(recache=False)

get all the internal tabulations

Return type:

dict

classmethod locate(key, fail=True)
Return type:

type

Returns:

the class or attribute by key if its in this system class or a subcomponent. If nothing is found raise an error

locate_ref(key, fail=True, **kw)

Pass a string of a relative var or property on this system or pass a callable to get a reference to a function. If the key has a . in it the comp the lowest level component will be returned, unless a callable is passed in which case this component will be used or the comp passed in the kw will be used. :type key: :param key: the key to locate, or a callable to be used as a reference :param comp: the component to use if a callable is passed :returns: the instance assigned to this system. If the key has a . in it the comp the lowest level component will be returned

message_with_identiy(message, color=None)

converts to color and string via the termcolor library :type message: str :param message: a string convertable entity :type color: :param color: a color in [grey,red,green,yellow,blue,magenta,cyan,white]

Parameters:

message (str)

msg(*args, lvl=5)

Writes to log… this should be for raw data or something… least priorty

property numeric_as_dict

recursively gets internal components numeric_as_dict as well as its own numeric values

classmethod numeric_fields()

no tuples,lists, dicts, strings, or attr base types

parse_run_kwargs(**kwargs)

ensures correct input for simulation. :returns: first set of input for initalization, and all input dictionaries as tuple.

parse_simulation_input(**kwargs)

parses the simulation input

Parameters:
  • dt – timestep in s, required for transients

  • endtime – when to end the simulation

classmethod plot_attributes()

Lists all plot attributes for class

Return type:

Dict[str, Attribute]

post_run_callback(**kwargs)[source]

user callback for when run is complete

post_update(parent, *args, **kwargs)

Kwargs comes from eval_kw in solver

pre_run_callback(**kwargs)[source]

user callback for when run is beginning

resetLog()

reset log

resetSystemLogs(reseted=None)

resets log on all internal instance LoggingMixins

run(**kwargs)[source]

the steady state run the solver for the system. It will run the system with the input vars and return the system with the results. Dynamics systems will be run so they are in a steady state nearest their initial position.

run_internal_systems(sys_kw=None)[source]

runs internal systems with potentially scoped kwargs

setattrs(dict)

sets attributes from a dictionary

classmethod signals_attributes()

Lists all signals attributes for class

Return type:

Dict[str, Attribute]

classmethod slot_refs(recache=False)

returns all slot references in this configuration

classmethod slots_attributes(attr_type=False)

Lists all slots attributes for class

Return type:

Dict[str, Attribute]

solver(enter_refresh=True, save_on_exit=True, **kw)[source]

runs the system solver using the current system state and modifying it. This is the default solver for the system, and it is recommended to add additional options or methods via the execute method.

Parameters:
  • obj – the objective function to minimize, by default will minimize the sum of the squares of the residuals. Objective function should be a function(system,Xs,Xt) where Xs is the system state and Xt is the system transient state. The objective function will be argmin(X)|(1+custom_objective)*residual_RSS when add_obj is True in kw otherwise argmin(X)|custom_objective with constraints on the system as balances instead of first objective being included.

  • cons – the constraints to be used in the solver, by default will use the system’s constraints will be enabled when True. If a dictionary is passed the solver will use the dictionary as the constraints in addition to system constraints. These can be individually disabled by key=None in the dictionary.

  • X0 – the initial guess for the solver, by default will use the current system state. If a dictionary is passed the solver will use the dictionary as the initial guess in addition to the system state.

  • dXdt – can be 0 to indicate steady-state, or None to not run the transient constraints. Otherwise a partial dictionary of vars for the dynamics rates can be given, those not given will be assumed steady state or 0.

  • kw – additional options for the solver, such as the solver_option, or the solver method options. Described below

  • combos – a csv str or list of combos to include, including wildcards. the default means all combos will be run unless ign_combos or only combos alters behavior. The initial selection of combos is made by matching any case with the full name of the combo, or a parial name with a wildcard(s) in the combo name Ignore and only combos will further filter the selection. Wildcards / queries per fnmatch

  • ign_combos – a list of combo vars to ignore.

  • only_combos – a list of combo vars to include exclusively.

  • add_var – a csv str or variables to include, including wildcards. the default means all combos will be run unless ign_combos or only combos alters behavior. The initial selection of combos is made by matching any case with the full name of the combo, or a parial name with a wildcard(s) in the combo name Ignore and only combos will further filter the selection. Wildcards / queries per fnmatch

  • ign_var – a list of combo vars to ignore.

  • only_var – a list of combo vars to include exclusively.

  • add_obj – a flag to add the objective to the system constraints, by default will add the objective to the system constraints. If False the objective will be the only constraint.

  • only_active – default True, will only look at active variables objectives and constraints

  • activate – default None, a list of solver vars to activate

  • deactivate – default None, a list of solver vars to deactivate (if not activated above)

solver_vars(check_dynamics=True, addable=None, **kwargs)[source]

applies the default combo filter, and your keyword arguments to the collect_solver_refs to test the ref / vars creations

parses add_vars in kwargs to append to the collected solver vars :param add_vars: can be a str, a list or a dictionary of variables: solver_instance kwargs. If a str or list the variable will be added with positive only constraints. If a dictionary is chosen, it can have keys as parameters, and itself have a subdictionary with keys: min / max, where each respective value is placed in the constraints list, which may be a callable(sys,prob) or numeric. If nothing is specified the default is min=0,max=None, ie positive only.

classmethod solvers_attributes()

Lists all signals attributes for class

Return type:

Dict[str, Attribute]

system_references(recache=False, numeric_only=False, **kw)

gather a list of references to attributes and

classmethod table_fields()

the table attributes corresponding to

property table_row_dict

returns values as they would be put in a table row from this instance ignoring any sub components

classmethod trace_attributes()

Lists all trace attributes for class

Return type:

Dict[str, Attribute]

classmethod transients_attributes()

Lists all signals attributes for class

Return type:

Dict[str, Attribute]

update(parent, *args, **kwargs)

Kwargs comes from eval_kw in solver

warning(*args)

Writes to log as a warning