import sys import gurobipy as gp import traceback #大M var_M = 10000
from gurobipy import GRB, GurobiError from gurobipy import Env, GRB from gurobipy import GRB, Model, LinExpr
try: model = gp.Model("最优解场") model.setParam('OutputFlag', 0)
# num of friendly m = int(input("输入友方随从数量:")) # num of enemy n = int(input("输入敌方随从数量:"))
# Atk of friendly A = [int(input(f"输入友方第{i+1} 个随从的攻击力: ")) for i inrange(0, m )] # Hp of friendly B = [int(input(f"输入友方第{i+1} 个随从的生命值: ")) for i inrange(0, m )] # Atk of enemy P = [int(input(f"输入敌方第{i+1} 个随从的攻击力: ")) for i inrange(0, n )] # Hp of enemy Q = [int(input(f"输入敌方第{i+1} 个随从的生命值: ")) for i inrange(0, n )]
# Variable initialization #攻击方式变量 x = [[model.addVar(0, 1, 0, GRB.BINARY) for j inrange(0, n )] for i inrange(0, m )] #友方随从存活变量 y = [model.addVar(0, 1, 0, GRB.BINARY) for i inrange(0, m )] #敌方随从存活变量 z = [model.addVar(0, 1, 0, GRB.BINARY) for i inrange(0, n )] model.update() # 敌方挨打伤害总和 C = [LinExpr(sum(x[i][j] * A[i] for i inrange(0, m ))) for j inrange(0, n )] # 友方挨打伤害总和 D = [LinExpr(sum(x[i][j] * P[j] for j inrange(0, n ))) for i inrange(0, m )]
# 目标函数 target = LinExpr() for i inrange(0, n ): temp = P[i] * z[i] target += temp #场攻最小 model.setObjective(target, GRB.MINIMIZE)
# 添加约束 for i inrange(0, m ): temp = LinExpr() for j inrange(0, n ): temp += x[i][j] model.addConstr(temp <= 1) #大M法进行操作表示敌方最后的攻击力 for i inrange(0, m ): model.addConstr(D[i] + var_M * y[i] >= B[i]) model.addConstr(D[i] + var_M * y[i] <= B[i] + var_M - 1)
for j inrange(0, n ): model.addConstr(C[j] + var_M * z[j] >= Q[j]) model.addConstr(C[j] + var_M * z[j] <= Q[j] + var_M - 1) # 求解模型 model.optimize() O = model.objVal
# 获取优化结果 if model.status == GRB.OPTIMAL: # 输出最优解 print("解场方式:") for i inrange(m): for j inrange(n): ifint(x[i][j].X) == 1: print(f"友方[{i+1}]({A[i]}/{B[i]})攻击敌方[{j+1}]({P[j]}/{Q[j]})") #存活参量 print("友方") for i inrange(0, m ): print(f'y[{i}] = {y[i].x}') print("敌方") for j inrange(0, n ): print(f'z[{j}] = {z[j].x}') # 获取最优目标值 optimal_value = model.objVal print(f'最优目标值:{optimal_value}') print("\n")
else: raise Exception("无法求最优解。") #二阶段:进一步优化求解 #选择模式 print("你在解场后更在意己方随从的什么价值?") print("1.攻击力价值2.站场价值(生命力)3.下回合攻击频率4.自定义价值5.综合考量(攻击力*剩余血量)") print("\n") way=int(input("请选择你喜爱的方式(输入数字即可): ")) #第二部分 #价值函数 s = {} for i inrange(m): s[i] = model.addVar(0, GRB.INFINITY, 0, GRB.INTEGER, "s" + str(i)) target2 = LinExpr() if way == 1: print("攻击力价值") for i inrange(0, m ): model.addConstr(D[i] + var_M * y[i] >= B[i]) model.addConstr(D[i] + var_M * y[i] <= B[i] + var_M - 1) for i inrange(m): target2 += A[i]*y[i] elif way == 2: print("站场价值(生命力)") # 在这里添加站场价值板块的代码 for i inrange(m): model.addConstr(s[i] - y[i] * var_M <= 0)
for i inrange(m): model.addConstr(s[i] + D[i] - y[i] * var_M >= B[i] - var_M)
for i inrange(m): model.addConstr(s[i] + D[i] + y[i] * var_M <= B[i] + var_M)
for i inrange(m): target2 += s[i] elif way == 3: print("下回合攻击频率") for i inrange(0, m ): model.addConstr(D[i] + var_M * y[i] >= B[i]) model.addConstr(D[i] + var_M * y[i] <= B[i] + var_M - 1) for i inrange(m): target2 += y[i] elif way == 4: print("自定义价值") V = [int(input(f"输入友方第{i+1} 个随从的价值: ")) for i inrange(0, m )] for i inrange(m): target2 += V[i]*y[i] elif way == 5: print("综合考量(攻击力*剩余血量)") # 同样使用大M法 for i inrange(m): model.addConstr(s[i] - y[i] * var_M <= 0)
for i inrange(m): model.addConstr(s[i] + A[i] * D[i] - y[i] * var_M >= A[i] * B[i] - var_M)
for i inrange(m): model.addConstr(s[i] + A[i] * D[i] + y[i] * var_M <= A[i] * B[i] + var_M)
for i inrange(m): target2 += s[i] else: raise Exception("输入了未知方案") model.addConstr(target <= O)
model.setObjective(target2, GRB.MAXIMIZE)
model.update() model.optimize()
#输出结果 print("解场方式:") for i inrange(m): for j inrange(n): ifint(x[i][j].X) == 1: print(f"友方[{i+1}]({A[i]}/{B[i]})攻击敌方[{j+1}]({P[j]}/{Q[j]})")
print("死亡的友方随从:") for i inrange(m): ifint(y[i].X) == 0: print(f"[{i+1}]({A[i]}/{B[i]}) ", end="") print()
print("死亡的敌方随从:") for i inrange(n): ifint(z[i].X) == 0: print(f"[{i+1}]({P[i]}/{Q[i]}) ", end="") print()
print("解场后:") print("敌方:") for i inrange(n): ifint(z[i].X) == 1: print(f"[{i+1}]({P[i]}/{Q[i]}) ", end="") print()
print("友方:") for i inrange(m): ifint(y[i].X) == 1: print(f"[{i+1}]({A[i]}/{B[i]}) ", end="") print()
print("解场后场上友方随从剩余价值最大值 =", model.objVal) print("\n") print("相关参数:") print("X[i][j]") for i inrange(m): for j inrange(n): print(int(x[i][j].X), end="") print("\n") print("Y[i]") for i inrange(m): print(int(y[i].X), end=" ") print("\n") print("Z[i]") for i inrange(n): print(int(z[i].X), end=" ")