diff --git a/__pycache__/offline.cpython-310.pyc b/__pycache__/offline.cpython-310.pyc index 8c56011a5f340cb03d65a27122df4efde591c4cc..5efa3c684dd1250014f4476d08736088512164ae 100644 Binary files a/__pycache__/offline.cpython-310.pyc and b/__pycache__/offline.cpython-310.pyc differ diff --git a/offline.py b/offline.py index 21eef3389c4be60218e1c9d484b9381f53c5a250..97b006a34a4495900817d8c9db7336daa3ec5a5b 100644 --- a/offline.py +++ b/offline.py @@ -32,6 +32,7 @@ def Solve(images, blackouts): """PARAMETERS""" h = GRB.INFINITY # time horizon = infinite + tfU_j = GRB.INFINITY # YA_t_br = [] # both not used # YB_t_br = [] @@ -39,10 +40,10 @@ def Solve(images, blackouts): """"DECISION VARIABLES""" # makespan ms = model.addVar(lb=0,vtype=GRB.CONTINUOUS) # makespan, objective function minimization value, bounded with constraints - P_j_br = model.addVars(image_count, blackout_count,lb=0,vtype=GRB.CONTINUOUS) # preemption time, will be set to 0 in Eq (33) + # P_j_br = model.addVars(image_count, blackout_count,lb=0,vtype=GRB.CONTINUOUS) # preemption time, will be set to 0 in Eq (33) PV_t = model.addVars(whitebox_count,lb=0,vtype=GRB.CONTINUOUS) # sum of length of images in whitebox t Tf_j = model.addVars(image_count,lb=0,vtype=GRB.CONTINUOUS) # finish of image j - Tf_t = model.addVars(whitebox_count,lb=0,vtype=GRB.CONTINUOUS) # finish of all images is whitebox t + Tf_t = model.addVars(whitebox_count,lb=0,vtype=GRB.CONTINUOUS) # finish of all images in whitebox t Ts_j = model.addVars(image_count,lb=0,vtype=GRB.CONTINUOUS) # start of image j Ts_t = model.addVars(whitebox_count,lb=0,vtype=GRB.CONTINUOUS) # start of timeslot t, probably redundant X_j_t = model.addVars(image_count, whitebox_count,vtype=GRB.BINARY) # order j in whitebox t @@ -52,6 +53,10 @@ def Solve(images, blackouts): YB_j_br = model.addVars(image_count, blackout_count,vtype=GRB.BINARY) # order j finishes before blackbox br """CONSTRAINTS""" + # model.addConstr(Y_j_jprime[0,0] == 0) + # model.addConstr(Y_j_jprime[0,1] == 0) + # model.addConstr(Y_j_jprime[1,0] == 1) + # model.addConstr(Y_j_jprime[1,1] == 0) # (1) minimize makespan model.setObjective(ms, GRB.MINIMIZE) # (3) @@ -63,6 +68,21 @@ def Solve(images, blackouts): for j in range(image_count) for jprime in range(image_count)), name="Eq(4)") + # (4 v2) + # model.addConstrs((Tf_j[j] <= Ts_j[jprime] + tfU_j * (1 - Y_j_jprime[j,jprime]) + # for j in range(image_count) + # for jprime in range(image_count)), + # name="Eq(4 v2)") + # (4 v3) + # for j in range(image_count): + # for jprime in range(j+1,image_count): + # model.addConstr((Tf_j[j] <= Ts_j[jprime] + tfU_j * (1 - Y_j_jprime[j,jprime])), + # name="Eq(4 v2)") + # # (5 v3) + # for j in range(image_count): + # for jprime in range(j+1,image_count): + # model.addConstr((Tf_j[jprime] <= Ts_j[j] + tfU_j * (1 + Y_j_jprime[j,jprime])), + # name="Eq(5 v3)") # (9) model.addConstrs((ms >= Tf_j[j] for j in range(image_count)), @@ -77,7 +97,8 @@ def Solve(images, blackouts): name="Eq(13)") # (17) model.addConstrs((gp.quicksum(X_j_t[j,t] for j in range(image_count)) - + Xfree_t[t] == 1 + + Xfree_t[t] + == 1 for t in range(whitebox_count)), name="Eq(17)") # (19) @@ -91,15 +112,15 @@ def Solve(images, blackouts): for t in range(whitebox_count)), name="Eq(20)") # (32) - model.addConstrs((YB_j_br[j,br_index] + YA_j_br[j,br_index] == X_j_t[j,t] - for j in range(image_count) - for t in range (whitebox_count) - for br_index in range(blackout_count)), - name="Eq(32)") + # model.addConstrs((YB_j_br[j,br_index] + YA_j_br[j,br_index] == X_j_t[j,t] + # for j in range(image_count) + # for t in range (whitebox_count) + # for br_index in range(blackout_count)), + # name="Eq(32)") # (33) - model.addConstrs((P_j_br[j,br_index] == 0 for j in range(image_count) - for br_index in range(blackout_count)), - name="Eq(33)") + # model.addConstrs((P_j_br[j,br_index] == 0 for j in range(image_count) + # for br_index in range(blackout_count)), + # name="Eq(33)") # (34) model.addConstrs((br[br_index][1] * YA_j_br[j,br_index] <= Ts_j[j] for br_index in range(blackout_count) @@ -124,36 +145,47 @@ def Solve(images, blackouts): # SOLVE AND PRINT THE RESULTS model.optimize() - res = printSolution(model) - return (res[0], res[1]) + time = -1 + starts = [-1] + if model.status == GRB.OPTIMAL: + deletus = starts.pop() + time = model.ObjVal + print('\nFinish time: %g' % time) + + print("White box decision variables:") + for (j,t) in X_j_t: + print(f"(im{j},wb{t}) = {X_j_t[j,t].X}") + + print('\nTimes:') + for im in Ts_j: + ts = Ts_j[im].X + tf = Tf_j[im].X + print(f"Image {im} starts t={ts} finishes t={tf}") + starts.append(t) + + print('\nOrder:') + for im in Ts_j: + for im2 in Ts_j: + print(f"{im} before {im2}: {Y_j_jprime[im, im2].X}") + + print('\nWhitebox contains:') + for t in PV_t: + print(f"wb{t} is {Xfree_t[t].X} empty and holds length {PV_t[t].X} of images") + + print('\nBlackboxes before/after:') + for (j,br_index) in YA_j_br: + print(f"YA_{j}_{br_index}: image {j} after blackbox {br_index} = {YA_j_br[j,br_index].X}") + print(f"YB_{j}_{br_index}: image {j} before blackbox {br_index} = {YB_j_br[j,br_index].X}") -def printSolution(model): - if model.status == GRB.OPTIMAL: - print('\nFinish time: %g' % model.ObjVal) else: print('\nNo solution, reason:') iis = model.computeIIS() # Irreducible Infeasible Subsystem, https://support.gurobi.com/hc/en-us/articles/360029969391-How-do-I-determine-why-my-model-is-infeasible- print(iis) - - # time = None - # timeSteps = [] - # if model.status == GRB.OPTIMAL: - # time = None - - # # TODO: loop over decision variables - # # i.e. for all images in whitebox j: - # # their order does not matter - # # so just paste them behind each other - - # # timeSteps.append(decVariables[x_ij].X) - # # timeSteps.append(1234) - # else: - # time = "No solution" # return (41.618, [35, 29.6, 18, 35.618, 0.86]) # answer to test instance 0 - return (45, [22, 4, 35, 42, 0, 5, 28, 11]) # answer to test instance 1 - # return (time, timeSteps) + # return (45, [22, 4, 35, 42, 0, 5, 28, 11]) # answer to test instance 1 + return (time, starts) def calculate_whiteboxes(blackouts): wb = [] diff --git a/t4_in.txt b/t4_in.txt index be816e5e89ad777b18244a0c0f190eb8f3f93df0..cf65d86d9abe2d0915a0b0a3ed67f4b02fc1a9af 100644 --- a/t4_in.txt +++ b/t4_in.txt @@ -2,4 +2,4 @@ 10 15 1 -50, 10 \ No newline at end of file +25, 5 \ No newline at end of file diff --git a/t4_out.txt b/t4_out.txt index 9975180bb6719d65a819838570afb9c978e1c60e..c006f6e306eeba32ae49910dc6aa351c5980224f 100644 --- a/t4_out.txt +++ b/t4_out.txt @@ -1,9 +1,3 @@ -45 -22 -4 -35 -42 -0 -5 -28 -11 \ No newline at end of file +15.0 +1 +1 \ No newline at end of file