# ___________________________________________________________________________ # # Pyomo: Python Optimization Modeling Objects # Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC # Under the terms of Contract DE-NA0003525 with National Technology and # Engineering Solutions of Sandia, LLC, the U.S. Government retains certain # rights in this software. # This software is distributed under the 3-clause BSD License. # ___________________________________________________________________________ from pyomo.core.base import Block, Var, Reference from pyomo.core.base.block import SubclassOf from pyomo.core.base.indexed_component_slice import IndexedComponent_slice def generate_time_only_slices(obj, time): o_sets = obj.index_set().subsets() # Given a potentially complex set, determine the index of the TIME # set, as well as all other "fixed" indices. We will even support a # single Set with dimen==None (using ellipsis in the slice). ellipsis_idx = None time_idx = None regular_idx = [] idx = 0 for s in o_sets: if s is time: time_idx = idx idx += 1 elif s.dimen is not None: for sub_idx in range(s.dimen): regular_idx.append(idx+sub_idx) idx += s.dimen elif ellipsis_idx is None: ellipsis_idx = idx idx += 1 else: raise RuntimeError( "We can only handle a single Set with dimen=None") # To support Sets with dimen==None (using ellipsis), we need to have # all fixed/time indices be positive if they appear before the # ellipsis and negative (counting from the end of the list) if they # are after the ellipsis. if ellipsis_idx: if time_idx > ellipsis_idx: time_idx = time_idx - idx regular_idx = [ i - idx if i > ellipsis_idx else i for i in fixed_idx ] # We now form a temporary slice that slices over all the regular # indices for a fixed value of the time index. tmp_sliced = {i: slice(None) for i in regular_idx} tmp_fixed = {time_idx: time.first()} tmp_ellipsis = ellipsis_idx _slice = IndexedComponent_slice( obj, tmp_fixed, tmp_sliced, tmp_ellipsis ) # For each combination of regular indices, we can generate a single # slice over the time index time_sliced = [time_idx] for key in _slice.wildcard_keys(): if type(key) is not tuple: key = (key,) time_fixed = dict( (i, val) if i