bap/final.py
def cp_solve(V, E, lb, ub, col_cov, cuts=[], tl=999999):
    """Solves a partial problem with a CP model.
    
    Args:
      V: List of vertices (columns).
      E: List of edges (if a transition between two columns is allowed).
      col_cov: Matrix of the zone coverages of the columns (c[i][j] == 1 if
               zone i is covered by column j).

    Returns:
      - Objective value of the best Hamiltonian path, -1 if there is no
        Hamiltonian path within the LB/UB limits, -2 if the graph is not
        connected (this latter case has been removed).
      - A feasible solution for this objective value.
    """
    cp_start_time = time.time()
    num_cols = len(V)
    num_zones = len(col_cov)
    
    # First, check if the graph is disconnected (in which case no
    # Hamiltonian path exists).
    G = networkx.Graph()
    G.add_nodes_from(V)
    G.add_edges_from(E)
    # # If the graph is not connected, no Hamiltonian path can exist.
    # if not networkx.is_connected(G):
    #     return -2, []

    # Variables.
    model = cp_model.CpModel()
    x = [model.NewIntVar(0, num_cols-1, 'x'+str(i)) for i in range(num_rounds)]

    # Alternative for GCC, since the constraint is not available in OR-Tools.
    x_occs = []
    for i in range(num_cols):
        occs = []
        for j in range(num_rounds):
            boolvar = model.NewBoolVar('')
            model.Add(x[j] == i).OnlyEnforceIf(boolvar)
            model.Add(x[j] != i).OnlyEnforceIf(boolvar.Not())
            occs.append(boolvar)
        x_occs.append(sum(occs))
        # if mp_integer:
        #     model.AddLinearConstraint(x_occs[i], 1, num_rounds-num_cols+1)

    # Add the CP cuts.
    for cut in cuts:
        model.Add(sum(x_occs[i] for i in range(num_cols) if i in cut) <= num_rounds-1)

    # Objective.
    if ub == 9999:
        ub = num_rounds+1
    phi = model.NewIntVar(int(lb), math.floor(ub)-1, 'phi')
    coverages = [model.NewIntVar(0, num_rounds, 'c'+str(i))
                 for i in range(num_zones)]
    for i in range(num_zones):
        model.Add(cp_model.LinearExpr.ScalProd(x_occs, col_cov[i]) == coverages[i])
    phi_low = model.NewIntVar(0, num_rounds, 'phi_low')
    phi_high = model.NewIntVar(0, num_rounds, 'phi_high')
    model.AddMinEquality(phi_low, coverages)
    model.AddMaxEquality(phi_high, coverages)
    model.Add(phi == phi_high-phi_low)
    model.Minimize(phi)

    # Regular constraint (Hamiltonian path).
    # For the initial state, we use a dummy node which is connected to
    # all other nodes.
    dummy = max(V)+1
    start = dummy
    end = V
    arcs = [(dummy, i, i) for i in V]
    for e in E:
        arcs.append((e[0], e[1], e[1]))
    # Node self-loops
    for v in V:
        arcs.append((v, v, v))
    # If there is only one vertex then a Hamiltonian path exists.
    if len(V) > 1:
        model.AddAutomaton(x, start, end, arcs)

    # Solve the model.
    solver = cp_model.CpSolver()
    solver.parameters.max_time_in_seconds = tl
    status = solver.Solve(model)
    #assert status == cp_model.OPTIMAL or status == cp_model.INFEASIBLE or status == cp_model.FEASIBLE

    if status == cp_model.OPTIMAL:
        solution = [solver.Value(x[i]) for i in range(num_rounds)]
        return solver.ObjectiveValue(), solution, time.time()-cp_start_time
    elif status == cp_model.INFEASIBLE or status == cp_model.UNKNOWN:
        return -1, [], time.time()-cp_start_time
    elif status == cp_model.FEASIBLE:
        return solver.ObjectiveValue(), [], time.time()-cp_start_time
