Пример #1
0
def main(instance, niter, seed):
    root = MipTreeNode.root(instance)
    sols = mcts.run(root, iter_limit=niter, rng_seed=seed)
    info("solutions found: {}".format(sols))
    info("best found objective: {}".format(sols.best.value))
    if sols.best.is_feas:
        info("best solution (non-zeros):")
        for var, val in sols.best.data.items():
            if is_nonzero(val):
                info("\t{}:\t{}".format(var, val))
    return root, sols
Пример #2
0
def verify_instance(instance, *args, **kwargs):
    if isinstance(instance, (tuple, list)):
        items, capacity, optimum = instance
    elif callable(instance):
        items, capacity, optimum = instance()
    else:
        raise TypeError("unexpected instance value: {}".format(instance))
    mcts.config_logging(level="INFO")
    root = KnapsackTreeNode.root([items, capacity])
    sols = mcts.run(root, *args, **kwargs)
    assert set(sols.best.data) == optimum
    assert root.is_exhausted
Пример #3
0
            kk_edges, diff = karmarkar_karp(self.labels)
            return mcts.Solution(value=objective(diff), data=edges + kk_edges)


def make_partition(edges):
    adj = {JOIN: defaultdict(set), SPLIT: defaultdict(set)}
    for i, j, edge_type in edges:
        adj_edge_type = adj[edge_type]
        adj_edge_type[i].add(j)
        adj_edge_type[j].add(i)
    nverts = len(edges) + 1
    subset = [None] * nverts
    _assign_subset(adj, subset, 0, 0)
    return subset


def _assign_subset(adj, subset, i, s):
    subset[i] = s
    for edge_type in (JOIN, SPLIT):
        adj_edge_type = adj[edge_type]
        adj_edge_type_i = adj_edge_type.pop(i, ())
        s_j = s if edge_type == JOIN else 1 - s
        for j in adj_edge_type_i:
            adj_edge_type[j].remove(i)
            _assign_subset(adj, subset, j, s_j)


mcts.config_logging()
r = TreeNode.root("instances/npp/hard1000.dat")
s = mcts.run(r, iter_limit=1000)