修复最终结果打印仍有重复问题,放大了延期的影响效果
This commit is contained in:
parent
aba7dc18d3
commit
dcea3818f1
|
|
@ -108,5 +108,5 @@ class DataStructures:
|
|||
else:
|
||||
cost_ratio = 2*(cost / optimal_cost)
|
||||
# 延期处理(+1避免除以零)
|
||||
tardiness_ratio = 8*((tardiness + 1) / (optimal_tardiness + 1))
|
||||
tardiness_ratio = 800*((tardiness + 1) / (optimal_tardiness + 1))
|
||||
return cost_ratio + tardiness_ratio
|
||||
18
main.py
18
main.py
|
|
@ -34,6 +34,7 @@ def main():
|
|||
population = encoder.initialize_population()
|
||||
print(f"初始化种群完成,(种群大小,染色体长度): {population.shape if population.size > 0 else '空'}")
|
||||
|
||||
|
||||
# 若种群初始化失败(为空),直接退出
|
||||
if population.size == 0:
|
||||
print("错误:种群初始化失败,无法继续进化")
|
||||
|
|
@ -57,6 +58,7 @@ def main():
|
|||
# 非支配排序,获取当前代的帕累托前沿
|
||||
ranks, fronts = nsga2.fast_non_dominated_sort(objectives)
|
||||
current_front = fronts[0] if fronts else [] # 第0层为最优前沿
|
||||
|
||||
current_front_objs = [objectives[i] for i in current_front] if current_front else []
|
||||
|
||||
best_front = population[current_front] if current_front else [] # 更新当前最优前沿解(整数)
|
||||
|
|
@ -78,6 +80,7 @@ def main():
|
|||
if (cost_change < config.early_stop_threshold and
|
||||
tardiness_change < config.early_stop_threshold):
|
||||
no_improve_count += 1
|
||||
|
||||
else:
|
||||
no_improve_count = 0 # 有改进,重置计数器
|
||||
prev_avg_cost = avg_cost
|
||||
|
|
@ -106,6 +109,7 @@ def main():
|
|||
selected_len = len(selected) # selected的长度(等于pop_size)
|
||||
i = 0
|
||||
max_iter = 2 * config.pop_size # 最大迭代次数,避免无限循环
|
||||
|
||||
iter_count = 0
|
||||
while len(offspring) < config.pop_size and iter_count < max_iter:
|
||||
iter_count += 1
|
||||
|
|
@ -131,6 +135,7 @@ def main():
|
|||
i += 1 # 处理下一个个体(步长改为1,避免快速越界)
|
||||
# 若迭代次数用尽仍未生成足够子代,补充随机个体(健壮性处理,整数)
|
||||
while len(offspring) < config.pop_size:
|
||||
|
||||
offspring.append(encoder._generate_random_chromosome()) # 整数化随机染色体
|
||||
# 变异操作(均匀变异,整数化)
|
||||
offspring = [
|
||||
|
|
@ -152,6 +157,7 @@ def main():
|
|||
# 早停检查(连续多代无改进则停止)
|
||||
if no_improve_count >= config.early_stop_patience:
|
||||
print(f"早停触发:连续{no_improve_count}代无改进,终止于第{generation}代")
|
||||
|
||||
break
|
||||
# 每50代打印一次进度
|
||||
if generation % 50 == 0:
|
||||
|
|
@ -163,17 +169,15 @@ def main():
|
|||
# 7. 结果可视化与输出(整数化)
|
||||
print("进化完成,处理结果(整数化)...")
|
||||
if len(best_front_objs) > 0:
|
||||
# 1. 过滤重复解
|
||||
# 1. 过滤重复解(关键改进:基于目标值去重,确保相同目标值只保留一个解)
|
||||
unique_front = []
|
||||
unique_front_objs = []
|
||||
seen = set()
|
||||
seen_obj = set() # 仅跟踪目标值,确保相同目标值只保留一个
|
||||
|
||||
for sol, obj in zip(best_front, best_front_objs):
|
||||
# 将解和目标值组合为元组用于查重
|
||||
sol_tuple = tuple(sol.tolist())
|
||||
obj_tuple = tuple(obj)
|
||||
if (sol_tuple, obj_tuple) not in seen:
|
||||
seen.add((sol_tuple, obj_tuple))
|
||||
obj_tuple = tuple(obj) # 将目标值转为元组用于查重
|
||||
if obj_tuple not in seen_obj:
|
||||
seen_obj.add(obj_tuple)
|
||||
unique_front.append(sol)
|
||||
unique_front_objs.append(obj)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue