Variables
The Xpress type var allows for creating optimization variables. Note that variables are not tied to a problem but may exist globally in a Python program. In order for them to be included into a problem, they have to be explicitly added to that problem. Below is the complete declaration with the list of all parameters (all of them are optional):
var (name, lb, ub, threshold, vartype)
The parameters are:
- name is a Python UTF-8 string containing the name of the variable (its ASCII version will be saved if written onto a file); a default name is assigned if the user does not specify it;
- lb is the lower bound (0 by default);
- ub is the upper bound (+inf is the default);
- threshold is the threshold for semi-continuous, semi-integer, and partially integer variables; it must be between its lower and its upper bound; it has no default, so if a variable is defined as partially integer the threshold must be specified;
- vartype is the variable type, one of the six following types:
- xpress.continuous for continuous variables;
- xpress.binary for binary variables (lower and upper bound are further restricted to 0 and 1);
- xpress.integer for integer variables;
- xpress.semicontinuous for semi-continuous variables;
- xpress.semiinteger for semi-integer variables;
- xpress.partiallyinteger for partially integer variables.
The features of each variable are accessible as members of the associated object: after declaring a variable with x = xpress.var(), its name, lower and upper bound can be accessed via x.name, x.lb, and x.ub. Note that, after a variable x has been added to one or more problems, a change in its feature will not be reflected in these problems, but only in the problems to which this variable is added subsequently.
One or more variables (or list of variables) can be added to a problem with the addVariable method:
v = xp.var(lb=-1, ub=2) m.addVariable (v) x = [xp.var(ub=10) for i in range(10)] y = [xp.var(ub=10, vartype=xp.integer) for i in range(10)] m.addVariable (x,y)
By default, variables added to an Xpress problems are constrained to be nonnegative. In order to add a free variable, one must specify its lower bound to be -∞ as follows:
v = xp.var(lb=-xp.infinity)
Variable names and Python objects
Variables and, as described below, constraints and other objects of the Xpress Python interface can have a name. Variable names and constraint names can be useful when saving a problem to a file and when querying the problem for the value of a variable in an optimal solution. If a variable is not given a name explicitly, it will be assigned a default name that is usually "C" followed by a sequence number.
Python also uses these names when printing expressions, because the variables' __str__ function is redirected to their name. Therefore, when querying Python for a variable or for an expression containing that variable, its name will be printed rather than the Python object used in the program, as in the following example:
>>> v = xp.var(lb=-1, ub=2) >>> v C1 >>> v.__str__() 'C1' >>> x = xp.var(name='myvar') >>> v + 2 * x C1 + 2 myvar >>>
This allows for querying a problem using both the variable object and its name, depending on what is more convenient. The following example prints twice an optimal solution to a simple problem:
x = xp.var(name='var1') y = xp.var(name='var2') p = xp.problem(x, y, x + y >= 3, x + 2*y) p.solve() print(p.getSolution([x, y])) print(p.getSolution(['var1', 'var2']))
It can be therefore useful to create xpress.var objects with a meaningful argument, perhaps similar to the name they have in the Python program one is writing.
© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.