Skip to content

Commit

Permalink
Merge pull request #171 from bknueven/issue170
Browse files Browse the repository at this point in the history
Add RuntimeError for stale non-anticipative variables on feasible scenarios
  • Loading branch information
bknueven authored Nov 3, 2021
2 parents a182427 + 57bddfe commit 5e5d6dc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mpisppy/spbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,10 @@ def is_zero_prob( self, scenario_model, var ):
return False
_mpisppy_data = scenario_model._mpisppy_data
ndn, i = _mpisppy_data.varid_to_nonant_index[id(var)]
return float(_mpisppy_data.prob_coeff[ndn][i]) == 0.
if isinstance(_mpisppy_data.prob_coeff[ndn], np.ndarray):
return float(_mpisppy_data.prob_coeff[ndn][i]) == 0.
else:
return False

def _check_variable_probabilities_sum(self, verbose):

Expand Down
22 changes: 22 additions & 0 deletions mpisppy/spopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,28 @@ def _vb(msg):
np.mean(all_pyomo_solve_times),
np.max(all_pyomo_solve_times)))

# Look at the feasible scenarios. If we have any stale non-anticipative
# variables, complain. Otherwise the user will hit this issue later when
# we attempt to access the variables `value` attribute (see Issue #170).
for sn, s in self.local_scenarios.items():
if s._mpisppy_data.scenario_feasible:
for v in s._mpisppy_data.nonant_indices.values():
if (not v.fixed) and v.stale:
if self.is_zero_prob(s, v) and v._value is None:
raise RuntimeError(
f"Non-anticipative zero-probability variable {v.name} "
f"on scenario {sn} reported as stale and has no value. "
"Zero-probability variables must have a value (e.g., fixed).")
else:
raise RuntimeError(
f"Non-anticipative variable {v.name} on scenario {sn} "
"reported as stale. This usually means this variable "
"did not appear in any (active) constraints, and hence "
"was not communicated to the subproblem solver. Please "
"ensure all non-anticipative variables appear in some "
"constraint.")



def Eobjective(self, verbose=False):
""" Compute the expected objective function across all scenarios.
Expand Down

0 comments on commit 5e5d6dc

Please sign in to comment.