|
|
import numpy as np
|
|
|
import random
|
|
|
from deap import base, creator, tools, algorithms
|
|
|
|
|
|
|
|
|
def cargo_load_planning_genetic(weights, cargo_names, cargo_types_dict, positions, cg_impact, cg_impact_2u, cg_impact_4u,
|
|
|
max_positions, population_size=100, generations=100, crossover_prob=0.7, mutation_prob=0.2):
|
|
|
"""
|
|
|
使用改进版遗传算法计算货物装载方案,最小化重心的变化量。
|
|
|
|
|
|
参数:
|
|
|
weights (list): 每个货物的质量列表。
|
|
|
cargo_names (list): 每个货物的名称。
|
|
|
cargo_types_dict (dict): 货物名称和占用的货位数量。
|
|
|
positions (list): 可用的货位编号。
|
|
|
cg_impact (list): 每个位置每kg货物对重心index的影响系数。
|
|
|
cg_impact_2u (list): 两个位置组合的重心影响系数。
|
|
|
cg_impact_4u (list): 四个位置组合的重心影响系数。
|
|
|
max_positions (int): 总货位的数量。
|
|
|
population_size (int): 遗传算法的种群大小。
|
|
|
generations (int): 遗传算法的代数。
|
|
|
crossover_prob (float): 交叉操作的概率。
|
|
|
mutation_prob (float): 变异操作的概率。
|
|
|
|
|
|
返回:
|
|
|
best_solution (np.array): 最优装载方案矩阵。
|
|
|
best_cg_change (float): 最优方案的重心变化量。
|
|
|
"""
|
|
|
try:
|
|
|
|
|
|
cargo_types = [cargo_types_dict[name] for name in cargo_names]
|
|
|
|
|
|
num_cargos = len(weights)
|
|
|
num_positions = len(positions)
|
|
|
|
|
|
|
|
|
if not hasattr(creator, "FitnessMin"):
|
|
|
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
|
|
|
if not hasattr(creator, "Individual"):
|
|
|
creator.create("Individual", list, fitness=creator.FitnessMin)
|
|
|
|
|
|
toolbox = base.Toolbox()
|
|
|
|
|
|
|
|
|
def init_individual():
|
|
|
individual = []
|
|
|
occupied = [False] * num_positions
|
|
|
for cargo_type in cargo_types:
|
|
|
if cargo_type == 1:
|
|
|
valid_positions = [j for j in range(num_positions) if not occupied[j]]
|
|
|
elif cargo_type == 2:
|
|
|
valid_positions = [j for j in range(0, num_positions - 1, 2) if not any(occupied[j + k] for k in range(cargo_type))]
|
|
|
elif cargo_type == 4:
|
|
|
valid_positions = [j for j in range(0, num_positions - 3, 4) if not any(occupied[j + k] for k in range(cargo_type))]
|
|
|
else:
|
|
|
valid_positions = []
|
|
|
|
|
|
if not valid_positions:
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
start_pos = random.randint(0, num_positions - 1)
|
|
|
elif cargo_type == 2:
|
|
|
choices = [j for j in range(0, num_positions - 1, 2)]
|
|
|
if choices:
|
|
|
start_pos = random.choice(choices)
|
|
|
else:
|
|
|
start_pos = 0
|
|
|
elif cargo_type == 4:
|
|
|
choices = [j for j in range(0, num_positions - 3, 4)]
|
|
|
if choices:
|
|
|
start_pos = random.choice(choices)
|
|
|
else:
|
|
|
start_pos = 0
|
|
|
else:
|
|
|
start_pos = 0
|
|
|
else:
|
|
|
start_pos = random.choice(valid_positions)
|
|
|
|
|
|
individual.append(start_pos)
|
|
|
|
|
|
|
|
|
for k in range(cargo_type):
|
|
|
pos = start_pos + k
|
|
|
if pos < num_positions:
|
|
|
occupied[pos] = True
|
|
|
|
|
|
return creator.Individual(individual)
|
|
|
|
|
|
toolbox.register("individual", init_individual)
|
|
|
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
|
|
|
|
|
|
|
|
|
def evaluate(individual):
|
|
|
|
|
|
occupied = [False] * num_positions
|
|
|
penalty = 0
|
|
|
cg_change = 0.0
|
|
|
|
|
|
for i, start_pos in enumerate(individual):
|
|
|
cargo_type = cargo_types[i]
|
|
|
weight = weights[i]
|
|
|
|
|
|
|
|
|
if start_pos < 0 or start_pos + cargo_type > num_positions:
|
|
|
penalty += 10000
|
|
|
continue
|
|
|
|
|
|
|
|
|
overlap = False
|
|
|
for k in range(cargo_type):
|
|
|
pos = start_pos + k
|
|
|
if occupied[pos]:
|
|
|
penalty += 10000
|
|
|
overlap = True
|
|
|
break
|
|
|
occupied[pos] = True
|
|
|
if overlap:
|
|
|
continue
|
|
|
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
cg_change += abs(weight * cg_impact[start_pos])
|
|
|
elif cargo_type == 2:
|
|
|
if start_pos % 2 == 0 and (start_pos // 2) < len(cg_impact_2u):
|
|
|
cg_change += abs(weight * cg_impact_2u[start_pos // 2])
|
|
|
else:
|
|
|
penalty += 10000
|
|
|
elif cargo_type == 4:
|
|
|
if start_pos % 4 == 0 and (start_pos // 4) < len(cg_impact_4u):
|
|
|
cg_change += abs(weight * cg_impact_4u[start_pos // 4])
|
|
|
else:
|
|
|
penalty += 10000
|
|
|
|
|
|
return (cg_change + penalty,)
|
|
|
|
|
|
toolbox.register("evaluate", evaluate)
|
|
|
toolbox.register("mate", tools.cxOnePoint)
|
|
|
toolbox.register("mutate", tools.mutShuffleIndexes, indpb=mutation_prob)
|
|
|
toolbox.register("select", tools.selRoulette)
|
|
|
|
|
|
|
|
|
population = toolbox.population(n=population_size)
|
|
|
|
|
|
|
|
|
try:
|
|
|
algorithms.eaSimple(population, toolbox, cxpb=crossover_prob, mutpb=1.0, ngen=generations,
|
|
|
verbose=False)
|
|
|
except ValueError as e:
|
|
|
print(f"遗传算法运行时出错: {e}")
|
|
|
return [], -1000000
|
|
|
|
|
|
|
|
|
try:
|
|
|
best_individual = tools.selBest(population, 1)[0]
|
|
|
best_cg_change = evaluate(best_individual)[0]
|
|
|
except IndexError as e:
|
|
|
print(f"选择最优个体时出错: {e}")
|
|
|
return [], -1000000
|
|
|
|
|
|
|
|
|
solution = np.zeros((num_cargos, num_positions))
|
|
|
for i, start_pos in enumerate(best_individual):
|
|
|
cargo_type = cargo_types[i]
|
|
|
for k in range(cargo_type):
|
|
|
pos = start_pos + k
|
|
|
if pos < num_positions:
|
|
|
solution[i, pos] = 1
|
|
|
|
|
|
return solution, best_cg_change
|
|
|
except Exception as e:
|
|
|
print(f"发生错误: {e}")
|
|
|
return [], -1000000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|