Solves a partial problem with a CP model.
    
    Args:
      V: List of vertices (columns).
      E: List of edges (if a transition between two columns is allowed).
      col_cov: Matrix of the zone coverages of the columns (c[i][j] == 1 if
               zone i is covered by column j).

    Returns:
      - Objective value of the best Hamiltonian path, -1 if there is no
        Hamiltonian path within the LB/UB limits, -2 if the graph is not
        connected (this latter case has been removed).
      - A feasible solution for this objective value.
    
Solves a partial problem with a CP model.
Solves a partial problem with a CP model .
def cp_solve(V, E, lb, ub, col_cov, cuts=[], tl=999999):
    cp_start_time = time.time()
    num_cols = len(V)
    num_zones = len(col_cov)
    G = networkx.Graph()
    G.add_nodes_from(V)
    G.add_edges_from(E)
    model = cp_model.CpModel()
    x = [model.NewIntVar(0, num_cols-1, 'x'+str(i)) for i in range(num_rounds)]
    x_occs = []
    for i in range(num_cols):
        occs = []
        for j in range(num_rounds):
            boolvar = model.NewBoolVar('')
            model.Add(x[j] == i).OnlyEnforceIf(boolvar)
            model.Add(x[j] != i).OnlyEnforceIf(boolvar.Not())
            occs.append(boolvar)
        x_occs.append(sum(occs))
    for cut in cuts:
        model.Add(sum(x_occs[i] for i in range(num_cols) if i in cut) <= num_rounds-1)
    if ub == 9999:
        ub = num_rounds+1
    phi = model.NewIntVar(int(lb), math.floor(ub)-1, 'phi')
    coverages = [model.NewIntVar(0, num_rounds, 'c'+str(i))
                 for i in range(num_zones)]
    for i in range(num_zones):
        model.Add(cp_model.LinearExpr.ScalProd(x_occs, col_cov[i]) == coverages[i])
    phi_low = model.NewIntVar(0, num_rounds, 'phi_low')
    phi_high = model.NewIntVar(0, num_rounds, 'phi_high')
    model.AddMinEquality(phi_low, coverages)
    model.AddMaxEquality(phi_high, coverages)
    model.Add(phi == phi_high-phi_low)
    model.Minimize(phi)
    dummy = max(V)+1
    start = dummy
    end = V
    arcs = [(dummy, i, i) for i in V]
    for e in E:
        arcs.append((e[0], e[1], e[1]))
    for v in V:
        arcs.append((v, v, v))
    if len(V) > 1:
        model.AddAutomaton(x, start, end, arcs)
    solver = cp_model.CpSolver()
    solver.parameters.max_time_in_seconds = tl
    status = solver.Solve(model)
    if status == cp_model.OPTIMAL:
        solution = [solver.Value(x[i]) for i in range(num_rounds)]
        return solver.ObjectiveValue(), solution, time.time()-cp_start_time
    elif status == cp_model.INFEASIBLE or status == cp_model.UNKNOWN:
        return -1, [], time.time()-cp_start_time
    elif status == cp_model.FEASIBLE:
        return solver.ObjectiveValue(), [], time.time()-cp_start_time
