Example #1
0
def bb(A, b):

    pb = xp.problem()
    x_lp = np.array([xp.var(vartype=xp.continuous) for _ in range(A.shape[1])])
    pb.addVariable(x_lp)
    pb.addConstraint(xp.Dot(A, x_lp) <= b)
    pb.setObjective(xp.Dot(c, x_lp), sense=xp.maximize)

    lb_node = Node(xp.problem(), -1)

    root_node = Node(pb)

    is_integer, is_infeasible = root_node.solve()

    show_tree(root_node)

    if is_integer:
        return root_node.pb.getObjVal(), root_node.pb.getSolution()
    elif is_infeasible:
        return False, False

    l_node, r_node = root_node.make_sub_problems()

    active_nodes = [l_node, r_node]

    while True:
        show_tree(root_node)
        not_pruned_nodes = []
        for node in active_nodes:
            is_integer, is_infeasible = node.solve()
            if is_integer:
                if node.objVal > lb_node.objVal:
                    lb_node = node
            elif not is_infeasible and node.objVal > lb_node.objVal:
                not_pruned_nodes.append(node)
            else:
                node.is_pruned = True
            # # inp = input()

        if is_optimal(lb_node, not_pruned_nodes):
            lb_node.is_optimal = True
            show_tree(root_node)
            return lb_node.pb.getObjVal(), lb_node.pb.getSolution()

        active_nodes = []
        for node in not_pruned_nodes:
            l_node, r_node = node.make_sub_problems()
            active_nodes.append(l_node)
            active_nodes.append(r_node)
Example #2
0
#!/bin/env python

# Test problem on a dot product between matrices of scalars and/or of
# variables. Note that the problem cannot be solved by the Optimizer
# as it is nonconvex.

from __future__ import print_function

import xpress as xp
import numpy as np

a = 0.1 + np.arange(21).reshape(3, 7)
y = np.array([xp.var() for i in range(21)]).reshape(3, 7)
x = np.array([xp.var() for i in range(35)]).reshape(7, 5)

p = xp.problem()
p.addVariable(x, y)

p.addConstraint(xp.Dot(y, x) <= 0)
p.addConstraint(xp.Dot(a, x) == 1)

p.setObjective(x[0][0])

p.write("test6-nonconv", "lp")
p.solve()
Example #3
0
Q = np.arange(1, N**2 + 1).reshape(N, N)
x = np.array([xp.var() for i in range(N)])
x0 = np.random.random(N)

p = xp.problem()

p.addVariable(x)

# c1 and c2 are two systems of constraints of size N each written as
#
# (x-x0)' Q = 1
#
# Qx >= 0

c1 = -xp.Dot((x - x0), Q) == 1
c2 = xp.Dot(Q, x) >= 0

# The objective function is quadratic

p.addConstraint(c1, c2)
p.setObjective(xp.Dot(x, Q + N**3 * np.eye(N), x))

# Compact (equivalent) construction of the problem
#
# p = xp.problem(x, c1, c2, xp.Dot(x, Q + N**3 * np.eye(N), x))

p.solve("")

print("nrows, ncols:", p.attributes.rows, p.attributes.cols)
print("solution:", p.getSolution())
Example #4
0
def bb(A, b):
    T = nx.Graph()
    tree_idx = 0

    pb = xp.problem()
    x_lp = np.array([xp.var(vartype=xp.continuous) for _ in range(A.shape[1])])
    pb.addVariable(x_lp)
    pb.addConstraint(xp.Dot(A, x_lp) <= b)
    pb.setObjective(xp.Dot(c, x_lp), sense=xp.maximize)

    lb_node = Node(xp.problem(), -1)

    root_node = Node(pb, tree_idx)
    T.add_node(tree_idx)

    is_integer, is_infeasible = root_node.solve()
    if is_integer:
        return root_node.pb.getObjVal(), root_node.pb.getSolution()
    elif is_infeasible:
        return False, False

    l_node, r_node = root_node.make_sub_problems(tree_idx)

    active_nodes = [l_node, r_node]
    T.add_node(l_node.tree_idx)
    T.add_node(r_node.tree_idx)
    T.add_edge(root_node.tree_idx, l_node.tree_idx)
    T.add_edge(root_node.tree_idx, r_node.tree_idx)
    tree_idx += 3

    while True:

        not_pruned_nodes = []
        for node in active_nodes:
            is_integer, is_infeasible = node.solve()
            if is_integer:
                if node.objVal > lb_node.objVal:
                    lb_node = node
            elif not is_infeasible:
                not_pruned_nodes.append(node)

        if is_optimal(lb_node, not_pruned_nodes):
            pos = graphviz_layout(T, prog="dot")
            print(pos)
            labels = pos
            print(labels)
            nx.draw(T, pos)
            nx.draw_networkx_labels(T, pos, labels)
            plt.show()
            return lb_node.pb.getObjVal(), lb_node.pb.getSolution()

        active_nodes = []
        for node in not_pruned_nodes:
            l_node, r_node = node.make_sub_problems(tree_idx)
            active_nodes.append(l_node)
            active_nodes.append(r_node)
            T.add_node(l_node.tree_idx)
            T.add_node(r_node.tree_idx)
            T.add_edge(node.tree_idx, l_node.tree_idx)
            T.add_edge(node.tree_idx, r_node.tree_idx)
            tree_idx += 3
