-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppmsolver
48 lines (40 loc) · 1.52 KB
/
ppmsolver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import sys
import time
import itertools
import click
import pandas as pd
from dwave.system import LeapHybridCQMSampler
from dimod import ConstrainedQuadraticModel, BinaryQuadraticModel, QuadraticModel
# Problem
import random
num_items = 100 # Set this to the desired number of items
weights = [random.randint(0, 100) for _ in range(num_items)]
values = [random.randint(0, 100) for _ in range(num_items)]
max_weight = 5000
print(f"solving for {len(values)} items")
print("build the model")
cqm = ConstrainedQuadraticModel()
obj = BinaryQuadraticModel(vartype="BINARY")
constraint = QuadraticModel()
for i in range(len(values)):
obj.add_variable(i)
obj.set_linear(i, -values[i])
constraint.add_variable("BINARY", i)
constraint.set_linear(i, weights[i])
cqm.set_objective(obj)
cqm.add_constraint(constraint, sense="<=", rhs=max_weight, label="capacity")
sampler = LeapHybridCQMSampler()
print(f"submit model to solver {sampler.solver.name}")
sampleset = sampler.sample_cqm(cqm, label="knapsack problem")
print("parse the solver output")
feasible_sampleset = sampleset.filter(lambda row: row.is_feasible)
if not len(feasible_sampleset):
raise ValueError("No feasible solution found")
best = feasible_sampleset.first
selected_items = [key for key, val in best.sample.items() if val == 1.0]
total_weight = sum([weights[i] for i in selected_items])
total_value = sum([values[i] for i in selected_items])
print(f"Selected items: {selected_items}")
print(f"Total weight: {total_weight}")
print(f"Total value: {total_value}")