rapper API

A class and some utilities to wrap PySP. In particular to enable programmatic access to some of the functionality in runef and runph for ConcreteModels Author: David L. Woodruff, started February 2017

class pysp.util.rapper.StochSolver(fsfile, fsfct=None, tree_model=None, phopts=None)[source]

Bases: object

A class for solving stochastic versions of concrete models and abstract models. Inspired by the IDAES use case and by daps ability to create tree models. Author: David L. Woodruff, February 2017

Parameters
  • fsfile (str) – is a path to the file that contains the scenario callback for concrete or the reference model for abstract.

  • fsfct (str, or fct, or None) –

    str: callback function name in the file
    fct: callback function (fsfile is ignored)
    None: it is a AbstractModel

  • tree_model (concrete model, or networkx tree, or path) – gives the tree as a concrete model (which could be a fct) or a valid networkx scenario tree or path to AMPL data file.

  • phopts – dictionary of ph options; needed during construction if there is bundling.

scenario_tree

scenario tree object (that includes data)

make_ef(verbose=False, generate_weighted_cvar=False, cvar_weight=None, risk_alpha=None, cc_indicator_var_name=None, cc_alpha=0.0)[source]

Make an ef object (used by solve_ef); all Args are optional.

Parameters
  • verbose (boolean) – indicates verbosity to PySP for construction

  • generate_weighted_cvar (boolean) – indicates we want weighted CVar

  • cvar_weight (float) – weight for the cvar term

  • risk_alpha (float) – alpha value for cvar

  • cc_indicator_var_name (string) – name of the Var used for chance constraint

  • cc_alpha (float) – alpha for chance constraint

Returns

the ef object

Return type

ef_instance

root_E_obj()[source]

post solve Expected cost of the solution in the scenario tree (xbar)

Returns

the expected costs of the solution in the tree (xbar)

Return type

float

root_Var_solution()[source]

Generator to loop over x-bar

Yields

name, value pair for root node solution values

solve_ef(subsolver, sopts=None, tee=False, need_gap=False, verbose=False, generate_weighted_cvar=False, cvar_weight=None, risk_alpha=None, cc_indicator_var_name=None, cc_alpha=0.0)[source]

Solve the stochastic program directly using the extensive form. All Args other than subsolver are optional.

Parameters
  • subsolver (str) – the solver to call (e.g., ‘ipopt’)

  • sopts (dict) – solver options

  • tee (bool) – indicates dynamic solver output to terminal.

  • need_gap (bool) – indicates the need for the optimality gap

  • verbose (boolean) – indicates verbosity to PySP for construction

  • generate_weighted_cvar (boolean) – indicates we want weighted CVar

  • cvar_weight (float) – weight for the cvar term

  • risk_alpha (float) – alpha value for cvar

  • cc_indicator_var_name (string) – name of the Var used for chance constraint

  • cc_alpha (float) – alpha for chance constraint

Returns: (Pyomo solver result, float)

solve_result is the solver return value.

absgap is the absolute optimality gap (might not be valid); only if requested

Note

Also update the scenario tree, populated with the solution. Also attach the full ef instance to the object. So you might want obj = pyo.value(stsolver.ef_instance.MASTER) This needs more work to deal with solver failure (dlw, March, 2018)

solve_ph(subsolver, default_rho, phopts=None, sopts=None)[source]

Solve the stochastic program given by this.scenario_tree using ph

Parameters
  • subsolver (str) – the solver to call (e.g., ‘ipopt’)

  • default_rho (float) – the rho value to use by default

  • phopts – dictionary of ph options (optional)

  • sopts – dictionary of subsolver options (optional)

Returns: the ph object

Note

Updates the scenario tree, populated with the xbar values; however, you probably want to do obj, xhat = ph.compute_and_report_inner_bound_using_xhat() where ph is the return value.

pysp.util.rapper.xhat_from_ph(ph)[source]

a service fuction to wrap a call to get xhat

Parameters

ph – a post-solve ph object

Returns: (float, object)

float: the expected cost of the xhat solution for the scenarios

xhat: an object with the solution tree

pysp.util.rapper.xhat_walker(xhat)[source]

A service generator to walk over a given xhat

Parameters

xhat (dict) – an xhat solution (probably from xhat_from_ph)

Yields

(nodename, varname, varvalue)