JP:
Most of your example is constructing an expression. The rest is
ugliness that I think we can work around! We want the following to
work:
expr = 3.0 * instance.x + 4.0 * instance.y
model.MASTER = Objective(expr=expr)
Right? I think we can do this by telling model instances that they
are concrete! That's a simple change.
I can't log in right now, but I'll try this out shortly...
--Bill
On Sep 28, 2:41 pm, "Watson, Jean-paul" <
jwat...@sandia.gov> wrote:
> Hi Dirk,
>
> Here's an example of what I do in PySP to create an objective on-the-fly, with some culling to keep the snippet size manageable:
>
> opt_expression = node_expected_cost_variable
>
> if generate_weighted_cvar is True:
> cvar_cost_variable_name = "CVAR_COST_" + tree_node._name
> cvar_cost_variable = getattr(binding_instance, cvar_cost_variable_name)
> if cvar_weight == 0.0:
> # if the cvar weight is 0, then we're only doing cvar - no mean.
> opt_expression = cvar_cost_variable
> else:
> opt_expression += cvar_weight * cvar_cost_variable
>
> new_objective = Objective(name="MASTER", sense=opt_sense)
> new_objective._data[None].expr = opt_expression
> setattr(binding_instance, "MASTER", new_objective)
>
> For now, let's assume you are only worried about a single-objective problem - in which case the "None" index in the expression set (second-to-last line) refers to the one-and-only expression.
>
> This should do it - at least it works in the examples I have deployed.
>
> I will readily admit, after reading the code snippet, that the line "new_objective._data[None].expr = opt_expression" is one ugly and non-intuitive syntax. I just put in a reminder to myself to mirror the functionality of "add(index, expression)" that we have in the Constraint class.
>
> I hope this helps - if things still don't work, let me/us know.
>
> jpw