Example #1
0
def generate_ising(args):
    domain_size = args.range
    size = args.size
    d = VariableDomain('d', 'dummy', range(domain_size))
    variables = {}
    constraints = {}
    for i in range(size):
        for j in range(size):
            v = Variable('v{}_{}'.format(i, j), d,
                         floor(domain_size * random.random()))
            variables[(i, j)] = v

    for i, j in variables:
        c = _create_ising_constraint(i, j, i, (j + 1) % size, domain_size,
                                     variables)
        constraints[(i, j, i, (j + 1) % size)] = c

        c = _create_ising_constraint(i, j, (i + 1) % size, j, domain_size,
                                     variables)
        constraints[(i, j, (i + 1) % size), j] = c

    dcop = DCOP('radom ising', 'min')
    #dcop.domains = {'d': d}
    #dcop.variables = variables
    dcop._agents_def = {}
    for c in constraints.values():
        dcop.add_constraint(c)

    if args.output:
        outputfile = args.output[0]
        write_in_file(outputfile, dcop_yaml(dcop))
    else:
        print(dcop_yaml(dcop))
Example #2
0
def load_dcop(dcop_str: str) -> DCOP:
    loaded = yaml.load(dcop_str)

    if 'name' not in loaded:
        raise ValueError('Missing name in dcop string')
    if 'objective' not in loaded or loaded['objective'] not in ['min', 'max']:
        raise ValueError('Objective is mandatory and must be min or max')

    dcop = DCOP(loaded['name'], loaded['objective'],
                loaded['description'] if 'description' in loaded else '')

    dcop.domains = _build_domains(loaded)
    dcop.variables = _build_variables(loaded, dcop)
    dcop.external_variables = _build_external_variables(loaded, dcop)
    dcop._constraints = _build_constraints(loaded, dcop)
    dcop._agents_def = _build_agents(loaded)
    dcop.dist_hints = _build_dist_hints(loaded, dcop)
    return dcop
Example #3
0
def load_dcop(dcop_str: str) -> DCOP:
    loaded = yaml.load(dcop_str, Loader=yaml.FullLoader)

    if "name" not in loaded:
        raise ValueError("Missing name in dcop string")
    if "objective" not in loaded or loaded["objective"] not in ["min", "max"]:
        raise ValueError("Objective is mandatory and must be min or max")

    dcop = DCOP(
        loaded["name"],
        loaded["objective"],
        loaded["description"] if "description" in loaded else "",
    )

    dcop.domains = _build_domains(loaded)
    dcop.variables = _build_variables(loaded, dcop)
    dcop.external_variables = _build_external_variables(loaded, dcop)
    dcop._constraints = _build_constraints(loaded, dcop)
    dcop._agents_def = _build_agents(loaded)
    dcop.dist_hints = _build_dist_hints(loaded, dcop)
    return dcop