Esempio n. 1
0
    def test_hybrid(self):
        import dimod
        import hybrid

        bqm = dimod.BinaryQuadraticModel({}, {'ab': 1, 'bc': -1, 'ca': 1}, 0, dimod.SPIN)

        workflow = hybrid.Loop(hybrid.Race(
            hybrid.InterruptableTabuSampler(),
            hybrid.EnergyImpactDecomposer(size=2)
            | hybrid.SimulatedAnnealingSubproblemSampler()
            | hybrid.SplatComposer()
        ) | hybrid.ArgMin(), convergence=3)

        result = workflow.run(hybrid.State.from_problem(bqm)).result()

        self.assertEqual(result.samples.first.energy, -3.0)
Esempio n. 2
0
import dimod
import hybrid


# load a problem
problem = sys.argv[1]
with open(problem) as fp:
    bqm = dimod.BinaryQuadraticModel.from_coo(fp)


# construct a workflow that races Simulated Annealing against SA/Tabu on a subproblem
iteration = hybrid.Race(
    hybrid.SimulatedAnnealingProblemSampler(),
    hybrid.EnergyImpactDecomposer(size=50)
        | hybrid.RacingBranches(
            hybrid.SimulatedAnnealingSubproblemSampler(num_sweeps=1000),
            hybrid.TabuSubproblemSampler(tenure=20, timeout=10))
        | hybrid.ArgMin('subsamples.first.energy')
        | hybrid.SplatComposer()
) | hybrid.ArgMin('samples.first.energy')
main = hybrid.Loop(iteration, max_iter=10, convergence=3)


# run the workflow
init_state = hybrid.State.from_sample(hybrid.utils.min_sample(bqm), bqm)
solution = main.run(init_state).result()

# show results
print("""
Solution:
    energy={s.samples.first.energy}