0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

amplifyの作法 その12

Posted at

概要

amplifyの作法、調べてみた。
qubo使ってみた。

参考にしたページ

サンプルコード

from amplify import (BinaryPoly, BinaryQuadraticModel, gen_symbols, Solver, decode_solution, )
from amplify.client import FixstarsClient
from typing import Set, Tuple, Container, Dict, Sequence, Collection, Mapping
from dataclasses import dataclass
import networkx as nx

def to_dict(q: np.ndarray) -> Dict[Tuple[int, int], float]:
	assert len(q.shape) == 2 and  q.shape[0] == q.shape[1]
	return dict(zip(zip(*np.nonzero(q)), q[q != 0]))



def qubo_spp(cost: np.ndarray, set_flag: np.ndarray, p = 10.):
	assert cost.shape[0] == set_flag.shape[1]
	b = np.ones(set_flag.shape[0])
	return np.diagflat(cost - p * 2 * b.dot(set_flag)) + p * np.einsum("ij,ik->jk", set_flag, set_flag)

c = np.array([3, 2, 1, 1, 3, 2])
a = np.array([[1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 1, 1], [0, 0, 1, 1, 1, 0], [1, 1, 0, 1, 0, 1]])
q = qubo_spp(c, a)
assert q.tolist() == [[-17, 10, 10, 10, 0, 20], [10, -18, 10, 10, 10, 20], [10, 10, -29, 10, 20, 20], [10, 10, 10, -19, 10, 10],	[0, 10, 20, 10, -17, 10], [20, 20, 20, 10, 10, -28]]
q = to_dict(q)
print(q)
f = BinaryPoly(q)
model0 = BinaryQuadraticModel(f)
solver = Solver(client)
result = solver.solve(model0)
values = result[0].values
print(values)
#	assert is_contain(sample, np.array([1,0,0,0,1,0]))


以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?