bap/final.py
def master_problem(cuts, lb=0, ub=num_rounds, integrality=False):
    """Creates and solves the master problem.

    Args:
      cuts: The cuts generated by the CP model for the master problem.
      integrality: Enforce integrality constraints.

    Returns:
      Objective value, solution, duals, mu
    """
    mp = gp.Model()
    mp.Params.OutputFlag = 0

    var_type=gp.GRB.INTEGER if integrality else gp.GRB.CONTINUOUS
    
    # Objective.
    phi_high = mp.addVar(name='phi_high', vtype=gp.GRB.CONTINUOUS)
    phi_low = mp.addVar(name='phi_low', vtype=gp.GRB.CONTINUOUS)
    phi = mp.addVar(name='phi', vtype=gp.GRB.CONTINUOUS)
    mp.addConstr(phi == phi_high-phi_low)
    mp.setObjective(phi, gp.GRB.MINIMIZE)
    x_vars = mp.addVars(len(allocations), vtype=var_type, name='x')

    # The n constraints constraining the value of phi.
    mp.addConstrs((gp.quicksum(coverages[j][i]*x_vars[j] for j in range(len(allocations))) <= phi_high for i in range(n)), name='phi_high_cstr')
    mp.addConstrs((gp.quicksum(coverages[j][i]*x_vars[j] for j in range(len(allocations))) >= phi_low for i in range(n)), name='phi_low_cstr')

    # Add the CP-generated cuts.
    for cut in cuts:
        mp.addConstr(gp.quicksum([x_vars[i] for i in cut]) <= num_rounds-1)
        
    # Time horizon constraint: We must use T configurations.
    mp.addConstr(gp.quicksum(x_vars) == num_rounds, name='time')

    mp.update()
    mp.optimize()

    if integrality:
        duals = None
        mu = None
    if not integrality:
        duals_low = [mp.getConstrByName(c).Pi for c in [f'phi_low_cstr[{i}]' for i in range(n)]]
        duals_high = [mp.getConstrByName(c).Pi for c in [f'phi_high_cstr[{i}]' for i in range(n)]]
        mu = mp.getConstrByName('time').Pi

    obj = mp.getObjective().getValue()
    x_res = [mp.getVarByName('x['+str(i)+']').X for i in range(len(allocations))]
    
    return obj, x_res, [sum([-x[0], -x[1]]) for x in zip(duals_low, duals_high)], mu
Creates and solves the master problem.

    Args:
      cuts: The cuts generated by the CP model for the master problem.
      integrality: Enforce integrality constraints.

    Returns:
      Objective value, solution, duals, mu
    
Creates and solves the master problem.
Creates and solves the master problem .
def master_problem(cuts, lb=0, ub=num_rounds, integrality=False):
    mp = gp.Model()
    mp.Params.OutputFlag = 0
    var_type=gp.GRB.INTEGER if integrality else gp.GRB.CONTINUOUS
    phi_high = mp.addVar(name='phi_high', vtype=gp.GRB.CONTINUOUS)
    phi_low = mp.addVar(name='phi_low', vtype=gp.GRB.CONTINUOUS)
    phi = mp.addVar(name='phi', vtype=gp.GRB.CONTINUOUS)
    mp.addConstr(phi == phi_high-phi_low)
    mp.setObjective(phi, gp.GRB.MINIMIZE)
    x_vars = mp.addVars(len(allocations), vtype=var_type, name='x')
    mp.addConstrs((gp.quicksum(coverages[j][i]*x_vars[j] for j in range(len(allocations))) <= phi_high for i in range(n)), name='phi_high_cstr')
    mp.addConstrs((gp.quicksum(coverages[j][i]*x_vars[j] for j in range(len(allocations))) >= phi_low for i in range(n)), name='phi_low_cstr')
    for cut in cuts:
        mp.addConstr(gp.quicksum([x_vars[i] for i in cut]) <= num_rounds-1)
    mp.addConstr(gp.quicksum(x_vars) == num_rounds, name='time')
    mp.update()
    mp.optimize()
    if integrality:
        duals = None
        mu = None
    if not integrality:
        duals_low = [mp.getConstrByName(c).Pi for c in [f'phi_low_cstr[{i}]' for i in range(n)]]
        duals_high = [mp.getConstrByName(c).Pi for c in [f'phi_high_cstr[{i}]' for i in range(n)]]
        mu = mp.getConstrByName('time').Pi
    obj = mp.getObjective().getValue()
    x_res = [mp.getVarByName('x['+str(i)+']').X for i in range(len(allocations))]
    return obj, x_res, [sum([-x[0], -x[1]]) for x in zip(duals_low, duals_high)], mu
def master_problem ( cuts , lb = 0 , ub = num_rounds , integrality = False ) : mp = gp . Model ( ) mp . Params . OutputFlag = 0 var_type = gp . GRB . INTEGER if integrality else gp . GRB . CONTINUOUS phi_high = mp . addVar ( name = 'phi_high' , vtype = gp . GRB . CONTINUOUS ) phi_low = mp . addVar ( name = 'phi_low' , vtype = gp . GRB . CONTINUOUS ) phi = mp . addVar ( name = 'phi' , vtype = gp . GRB . CONTINUOUS ) mp . addConstr ( phi == phi_high - phi_low ) mp . setObjective ( phi , gp . GRB . MINIMIZE ) x_vars = mp . addVars ( len ( allocations ) , vtype = var_type , name = 'x' ) mp . addConstrs ( ( gp . quicksum ( coverages [ j ] [ i ] * x_vars [ j ] for j in range ( len ( allocations ) ) ) <= phi_high for i in range ( n ) ) , name = 'phi_high_cstr' ) mp . addConstrs ( ( gp . quicksum ( coverages [ j ] [ i ] * x_vars [ j ] for j in range ( len ( allocations ) ) ) >= phi_low for i in range ( n ) ) , name = 'phi_low_cstr' ) for cut in cuts : mp . addConstr ( gp . quicksum ( [ x_vars [ i ] for i in cut ] ) <= num_rounds - 1 ) mp . addConstr ( gp . quicksum ( x_vars ) == num_rounds , name = 'time' ) mp . update ( ) mp . optimize ( ) if integrality : duals = None mu = None if not integrality : duals_low = [ mp . getConstrByName ( c ) . Pi for c in [ f'phi_low_cstr[{i}]' for i in range ( n ) ] ] duals_high = [ mp . getConstrByName ( c ) . Pi for c in [ f'phi_high_cstr[{i}]' for i in range ( n ) ] ] mu = mp . getConstrByName ( 'time' ) . Pi obj = mp . getObjective ( ) . getValue ( ) x_res = [ mp . getVarByName ( 'x[' + str ( i ) + ']' ) . X for i in range ( len ( allocations ) ) ] return obj , x_res , [ sum ( [ - x [ 0 ] , - x [ 1 ] ] ) for x in zip ( duals_low , duals_high ) ] , mu
Creates and solves the master problem.
Creates and solves the master problem .
"""Creates and solves the master problem.

    Args:
      cuts: The cuts generated by the CP model for the master problem.
      integrality: Enforce integrality constraints.

    Returns:
      Objective value, solution, duals, mu
    """ # Objective. # The n constraints constraining the value of phi. # Add the CP-generated cuts. # Time horizon constraint: We must use T configurations.
Objective value, solution, duals, mu
Objective value solution duals mu
cuts
The cuts generated by the CP model for the master problem.
The cuts generated by the CP model for the master problem .
lb
ub
integrality
Enforce integrality constraints.
Enforce integrality constraints .