Hosted by ИСПРАН Лаборатория «lab22» | Download | Raw |
Kernel: Python 3 (ipykernel)
import random 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 np.set_printoptions(precision=2) np.set_printoptions(suppress=True) 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)
# Случайные данные m = np.random.randint(3, 20) n = np.random.randint(3, 20) A = np.random.randint(1, 100, size=(m,n)) c = np.random.randint(2, size=(n)) b = np.floor(1/np.random.rand(m)) # Увеличиваем вероятность нетривиального решения for i in range(len(b)): b[i] = max(b[i], max(A[i, j] for j in range(n))) A, b, c
(array([[38, 3, 56, 83, 95], [95, 58, 46, 40, 46], [14, 91, 61, 71, 72], [96, 1, 37, 9, 41], [30, 73, 32, 33, 73], [28, 77, 57, 56, 94], [ 8, 96, 62, 31, 61], [48, 91, 54, 29, 92], [64, 54, 51, 68, 37], [39, 67, 39, 30, 85]]), array([95., 95., 91., 96., 73., 94., 96., 92., 68., 85.]), array([1, 0, 0, 0, 1]))

def get_model(A, b, c): m = pyo.ConcreteModel() m.m, m.n = A.shape # на всякий случай, возьмем с собой m.A = A m.b = b m.c = c m.I = range(m.m) m.J = range(m.n) m.x = pyo.Var(m.J, domain=pyo.Binary) m.obj = pyo.Objective(expr = sum( c[j] * m.x[j] for j in m.J), sense=pyo.maximize) @m.Constraint(m.I) def строчка_не_больше(m, i): return sum(A[i, j] * m.x[j] for j in m.J) <= b[i] return m m = get_model(A, b, c) m.pprint()
2 Set Declarations x_index : Size=1, Index=None, Ordered=Insertion Key : Dimen : Domain : Size : Members None : 1 : Any : 5 : {0, 1, 2, 3, 4} строчка_не_больше_index : Size=1, Index=None, Ordered=Insertion Key : Dimen : Domain : Size : Members None : 1 : Any : 10 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 1 Var Declarations x : Size=5, Index=x_index Key : Lower : Value : Upper : Fixed : Stale : Domain 0 : 0 : None : 1 : False : True : Binary 1 : 0 : None : 1 : False : True : Binary 2 : 0 : None : 1 : False : True : Binary 3 : 0 : None : 1 : False : True : Binary 4 : 0 : None : 1 : False : True : Binary 1 Objective Declarations obj : Size=1, Index=None, Active=True Key : Active : Sense : Expression None : True : maximize : x[0] + 0*x[1] + 0*x[2] + 0*x[3] + x[4] 1 Constraint Declarations строчка_не_больше : Size=10, Index=строчка_не_больше_index, Active=True Key : Lower : Body : Upper : Active 0 : -Inf : 38*x[0] + 3*x[1] + 56*x[2] + 83*x[3] + 95*x[4] : 95.0 : True 1 : -Inf : 95*x[0] + 58*x[1] + 46*x[2] + 40*x[3] + 46*x[4] : 95.0 : True 2 : -Inf : 14*x[0] + 91*x[1] + 61*x[2] + 71*x[3] + 72*x[4] : 91.0 : True 3 : -Inf : 96*x[0] + x[1] + 37*x[2] + 9*x[3] + 41*x[4] : 96.0 : True 4 : -Inf : 30*x[0] + 73*x[1] + 32*x[2] + 33*x[3] + 73*x[4] : 73.0 : True 5 : -Inf : 28*x[0] + 77*x[1] + 57*x[2] + 56*x[3] + 94*x[4] : 94.0 : True 6 : -Inf : 8*x[0] + 96*x[1] + 62*x[2] + 31*x[3] + 61*x[4] : 96.0 : True 7 : -Inf : 48*x[0] + 91*x[1] + 54*x[2] + 29*x[3] + 92*x[4] : 92.0 : True 8 : -Inf : 64*x[0] + 54*x[1] + 51*x[2] + 68*x[3] + 37*x[4] : 68.0 : True 9 : -Inf : 39*x[0] + 67*x[1] + 39*x[2] + 30*x[3] + 85*x[4] : 85.0 : True 5 Declarations: x_index x obj строчка_не_больше_index строчка_не_больше
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)
solver = pyo.SolverFactory('cbc') solver.solve(m).write() print_solution(m)
# ========================================================== # = Solver Results = # ========================================================== # ---------------------------------------------------------- # Problem Information # ---------------------------------------------------------- Problem: - Name: unknown Lower bound: 1.0 Upper bound: 1.0 Number of objectives: 1 Number of constraints: 8 Number of variables: 2 Number of binary variables: 5 Number of integer variables: 5 Number of nonzeros: 2 Sense: maximize # ---------------------------------------------------------- # Solver Information # ---------------------------------------------------------- Solver: - Status: ok User time: -1.0 System time: 0.0 Wallclock time: 0.01 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.04661393165588379 # ---------------------------------------------------------- # Solution Information # ---------------------------------------------------------- Solution: - number of solutions: 0 number of solutions displayed: 0 x[0] 1.0