Ticket #7637: trac_7637.patch

File trac_7637.patch, 1.8 KB (added by ncohen, 11 years ago)
  • sage/numerical/mip.pyx

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1260361682 -3600
    # Node ID 0b83d40755944807aeee73fda62ee4bf65d9d512
    # Parent  856ff670def662a9c2dddefab4ca6cbd42c43b13
    Default variable in MixedIntegerLinearProgram
    
    diff -r 856ff670def6 -r 0b83d4075594 sage/numerical/mip.pyx
    a b  
    194194            self._constraints_bounds_max == p._constraints_bounds_max
    195195            )
    196196
     197    def __getitem__(self, v):
     198        r"""
     199        Returns the symbolic variable corresponding to the key
     200        from a default dictionary.
     201
     202        It returns the element asked, and otherwise creates it.
     203        If necessary, it also creates the default dictionary.
     204
     205        This method lets the user define LinearProgram without having to
     206        define independent dictionaries when it is not necessary for him.
     207
     208        EXAMPLE::
     209
     210            sage: p = MixedIntegerLinearProgram()
     211            sage: p.set_objective(p['x'] + p['z'])
     212            sage: p['x']
     213            x0
     214        """
     215
     216        try:
     217            return self._default_mipvariable[v]
     218        except AttributeError:
     219            self._default_mipvariable = self.new_variable()
     220            return self._default_mipvariable[v]
     221
    197222    def set_problem_name(self,name):
    198223        r"""
    199224        Sets the name of the ``MixedIntegerLinearProgram``.
     
    929954            sage: b = p.new_variable()
    930955            sage: p.set_objective(sum([b[v] for v in g]))
    931956            sage: p.set_binary(b)
    932             sage: p.solve(solver='GLPK', objective_only=True)
     957            sage: p.solve(solver='GLPK', objective_only=True) # optional - requires GLPK
    933958            Traceback (most recent call last):
    934959            ...
    935960            NotImplementedError: ...