Beispiel #1
0
import sys
import pytoulbar2

Lines = open(sys.argv[1], 'r').readlines()
N = len(Lines)
Matrix = [[int(e) for e in l.split(' ')] for l in Lines]
symmetric = all([
    Matrix[i][j] == Matrix[j][i] for i in range(N) for j in range(N) if j > i
])

K = int(sys.argv[2])

Top = 1 + N * N
Problem = pytoulbar2.CFN(Top)

Problem.Option.verbose = 0  # show toulbar2 statistics
Problem.Option.showSolutions = 3  # output solutions found with variable and value names

# order matrix M variables starting from the main diagonal and moving away progressively
# if input graph is symmetric then keep only the upper triangular matrix of M
for u in range(K):
    Problem.AddVariable("M_" + str(u) + "_" + str(u), range(2))

for d in range(K):
    for u in range(K):
        for v in range(K):
            if u != v and (not symmetric or u < v) and abs(u - v) == d:
                Problem.AddVariable("M_" + str(u) + "_" + str(v), range(2))

# give node variable names
Var = [(chr(65 + i) if N < 28 else "x" + str(i))
Beispiel #2
0
for i in range(N):
    token()  # skip appearance time
    # Times per plane: {earliest landing time, target landing time, latest landing time}
    LT.append([token(), token(), token()])

    # Penalty cost per unit of time per plane:
    # [for landing before target, after target]
    PC.append([token(), token()])

    # Separation time required after i lands before j can land
    ST.append([token() for j in range(N)])

top = 99999

Problem = pytoulbar2.CFN(top)
for i in range(N):
    Problem.AddVariable('x' + str(i), range(LT[i][0], LT[i][2] + 1))

for i in range(N):
    Problem.AddFunction(
        [i],
        [PC[i][0] * abs(a - LT[i][1]) for a in range(LT[i][0], LT[i][2] + 1)])

for i in range(N):
    for j in range(i + 1, N):
        Problem.AddFunction([i, j], [
            top * (a + ST[i][j] > b and b + ST[j][i] > a)
            for a in range(LT[i][0], LT[i][2] + 1)
            for b in range(LT[j][0], LT[j][2] + 1)
        ])
Beispiel #3
0
##########################################################################
# Auxiliary CFN functions for Sudoku : 6 13
##########################################################################
CP_mode = False

grid_number = 13
cgrid = hints[grid_number]
csol = sols[grid_number]
grid = [int(h) for h in cgrid]

# list of row, column and cells variable indices
rows = [[] for _ in range(size)]
columns = [[] for _ in range(size)]
cells = [[] for _ in range(size)]

myCFN = pytoulbar2.CFN(1) if CP_mode else pytoulbar2.CFN(1000000, 6)

# create variables and keep indices in row, columns and cells
for i in range(size):
    for j in range(size):
        vIdx = myCFN.AddVariable("X" + str(i + 1) + "." + str(j + 1),
                                 range(1, size + 1))
        columns[j].append(vIdx)
        rows[i].append(vIdx)
        cells[(i // par) * par + (j // par)].append(vIdx)

# add the clique constraints on rows, columns and cells
for scope in rows + columns + cells:
    addCliqueAllDiff(myCFN, scope, myCFN.GetUB())

# assign/bias variables
Beispiel #4
0
 def test_1(self):
    myCFN = pytoulbar2.CFN(2)
    res = myCFN.Solve()
    self.assertEqual(res[0],[])
    self.assertEqual(res[1],0.0)
    self.assertEqual(res[2],1)
Beispiel #5
0
        theCFN.AddFunction(vp, different)


# Sets the value of variable with index "vIdx" to "value" using a unary function
def setHint(theCFN, vIdx, value):
    costs = theCFN.GetUB() * np.ones(size, dtype=np.int64)
    costs[value - 1] = 0
    theCFN.AddFunction([vIdx], costs)


def printGrid(l):
    for i, v in enumerate(l):
        print(v, end=(' ' if (i + 1) % size else '\n'))


myCFN = pytoulbar2.CFN(1)

# Sudoku size parameter (typical 3 gives 3*3 x 3*3 grid)
par = 3
size = par * par

# Prefilled grids/solutions from the validation set of the RRN paper (0 meaning unknown)
valid = pd.read_csv("valid.csv.xz", sep=",", header=None).values
hints = valid[:][:, 0]
sols = valid[:][:, 1]

grid = [int(h) for h in hints[0]]

# list of row, column and cells variable indices
rows = [[] for _ in range(size)]
columns = [[] for _ in range(size)]
Beispiel #6
0
"""

Test basic pytoulbar2 API.

"""

import sys
import random
random.seed()
import pytoulbar2

# create a new empty cost function network with 2-digit precision and initial upper bound of 100
Problem = pytoulbar2.CFN(100., resolution=2)

# add three Boolean variables and a 4-value variable
x = Problem.AddVariable('x', range(2))
y = Problem.AddVariable('y', range(2))
z = Problem.AddVariable('z', range(2))
u = Problem.AddVariable('u', range(4))

# add random unary cost functions on each variable
Problem.AddFunction([x],
                    [random.uniform(-99.9, 99.9),
                     random.uniform(-99.9, 99.9)])
Problem.AddFunction([y],
                    [random.uniform(-99.9, 99.9),
                     random.uniform(-99.9, 99.9)])
Problem.AddFunction([z],
                    [random.uniform(-99.9, 99.9),
                     random.uniform(-99.9, 99.9)])