示例#1
0
def solve():
    N = int(stdin.readline().strip())
    data = collections.defaultdict(list)
    for i in range(N):
        sentence = stdin.readline().strip()
        for word in sentence.split(" "):
            data[word].append(i)
    model = Numberjack.Model()
    is_english = Numberjack.VarArray(N, 0, 1)
    model.add(is_english[0] == 1)
    model.add(is_english[1] == 0)
    common = Numberjack.Variable(0, 1000000)
    zero = Numberjack.Variable(0,0)
    total = []
    for word, sentences in data.items():
        have_french = Numberjack.Variable(0, 1)
        for i in sentences:
            model.add(have_french <= is_english[i])
        # have_french is 0 if there is french, otherwise 0 or 1
        have_english = Numberjack.Variable(0, 1)
        for i in sentences:
            model.add(have_english >= is_english[i])
        # have_english is 1 if there is english, otherwise 0 or 1
        have_both = Numberjack.Variable(0, 1)
        model.add(have_both >= have_english - have_french)
        total.append(have_both)
    model.add(Numberjack.Sum(total) <= common)
    model.add(Numberjack.Minimize(common))

    solver = model.load("SCIP")
    #solver.setVerbosity(1)
    solver.solve()
    assert solver.is_opt()
    return common.get_value()
示例#2
0
 def __init__(self, grid_categories, extra_categories=None):
     if nj is None:
         raise ValueError("Numberjack not installed, cannot use Solver.")
     super().__init__(grid_categories)
     self.xcats = extra_categories or dict()
     # Create grids of variables for each pair of categories
     self.vgrids = {n: {} for n in self.categories}
     self.all_grids = []
     for f1, f2 in self.pairs:
         vname = '{}_{}.'.format(f1, f2)
         mat = nj.Matrix(self.num_items, self.num_items, vname)
         # Like the normal grid, these are two views of the same object
         self.vgrids[f1][f2] = mat
         self.vgrids[f2][f1] = mat.col
         self.all_grids.append(mat)
     # Create variables for each extra category
     self.vcats = {}
     for (name, domain) in self.xcats.items():
         v = self.vcats[name] = {}
         for cat in self.categories:
             vname = '{}_{}'.format(cat, name)
             v[cat] = nj.VarArray(self.num_items, domain, vname)
     self.vars = VarLookup(self)
     # Precompute sanity constraints
     self.constraints = (self.cons_rowcol() + self.cons_sanity())
     self.model = self.solver = None
     self.soln = self._soln_df = self._soln_grid = None
 def __init__(self, mk_list, mk_pairs, recmap_f):
     self.mk = list(mk_list)
     self.pairs = mk_pairs
     self.recf = recmap_f
     self.L = len(self.mk)
     ## Initialize Optim. model
     ### Creates an array of phase indicators
     self.Variables = nj.VarArray(self.L)
     ### Init model with simple constraint Var[0]==False
     self.constraints = [self.Variables[0] == 0]
示例#4
0
 def knapsack(costs, nutrients, n):
     z = nj.Variable(0, 10000)
     x = nj.VarArray(len(costs), 0, 20)
     model = nj.Model(
         z >= 0,
         z == nj.Sum(x, costs),
         nj.Sum(x, nutrients[0]) >= n[0],  #nutrient #1
         nj.Sum(x, nutrients[1]) >= n[1],  #nutrient #2
         nj.Sum(x, nutrients[2]) >= n[2],  #nutrient #3
         nj.Sum(x, nutrients[3]) >= n[3],  #nutrient #4
         nj.Sum(x, nutrients[4]) >= n[4],  #nutrient #5
         nj.Sum(x, nutrients[5]) >= n[5],  #nutrient #6
         nj.Minimise(z))
     return [model, x, z]
示例#5
0
def solve(vol, temp, sources):
    model = Numberjack.Model()
    times = Numberjack.VarArray( len(sources), 0.0, 1e100 )
    worst = Numberjack.Variable(0.0, 1e100)
    # add constraints
    # times * sources.rate == volume
    rates = [x[0] for x in sources]
    model.add(Numberjack.Sum(times, rates) == vol)
    # times * sources.rate * sources.temp == temp * vol
    tmp = [x[0] * x[1] for x in sources]
    model.add(Numberjack.Sum(times, tmp) == vol * temp)
    for i in range(len(sources)):
        model.add(worst >= times[i])
    model.add(Numberjack.Minimize(worst))
    solver = model.load("SCIP")
    solver.solve()
    if not solver.is_opt():
        return "IMPOSSIBLE"
    return "%.14f" % worst.get_value()
示例#6
0
 def knapsack(costs, nutrients, n, patient):
     z = nj.Variable(patient, 10000)
     x = nj.VarArray(len(costs), 0, 1)
     model = nj.Model(
         #            (z >= patient) & (z <= 950),
         #z >= patient,
         z == nj.Sum(x, costs),
         (nj.Sum(x, nutrients[0]) >= n[0]) &
         (nj.Sum(x, nutrients[0]) <= 250000),  #nutrient #1
         (nj.Sum(x, nutrients[1]) >= n[1]) &
         (nj.Sum(x, nutrients[1]) <= 20000),  #nutrient #2
         (nj.Sum(x, nutrients[2]) >= n[2]) &
         (nj.Sum(x, nutrients[2]) <= 10000),  #nutrient #3
         (nj.Sum(x, nutrients[3]) >= n[3]) &
         (nj.Sum(x, nutrients[3]) <= 1100),  #nutrient #4
         (nj.Sum(x, nutrients[4]) >= n[4]) &
         (nj.Sum(x, nutrients[4]) <= 200000),  #nutrient #5
         (nj.Sum(x, nutrients[5]) >= n[5]) &
         (nj.Sum(x, nutrients[5]) <= 10000),  #nutrient #6
         #nj.Sum(x)<=20,
         nj.Minimise(z))
     return [model, x, z]