B @`*@slddlmZddlmZddlmZmZddlmZddZ ddZ dd d Z d d Z ddZ dddZd S))Counter) ComponentSet) ConstraintBlock) SetProductcCs d||fS)NzWARNING: %s has no index %s)nameindexrr7/tmp/pip-unpacked-wheel-bi3529v6/pyomo/dae/set_utils.py index_warningsr cs|s dSx"|D]}t|trd}t|qW|dd}|rX|}tdt||j |d}t |t fdd|DS)a Function for determining whether a pyomo component is indexed by a set or group of sets. Args: comp : Some Pyomo component, possibly indexed sets : Pyomo Sets to check indexing by expand_all_set_operators : Whether or not to expand all set operators in the subsets method Returns: A bool that is True if comp is directly indexed by every set in sets. Fz?Checking for explicit indexing by a SetProduct is not supportedexpand_all_set_operatorsz"Unrecognized keyword arguments: %s)r csg|] }|kqSrr).0_) subset_setrr :sz,is_explicitly_indexed_by..) Z is_indexed isinstancer TypeErrorpopkeys ValueErrorstr index_setsubsetsrall)compsetskwargssmsgr rZprojected_subsetsr)rr is_explicitly_indexed_bys    rNcCsP|}xB|dk rJ||krdS|}||kr2dSt||r@dS|}q WdS)a Function for determining whether a component is contained in a block that is indexed by a particular set. Args: comp : Component whose parent blocks are checked s : Set for which indices are checked stop_at : Block at which to stop searching if reached, regardless of whether or not it is indexed by s Returns: Bool that is true if comp is contained in a block indexed by s NFT)Z parent_blockZparent_componentr)rrZstop_atparentrrr is_in_block_indexed_by=s   r!cst|}ytdd|D}Wn tk r>d}t|YnXi}t|f|sv|jdtdd|D}t||}t|t r0t | }t dd|D}x(|D] } |t | dkrd}t|qWig} x`t|D]F\} } d } x*t|D]\}}| |kr|| <d } PqW| s| | qWn d d ig} ||krhd g|d <fdd|d<|St| dkr| d }n.t| dkr| d j| dd }ntdfdd}||d <||d<|S)a@ Function for getting indices of a component over a product of its indexing sets other than those specified. Indices for the specified sets can be used to construct indices of the proper dimension for the original component via the index_getter function. Args: comp : Component whose indexing sets are to be manipulated sets : Sets to omit from the set_except product Returns: A dictionary. Maps 'set_except' to a Pyomo Set or SetProduct of comp's index set, excluding those in sets. Maps 'index_getter' to a function that returns an index of the proper dimension for comp, given an element of set_except and a value for each set excluded. These values must be provided in the same order their Sets were provided in the sets argument. cSsg|] }|jqSr)Zdimen)r rrrr rysz(get_index_set_except..zfget_index_set_except does not support sets with dimen == None, including those with inconsistent dimenz# is not indexed by at least one of cSsg|] }|jqSr)r)r rrrr rscSsg|] }t|qSr)id)r rrrr rsz+Cannot omit sets that appear multiple timesFTrN set_exceptcs,tdkrdStfddDS)Nr#rcsg|]}|qSrr)r i)locationnewvalsrr rsz:get_index_set_except....)lentuple)incomplete_indexr')r&)r'r sz&get_index_set_except.. index_getterzDid not expect this to happencst|f|S)N)_complete_index)r*r')r&rr r+s)rsumrrrrrrrrlistrrr" enumerateappendZdimr(Zcross)rrZs_setZ total_s_dimrinforZprojection_setscounterrZother_ind_setsZind_locZind_setZ found_setZs_locr$r,r)r&r get_index_set_exceptdsZ       r5cGst|tk r|f}t|}t|t|kr6tdxNt|D]>}|||}t|tk rf|f}|d||||d}qDW|S)a^ Function for inserting new values into a partial index. Used by get_index_set_except function to construct the index_getter function for completing indices of a particular component with particular sets excluded. Args: loc : Dictionary mapping location in the new index to location in newvals index : Partial index newvals : New values to insert into index. Can be scalars or tuples (for higher-dimension sets) Returns: An index (tuple) with values from newvals inserted in locations specified by loc z(Wrong number of values to complete indexrN)typer)sortedrr(r)locr r'rr%Znewvalrrr r.s     r.TFc Cs@t|tk r|g}x.|D]&}||krt|d|j}t|qWdd|D}t}x|jttgddD]} t | |kr|qj| t | t | |rjt | |sjt | |} | d} | d} x~| D]v} xp|D]h}| | |}y"| |||| |Wqtk r.|s tt| j||s(wYqXqWqWqjW|S)aB Finds any block or constraint in block b, indexed explicitly (and not implicitly) by cset, and deactivates it at points specified. Implicitly indexed components are excluded because one of their parent blocks will be deactivated, so deactivating them too would be redundant. Args: b : Block to search cset : ContinuousSet of interest pts : Value or list of values, in ContinuousSet, to deactivate at Returns: A dictionary mapping points in pts to lists of component data that have been deactivated there z is not in ContinuousSet cSsi|] }g|qSrr)r ptrrr sz'deactivate_model_at..T)activer$r,)r6r0rrrsetZcomponent_objectsrrr"addrr!r5Z deactivater2KeyErrorprintr )bZcsetZptsZ allow_skipZsuppress_warningsr9rZ deactivatedvisitedrr3Z non_cset_setr,Znon_cset_indexr rrr deactivate_model_ats<           rB)N)TF) collectionsrZpyomo.common.collectionsrZpyomo.core.baserrZpyomo.core.base.setrr rr!r5r.rBrrrr  s   ( 'c