Ticket #7789: trac_7789.patch

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

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1263659416 -3600
    # Node ID 3eae2c0af79f0346722cfe76307b4d21876fe55e
    # Parent  263f1caae32dd7ac94a108e8b279a9872b56f260
    Small modifications to MixedIntegerLinearProgram.new_variable
    
    diff -r 263f1caae32d -r 3eae2c0af79f sage/numerical/mip.pyx
    a b  
    293293            v._update_variables_name()
    294294
    295295
    296     def new_variable(self, vtype=-1, dim=1,name=None):
     296    def new_variable(self, real=False, binary=False, integer=False, dim=1,name=None):
    297297        r"""
    298298        Returns an instance of ``MIPVariable`` associated
    299299        to the current instance of ``MixedIntegerLinearProgram``.
     
    314314        - ``dim`` (integer) -- Defines the dimension of the dictionary.
    315315          If ``x`` has dimension `2`, its fields will be of the form
    316316          ``x[key1][key2]``.
    317         - ``vtype`` (integer) -- Defines the type of the variables
    318           (default is ``REAL``).
    319         - ``name`` (string) -- A name for the variable
    320           ( default is V+number )
     317
     318        - ``binary, integer, real`` (boolean) -- Set one of these arguments
     319          to ``True`` to ensure that the variable gets the corresponding
     320          type. The default type is ``real``.
     321
     322        - ``name`` (string) -- Associates a name to the variable. This is
     323          only useful when exporting the linear program to a file using
     324          ``write_mps`` or ``write_lp``, and has no other effect.         
    321325
    322326        EXAMPLE::
    323327
    324328            sage: p = MixedIntegerLinearProgram()
    325329
    326         The available types are ``p.__REAL``, ``p.__INTEGER`` and ``p.__BINARY``::
     330         To define two dictionaries of variables, the first being
     331         of real type, and the second of integer type ::
    327332
    328             sage: x = p.new_variable(vtype=p.__REAL)
    329             sage: y = p.new_variable(dim=2)
     333            sage: x = p.new_variable(real=True)
     334            sage: y = p.new_variable(dim=2, integer=True)
    330335            sage: p.add_constraint(x[2] + y[3][5], max=2)
     336            sage: p.is_integer(x[2])
     337            False
     338            sage: p.is_integer(y[3][5])
     339            True
     340
     341        An exception is raised when two types are supplied ::
     342
     343            sage: z = p.new_variable(real = True, integer = True)
     344            Traceback (most recent call last):
     345            ...
     346            ValueError: Exactly one of the available types has to be True
    331347        """
    332348        if name==None:
    333349            name="V"+str(len(self._mipvariables))
     350
     351        if sum([real, binary, integer]) >= 2:
     352            raise ValueError("Exactly one of the available types has to be True")
     353
     354        if binary:
     355            vtype = self.__BINARY
     356        elif integer:
     357            vtype = self.__INTEGER
     358        else:
     359            vtype = self.__REAL
     360
    334361        v=MIPVariable(self, vtype, dim=dim,name=name)
    335362        self._mipvariables.append(v)
    336363        return v