Coder Social home page Coder Social logo

dwave-examples / graph-coloring Goto Github PK

View Code? Open in Web Editor NEW
11.0 6.0 13.0 40.94 MB

A demo of graph coloring using Leap's hybrid constrained quadratic model (CQM) solver.

License: Apache License 2.0

Python 100.00%
hybrid-solution good-first-example cqm beginner

graph-coloring's Introduction

Open in GitHub Codespaces Linux/Mac/Windows build status

Graph Coloring

A demo of graph coloring using Leap's hybrid constrained quadratic model (CQM) solver.

Original Plot

Figure: The graph that we want to color with no neighboring nodes the same color.

We want to color this graph so that no neighboring nodes have the same color. Graph coloring is a well-known hard problem and an alternative formulation is available in this collection of code examples (see Map Coloring). In this example, we formulate this problem as a constrained quadratic model (CQM) and solve it using the hybrid CQM solver.

Usage

To run the graph coloring demo, enter the command:

python graph_coloring.py

The program will print out the number of colors used and whether or not the solution is valid, and produce an image of the solution saved as result_graph.png.

Color Plot

Also included is a map coloring demo, map_coloring.py. To run the map coloring demo, enter the command:

python map_coloring.py

The map coloring demo has an option to run on the Canadian map (-c canada) or the USA map (-c usa, default) and produces an output image file as shown below.

USA Map

Code Overview

The hybrid CQM solver accepts problems expressed in terms of a ConstrainedQuadraticModel object. A CQM consists of an objective and/or constraints, both formulated as Quadratic Models (QMs). In this model, the problem is formulated as a constraint satisfaction problem and does not have an objective.

In this problem, we define binary variables as ordered pairs (node, color). For example, the pair (1, 2) corresponds to node 1 and color 2. In the map coloring example, the pair will look like ('Maryland', 0) to indicate that the state of Maryland should be colored with color 0 in the USA map. If variable (n, i) equals 1 then we assign node n color i.

Constraints

In this example, we have two groups of constraints.

Discrete Constraints

Since each node must be assigned exactly one color, we need a set of discrete constraints: one constraint for each node n so that it is assigned exactly one color.

Edge Constraints

Our assignment of colors to nodes must satisfy the constraint that no edge has endpoints with the same color. In terms of map coloring, this is equivalent to the constraint that no adjacent regions having the same colors assigned.

To build this constraint, we consider each edge (u, v) independently, and each possible color independently as well. For edge (u, v) and color i, we want to ensure that binary variables (u, i) and (v, i) are not both equal to 1. All other combinations are acceptable. We can determine an expression for this using a truth table, as shown below.

(u,i) (v,i) Acceptable?
0 0 Yes (0)
0 1 Yes (0)
1 0 Yes (0)
1 1 No (1)

By using one value (0) to denote "Yes" and a different value (1) to denote "No", we are able to formulate a mathematical expression to build the constraint. This constraint can be expressed as (u,i) * (v,i) == 0.

License

Released under the Apache License 2.0. See LICENSE file.

graph-coloring's People

Contributors

arcondello avatar hemantdwave avatar joelgdwave avatar joelpasvolsky avatar mike-sol avatar randomir avatar vgoliber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

graph-coloring's Issues

Canada case failing

Error message:

Reading shp file...
Traceback (most recent call last):
File "/workspace/graph-coloring/map_coloring.py", line 157, in
state_records, state_neighbors = get_state_info(input_shp_file)
File "/workspace/graph-coloring/map_coloring.py", line 49, in get_state_info
for state in sf.records():
File "/workspace/.pip-modules/lib/python3.7/site-packages/shapefile.py", line 1306, in records
r = self.__record(oid=i)
File "/workspace/.pip-modules/lib/python3.7/site-packages/shapefile.py", line 1281, in __record
value = u(value, self.encoding, self.encodingErrors)
File "/workspace/.pip-modules/lib/python3.7/site-packages/shapefile.py", line 104, in u
return v.decode(encoding, encodingErrors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 11: invalid continuation byte

Add visualization

README shows images and results, but none are produced in the demo. It would be nice to have an image generated to visualize the results.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.