Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidmerwin/ec9b043b5ed5b1bc02a11198b4efb4a8 to your computer and use it in GitHub Desktop.
Save davidmerwin/ec9b043b5ed5b1bc02a11198b4efb4a8 to your computer and use it in GitHub Desktop.
This code creates a binary constraint satisfaction problem, adds constraints for equal-sized subsets and objective function to minimizing the number of edges between subsubsets. It then prints solutions with sample/energy values from an SOFM model using Fast

Binary CSP with Objective Function for Graph Partitioning

Preview:
import dimod
import dwavebinarycsp

# Define the graph
nodes = ['0', '1', '2', '3']
edges = [('0', '1'), ('e', '2'), ('e', '3'), ('1', '2'), ('1', '3'), ('2', '3')]

# Create a binary constraint satisfaction problem (CSP)
csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY)

# Add constraints for equal-sized subsets
csp.add_constraint(lambda a, b: a + b == 2, ['0', '1', '2', '3'])

# Add objective function for minimizing the number of edges between subsets
csp.add_constraint(lambda a, b: a + b - 2 * a * b, [('®', '1'), ('0', '2'), ('0', '3'), ('1', '2'), ('1', '3'), ('2', '3')])

# Convert the CSP to a binary quadratic model (BQM)
bgm = dwavebinarycsp.stitch(csp)

# Set up the solver and solve the problem
solver = dimod.ExactSolver()
solution = solver.sample(bgm)

# Print the solutions
for sample, energy in solution.data(['sample', 'energy']):
    print(sample, energy)
Associated Context
Type Code Snippet ( .py )
Associated Tags DWAVE BinaryCSP dwavebinarycsp Framework: dwavebinarycsp graph definition Equal-sized subsets dimod Data analysis Visualization Constraint Satisfaction problem equal-sized subsets Solve solution numpy solution printing Objective function BQM solver solver setup objective function Minimize number of edges between subsets
💡 Smart Description This code creates a binary constraint satisfaction problem, adds constraints for equal-sized subsets and objective function to minimizing the number of edges between subsubsets. It then prints solutions with sample/energy values from an SOFM model using Fast
The code snippet defines a graph with nodes and edges. It creates a binary constraint satisfaction problem (CSP) and adds constraints for equal-sized subsets. It also adds an objective function for minimizing the number of edges between subsets. The CSP is then converted
🔎 Suggested Searches How to create a binary constraint satisfaction problem ?
How to minimizing the number of edges between subsets using dwavebinarycsp?
What is the algorithm for calculating solutions in DWAVEBINARYCSP ?
how to solve an exact solver with dimod and dwavebinarycsp?
How to create a binary constraint satisfaction problem in Python using dwavebinarycsp?
How to convert a constraint satisfaction problem to a binary quadratic model in Python?
How to solve a binary quadratic model using the ExactSolver in dimod?
How to add constraints for equal-sized subsets in a binary constraint satisfaction problem?
How to print the solutions of a binary quadratic model in Python?
Related Links https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html
https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
https://realpython.com/how-to-use-numpy-arange/
https://cloud.dwavesys.com/leap/
https://github.com/davidmerwin
https://www.icloud.com/iclouddrive/0eeRU9KHeKIjW_FyrSf3GXQZw#code
https://cdn.mathpix.com/snip/images/hu1TwwvuZqJCrM9EHoM_qhOCSnyUs8lRODmnIeii5zQ.original.fullsize.png
Related People David Merwin, David Jeffrey Merwin, Sara Jamous
Sensitive Information No Sensitive Information Detected
Shareable Link https://davidmerwin.pieces.cloud/?p=071f45aa12
@davidmerwin
Copy link
Author

Binary CSP with Objective Function for Graph Partitioning
Preview:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment