コード例 #1
0
ファイル: bigm.py プロジェクト: CanLi1/pyomo-1
    def _get_M_from_args(self, constraint, bigMargs):
        # check args: we only have to look for constraint, constraintdata, and
        # None
        if bigMargs is None:
            return None

        cuid = ComponentUID(constraint)
        parentcuid = ComponentUID(constraint.parent_component())
        if cuid in bigMargs:
            return bigMargs[cuid]
        elif parentcuid in bigMargs:
            return bigMargs[parentcuid]
        elif None in bigMargs:
            return bigMargs[None]
        return None
コード例 #2
0
ファイル: util.py プロジェクト: jinliangma/pyomo
def target_list(x):
    if isinstance(x, ComponentUID):
        return [x]
    elif isinstance(x, (_ComponentBase, string_types)):
        return [ComponentUID(x)]
    elif hasattr(x, '__iter__'):
        ans = []
        for i in x:
            if isinstance(i, ComponentUID):
                ans.append(i)
            elif isinstance(i, (_ComponentBase, string_types)):
                ans.append(ComponentUID(i))
            else:
                raise ValueError(
                    "Expected ComponentUID, Component, Component name, "
                    "or list of these.\n\tReceived %s" % (type(i), ))

        return ans
    else:
        raise ValueError("Expected ComponentUID, Component, Component name, "
                         "or list of these.\n\tReceived %s" % (type(x), ))
コード例 #3
0
ファイル: PyomoModel.py プロジェクト: Isaquedanielre/pyomo
 def __getstate__(self):
     state = {}
     state['index'] = self.index
     state['_instance'] = self._instance()
     solutions = []
     for soln in self.solutions:
         soln_ = {}
         soln_['metadata'] = soln._metadata
         tmp = {}
         for (name, data) in iteritems(soln._entry):
             tmp[name] = {}
             tmp[name].update( (ComponentUID(obj()), entry) for (obj, entry) in itervalues(data) )
         soln_['entry'] = tmp
         solutions.append(soln_)
     state['solutions'] = solutions
     return state
コード例 #4
0
 def __call__(self, obj=None):
     return ComponentUID(obj)
コード例 #5
0
    def add_solution(self,
                     solution,
                     smap_id,
                     delete_symbol_map=True,
                     cache=None,
                     ignore_invalid_labels=False,
                     ignore_missing_symbols=True,
                     default_variable_value=None):

        instance = self._instance()

        soln = ModelSolution()
        soln._metadata['status'] = solution.status
        if not type(solution.message) is UndefinedData:
            soln._metadata['message'] = solution.message
        if not type(solution.gap) is UndefinedData:
            soln._metadata['gap'] = solution.gap

        if smap_id is None:
            #
            # Cache symbol names, which might be re-used in subsequent
            # calls to add_solution()
            #
            if cache is None:
                cache = {}
            if solution._cuid:
                #
                # Loading a solution with CUID keys
                #
                if len(cache) == 0:
                    for obj in instance.component_data_objects(Var):
                        cache[ComponentUID(obj)] = obj
                    for obj in instance.component_data_objects(Objective,
                                                               active=True):
                        cache[ComponentUID(obj)] = obj
                    for obj in instance.component_data_objects(Constraint,
                                                               active=True):
                        cache[ComponentUID(obj)] = obj

                for name in ['problem', 'objective', 'variable', 'constraint']:
                    tmp = soln._entry[name]
                    for cuid, val in iteritems(getattr(solution, name)):
                        obj = cache.get(cuid, None)
                        if obj is None:
                            if ignore_invalid_labels:
                                continue
                            raise RuntimeError(
                                "CUID %s is missing from model %s" %
                                (str(cuid), instance.name))
                        tmp[id(obj)] = (weakref_ref(obj), val)
            else:
                #
                # Loading a solution with string keys
                #
                if len(cache) == 0:
                    for obj in instance.component_data_objects(Var):
                        cache[obj.name] = obj
                    for obj in instance.component_data_objects(Objective,
                                                               active=True):
                        cache[obj.name] = obj
                    for obj in instance.component_data_objects(Constraint,
                                                               active=True):
                        cache[obj.name] = obj

                for name in ['problem', 'objective', 'variable', 'constraint']:
                    tmp = soln._entry[name]
                    for symb, val in iteritems(getattr(solution, name)):
                        obj = cache.get(symb, None)
                        if obj is None:
                            if ignore_invalid_labels:
                                continue
                            raise RuntimeError(
                                "Symbol %s is missing from model %s" %
                                (symb, instance.name))
                        tmp[id(obj)] = (weakref_ref(obj), val)
        else:
            #
            # Map solution
            #
            smap = self.symbol_map[smap_id]
            for name in ['problem', 'objective', 'variable', 'constraint']:
                tmp = soln._entry[name]
                for symb, val in iteritems(getattr(solution, name)):
                    if symb in smap.bySymbol:
                        obj = smap.bySymbol[symb]
                    elif symb in smap.aliases:
                        obj = smap.aliases[symb]
                    elif ignore_missing_symbols:
                        continue
                    else:  #pragma:nocover
                        #
                        # This should never happen ...
                        #
                        raise RuntimeError(
                            "ERROR: Symbol %s is missing from "
                            "model %s when loading with a symbol map!" %
                            (symb, instance.name))

                    tmp[id(obj())] = (obj, val)
            #
            # Wrap up
            #
            if delete_symbol_map:
                self.delete_symbol_map(smap_id)

        #
        # Collect fixed variables
        #
        tmp = soln._entry['variable']
        for vdata in instance.component_data_objects(Var):
            id_ = id(vdata)
            if vdata.fixed:
                tmp[id_] = (weakref_ref(vdata), {'Value': value(vdata)})
            elif (default_variable_value is not None) and \
                 (smap_id is not None) and \
                 (id_ in smap.byObject) and \
                 (id_ not in tmp):
                tmp[id_] = (weakref_ref(vdata), {
                    'Value': default_variable_value
                })

        self.solutions.append(soln)
        return len(self.solutions) - 1