CoCalc Logo Icon
DocsShareSupport News Sign UpSign In
Views: 16
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2204
Embed | Download | Raw |
Kernel: Python 3 (ipykernel)
import random import networkx as nx from IPython.core.display import SVG import pyomo.environ as pyo from pysat.solvers import Solver from pysat.formula import CNF import py_svg_combinatorics as psc from ipywidgets import widgets, HBox from collections import Counter from pprint import pprint from random import randint import numpy as np from IPython.display import IFrame import IPython from copy import copy import os from pathlib import Path def print_solution(m): for v in m.component_data_objects(pyo.Var): if v.value and v.value > 0: print(str(v), v.value) nbname = '' try: nbname = __vsc_ipynb_file__ except: if 'COCALC_JUPYTER_FILENAME' in os.environ: nbname = os.environ['COCALC_JUPYTER_FILENAME'] title_ = Path(nbname).stem.replace('-', '_').title() IFrame(f'https://discopal.ispras.ru/index.php?title=Hardprob/{title_}&useskin=cleanmonobook', width=1280, height=300)
def visme(G, m=None): def norm_size(sz): return int(sz*100) pos = nx.get_node_attributes(G, "pos") if not pos: pos = nx.shell_layout(G) cut_edges = None v_colors = ['blue']*len(G.nodes()) if m: cut_edges = [e for e in m.E if pyo.value(m.y[e])>0] for v in G.nodes(): val_= 0 if m.x[v].value is not None: val_ = m.x[v].value if val_ > 0: v_colors[v] = 'green' nx.draw_networkx( G, pos, with_labels=True, edge_color="green", node_color=v_colors, node_size=300, style=':', width=0.4, font_size=6 ) if cut_edges: nx.draw_networkx_edges( G, pos, edgelist=cut_edges, edge_color="blue", node_size=200, width=0.7, )
G = nx.fast_gnp_random_graph(12, 0.1, directed=True) for (u, v) in G.edges(): G.edges[u, v]['weight'] = 1 #np.random.rand() visme(G)
Image in a Jupyter notebook
def get_model(G): m = pyo.ConcreteModel() m.E = m.A = list(G.edges()) m.V = list(G.nodes()) m.W = dict([(e[:2], e[2]['weight']) for e in G.edges(data=True)]) m.m = len(m.A) m.n = len(m.V) # 0-1 — в левом или правом разбиении. m.x = pyo.Var(m.V, domain=pyo.Binary) # 0-1 — на разрезе или нет. m.y = pyo.Var(m.A, domain=pyo.Binary) m.cut_weight = pyo.Objective(expr = sum(m.W[e] * m.y[e] for e in m.A), sense=pyo.maximize) @m.Constraint(m.A) def ребро_на_разрезе_только_когда_начальная_вершина_в_левом(m, u, v): return m.y[(u, v)] <= 1 - m.x[u] @m.Constraint(m.A) def ребро_на_разрезе_только_когда_конечная_вершина_в_правом(m, u, v): return m.y[(u, v)] <= m.x[v] return m m = get_model(G)
solver = pyo.SolverFactory('cbc') solver.solve(m).write() print_solution(m)
# ========================================================== # = Solver Results = # ========================================================== # ---------------------------------------------------------- # Problem Information # ---------------------------------------------------------- Problem: - Name: unknown Lower bound: 7.0 Upper bound: 7.0 Number of objectives: 1 Number of constraints: 14 Number of variables: 15 Number of binary variables: 23 Number of integer variables: 23 Number of nonzeros: 10 Sense: maximize # ---------------------------------------------------------- # Solver Information # ---------------------------------------------------------- Solver: - Status: ok User time: -1.0 System time: 0.0 Wallclock time: 0.0 Termination condition: optimal Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available. Statistics: Branch and bound: Number of bounded subproblems: 0 Number of created subproblems: 0 Black box: Number of iterations: 0 Error rc: 0 Time: 0.0410923957824707 # ---------------------------------------------------------- # Solution Information # ---------------------------------------------------------- Solution: - number of solutions: 0 number of solutions displayed: 0 x[0] 1.0 x[3] 1.0 x[4] 1.0 x[10] 1.0 x[11] 1.0 y[1,0] 1.0 y[2,4] 1.0 y[5,11] 1.0 y[7,3] 1.0 y[8,10] 1.0 y[9,3] 1.0 y[9,4] 1.0
y = m.y y
<pyomo.core.base.var.IndexedVar at 0x7fabb7ba6fd0>
visme(G, m)
Image in a Jupyter notebook