Example #5
0
#
# Example: use numpy to print the product of a matrix by a random vector.
#
# Uses xpress.Dot to on a matrix and a vector. Note that the NumPy dot operator
# works perfectly fine here.
#

from __future__ import print_function

import numpy as np
import xpress as xp

x = [xp.var() for i in range(5)]

p = xp.problem ()
p.addVariable (x)
p.addConstraint (xp.Sum (x) >= 2)

p.setObjective (xp.Sum (x[i]**2 for i in range (5)))
p.solve()

A   = np.array (range(30)).reshape(6,5) # A is a 6x5 matrix
sol = np.array (p.getSolution ()) # suppose it's a vector of size 5
columns = A*sol         # not a matrix-vector product!
v = xp.Dot (A,sol)      # this is a matrix-vector product A*sol

print (v)
Example #6
0
E = [[1, 2], [1, 4], [2, 3], [3, 4], [4, 5], [5, 1]]  # arcs

n = len(V)  # number of nodes
m = len(E)  # number of arcs

G = netx.DiGraph(E)

# Get NumPy representation
A = (netx.incidence_matrix(G, oriented=True).toarray())

print("incidence matrix:\n", A)

# One (random) demand for each node
demand = np.random.randint(100, size=n)
# Balance demand at nodes
demand[0] = -sum(demand[1:])

cost = np.random.randint(20, size=m)  # (Random) costs

flow = np.array([xp.var() for i in E])  # flow variables declared on arcs

p = xp.problem('network flow')

p.addVariable(flow)
p.addConstraint(xp.Dot(A, flow) == -demand)
p.setObjective(xp.Dot(cost, flow))

p.solve()

for i in range(m):
    print('flow on', E[i], ':', p.getSolution(flow[i]))
Example #7
0
#

n = 10 # dimension of variable space
h = 3  # number of polynomial constraints
k = 4  # degree of each polynomial

# Vector of n variables

x = np.array ([1] + [xp.var (lb = -10, ub = 10) for _ in range (n-1)])

sizes = [n]*k # creates list [n,n,...,n] of k elements

# Operator * before a list translates the list into its
# (unparenthesized) tuple, i.e., the result is a reshape list of
# argument that looks like (h, n, n, ..., n)

T = np.random.random (h * n ** k).reshape (h, *sizes)

print (T)

T2list = [x]*k

compact = xp.Dot (T, *T2list) <= 0

p = xp.problem ()

p.addVariable (x[1:])
p.addConstraint (compact)

p.write ('polynomial', 'lp')
Example #8
0
#
# An example of a problem formulation that uses the xpress.Dot() operator
# to formulate constraints simply. Note that the NumPy dot operator is not
# suitable here as the result is an expression in the Xpress variables.
#

import xpress as xp
import numpy as np

A = np.random.random(30).reshape(6, 5)  # A is a 6x5 matrix
Q = np.random.random(25).reshape(5, 5)  # Q is a 5x5 matrix
x = np.array([xp.var() for i in range(5)])  # vector of variables
x0 = np.random.random(5)  # random vector

Q += 4 * np.eye(5)  # add 5 * the identity matrix

Lin_sys = xp.Dot(A, x) <= np.array([3, 4, 1, 4, 8, 7
                                    ])  # 6 constraints (rows of A)
Conv_c = xp.Dot(x, Q, x) <= 1  # one quadratic constraint

p = xp.problem()

p.addVariable(x)
p.addConstraint(Lin_sys, Conv_c)
p.setObjective(xp.Dot(x - x0, x - x0))

p.solve()