def cp_solve ( V , E , lb , ub , col_cov , cuts = [ ] , tl = 999999 ) : cp_start_time = time . time ( ) num_cols = len ( V ) num_zones = len ( col_cov ) G = networkx . Graph ( ) G . add_nodes_from ( V ) G . add_edges_from ( E ) model = cp_model . CpModel ( ) x = [ model . NewIntVar ( 0 , num_cols - 1 , 'x' + str ( i ) ) for i in range ( num_rounds ) ] x_occs = [ ] for i in range ( num_cols ) : occs = [ ] for j in range ( num_rounds ) : boolvar = model . NewBoolVar ( '' ) model . Add ( x [ j ] == i ) . OnlyEnforceIf ( boolvar ) model . Add ( x [ j ] != i ) . OnlyEnforceIf ( boolvar . Not ( ) ) occs . append ( boolvar ) x_occs . append ( sum ( occs ) ) for cut in cuts : model . Add ( sum ( x_occs [ i ] for i in range ( num_cols ) if i in cut ) <= num_rounds - 1 ) if ub == 9999 : ub = num_rounds + 1 phi = model . NewIntVar ( int ( lb ) , math . floor ( ub ) - 1 , 'phi' ) coverages = [ model . NewIntVar ( 0 , num_rounds , 'c' + str ( i ) ) for i in range ( num_zones ) ] for i in range ( num_zones ) : model . Add ( cp_model . LinearExpr . ScalProd ( x_occs , col_cov [ i ] ) == coverages [ i ] ) phi_low = model . NewIntVar ( 0 , num_rounds , 'phi_low' ) phi_high = model . NewIntVar ( 0 , num_rounds , 'phi_high' ) model . AddMinEquality ( phi_low , coverages ) model . AddMaxEquality ( phi_high , coverages ) model . Add ( phi == phi_high - phi_low ) model . Minimize ( phi ) dummy = max ( V ) + 1 start = dummy end = V arcs = [ ( dummy , i , i ) for i in V ] for e in E : arcs . append ( ( e [ 0 ] , e [ 1 ] , e [ 1 ] ) ) for v in V : arcs . append ( ( v , v , v ) ) if len ( V ) > 1 : model . AddAutomaton ( x , start , end , arcs ) solver = cp_model . CpSolver ( ) solver . parameters . max_time_in_seconds = tl status = solver . Solve ( model ) if status == cp_model . OPTIMAL : solution = [ solver . Value ( x [ i ] ) for i in range ( num_rounds ) ] return solver . ObjectiveValue ( ) , solution , time . time ( ) - cp_start_time elif status == cp_model . INFEASIBLE or status == cp_model . UNKNOWN : return - 1 , [ ] , time . time ( ) - cp_start_time elif status == cp_model . FEASIBLE : return solver . ObjectiveValue ( ) , [ ] , time . time ( ) - cp_start_time
Solves a partial problem with a CP model.
Solves a partial problem with a CP model .
"""Solves a partial problem with a CP model.
    
    Args:
      V: List of vertices (columns).
      E: List of edges (if a transition between two columns is allowed).
      col_cov: Matrix of the zone coverages of the columns (c[i][j] == 1 if
               zone i is covered by column j).

    Returns:
      - Objective value of the best Hamiltonian path, -1 if there is no
        Hamiltonian path within the LB/UB limits, -2 if the graph is not
        connected (this latter case has been removed).
      - A feasible solution for this objective value.
    """ # First, check if the graph is disconnected (in which case no # Hamiltonian path exists). # # If the graph is not connected, no Hamiltonian path can exist. # if not networkx.is_connected(G): #     return -2, [] # Variables. # Alternative for GCC, since the constraint is not available in OR-Tools. # if mp_integer: #     model.AddLinearConstraint(x_occs[i], 1, num_rounds-num_cols+1) # Add the CP cuts. # Objective. # Regular constraint (Hamiltonian path). # For the initial state, we use a dummy node which is connected to # all other nodes. # Node self-loops # If there is only one vertex then a Hamiltonian path exists. # Solve the model. #assert status == cp_model.OPTIMAL or status == cp_model.INFEASIBLE or status == cp_model.FEASIBLE
Objective value of the best Hamiltonian path, -1 if there is no
Hamiltonian path within the LB/UB limits, -2 if the graph is not
connected (this latter case has been removed).
A feasible solution for this objective value.
Objective value of the best Hamiltonian path - 1 if there is no Hamiltonian path within the LB / UB limits - 2 if the graph is not connected ( this latter case has been removed ) . A feasible solution for this objective value .
V
List of vertices (columns).
List of vertices ( columns ) .
E
List of edges (if a transition between two columns is allowed).
List of edges ( if a transition between two columns is allowed ) .
lb
ub
col_cov
Matrix of the zone coverages of the columns (c[i][j] == 1 if
zone i is covered by column j).
Matrix of the zone coverages of the columns ( c [ i ] [ j ] == 1 if zone i is covered by column j ) .
cuts
tl