dwavebinarycsp

从一个有小约束(何为小)的可满足问题(Constraint SatisfactionProblem),CSP中构造二元二次模型的库函数
下方是使用例子

import dwavebinarycsp
import dimod
csp = dwavebinarycsp.factories.random_2in4sat(8, 4) # 8 variables, 4 clauses
bqm = dwavebinarycsp.stitch(csp)
resp = dimod.ExactSolver().sample(bqm)
for sample, energy in resp.data(['sample', 'energy']):
print(sample, csp.check(sample), energy)

介绍

dwavebinarycsp是一个库,用于为约束满足问题(csp)构造二进制二次模型,该问题中二进制变量(表示为二进制值{0,1}或自旋值{-1,1})存在小的约束。
有关CSPs的介绍,请参见网页

例:解决地图上色问CSP问题

地图着色CSP将为地图的每个区域分配一种颜色,使得共享边界的任何两个区域具有不同的颜色。
有限域CSP由一组变量、每个变量的域规范以及变量的允许值组合的约束规范组成。要解决像Dwave系统采样器上映射的着色CSP的问题,数学公式必须使用二进制变量,因为解决方案是用量子位物理实现的,因此必须转换为自旋∈1,+1}或等效的二进制值∈ {0,1}变量。这意味着,在用数学方法表述问题时,可以用一元编码来表示C个颜色:每个区域用C个变量表示,若选择了某个颜色,对应变量为1,其余C-1个变量为0.
本示例使用四种颜色来解决加拿大地图的地图着色问题。加拿大的13个省用邮政编码表示:
在这里插入图片描述
解决方案的工作流程如下:
1.将问题公式化为一个图,用节点表示省份,共享边界表示边,每个省使用4个二进制变量(代表一个颜色)。
2.创建一个二元约束满足问题csp,并添加所有需要的约束。
3.转换成二进制二次模型。bqm
4.采样
5.如果找到了有效的解决方案,请绘制一个图。
下面的示例代码创建了一个地图图形,其中以省份为节点,并在省份之间共享边界
作为边(例如,“(‘AB’,‘BC’)”是代表不列颠哥伦比亚省和艾伯塔省之间共享边界的边)。它基于两种类型的约束创建了一个二元约束满足问题:

  • csp.add_constraint(one_color_configurations, variables) 表每个节点(省)选择单一颜色的约束,由有效配置表示:one_color_configurations = {(0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), (1,0, 0, 0)}
  • csp.add_constraint(not_both_1, variables)表示具有共享边(边界)的两个节点(省份)不都选择相同颜色的约束。
import dwavebinarycsp
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite
import networkx as nx
import matplotlib.pyplot as plt
# Represent the map as the nodes and edges of a graph
provinces = ['AB', 'BC', 'MB', 'NB', 'NL', 'NS', 'NT', 'NU', 'ON', 'PE', 'QC', 'SK', ˓→'YT']
neighbors = [('AB', 'BC'), ('AB', 'NT'), ('AB', 'SK'), ('BC', 'NT'), ('BC', 'YT'), (
˓→'MB', 'NU'),
('MB', 'ON'), ('MB', 'SK'), ('NB', 'NS'), ('NB', 'QC'), ('NL', 'QC'), (
˓→'NT', 'NU'),
('NT', 'SK'), ('NT', 'YT'), ('ON', 'QC')]
# Function for the constraint that two nodes with a shared edge not both select one color
def not_both_1(v, u):
return not (v and u)
# Function that plots a returned sample
def plot_map(sample):
G = nx.Graph()
G.add_nodes_from(provinces)
G.add_edges_from(neighbors)
# Translate from binary to integer color representation
color_map = {
   }
for province in provinces:
for i in range(colors):
if sample[province+str(i)]:
color_map[province] = i
# Plot the sample with color-coded nodes
node_colors = [color_map.get(node) for node in G.nodes()]
nx.draw_circular(G, with_labels=True, node_color=node_colors, node_size=3000, ˓→cmap=plt.cm.rainbow)
plt.show()
# Valid configurations for the constraint that each node select a single color
one_color_configurations = {
   (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), (1, 0, 0, 0)}
colors = len(one_color_configurations)
# Create a binary constraint satisfaction problem
csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY)
# Add constraint that each node (province) select a single color
for province in provinces:
variables = [province+str(i) for i in range(colors)]
csp.add_constraint(one_color_configurations, variables)
# Add constraint that each pair of nodes with a shared edge not both select one color
for neighbor in neighbors:
v, u = neighbor
for i in range(colors):
variables = [v+str(i), u+str(i)]
csp.add_constraint(not_both_1, variables)
# Convert the binary constraint satisfaction problem to a binary quadratic model
bqm = dwavebinarycsp.stitch(csp)
# Set up a solver using the local system’s default D-Wave Cloud Client configurationfile
# and sample 50 times
sampler = EmbeddingComposite(DWaveSampler()) # doctest: +SKIP
response = sampler.sample(bqm, num_reads=50) # doctest: +SKIP
# Plot the lowest-energy sample if it meets the constraints
sample = next(response.samples()) # doctest: +SKIP
if not csp.check(sample): # doctest: +SKIP
print("Failed to color map")
else:
plot_map(sample)

图显示了由Dwave求解器返回的解。有共同边界的省份没有相同的颜色。
四色加拿大地图解决方案。该图包括13个节点,代表由代表共享边界的边连接的省份。没有两个由一条边连接的节点共享一种颜色

定义可满足约束问题

约束满足问题要求问题的所有变量都在有限域之外赋值*(???)*,从而满足所有约束。constraintSatisfactionProb

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值