This commit is contained in:
Yofuria 2023-08-02 23:12:36 +08:00
parent a0045c341b
commit f4c2cd9532
3 changed files with 15 additions and 51 deletions

View File

@ -84,6 +84,7 @@ class FMSEnv(ap.Model):
if self.t >= self.int_stop_time: if self.t >= self.int_stop_time:
self.running = False self.running = False
self.stop() self.stop()
# else: # else:
# #
# # print(f"running the {self.t} step") # # print(f"running the {self.t} step")
@ -121,22 +122,9 @@ def GA_run(inventory_bound=None):
S = tuple(tuple([i, j]) for i, j in zip(material, inventory_bound[ S = tuple(tuple([i, j]) for i, j in zip(material, inventory_bound[
len(pd.read_excel("initial_material.xlsx").to_numpy()): len( len(pd.read_excel("initial_material.xlsx").to_numpy()): len(
pd.read_excel("initial_material.xlsx").to_numpy()) * 2])) pd.read_excel("initial_material.xlsx").to_numpy()) * 2]))
# print(s)
# print(S)
dct_para = { dct_para = {
'time': 300, # 进行总时间数 'time': 300, # 进行总时间数
# 'xv_int_max_order': random.randint(30, 50),
# 'xv_dlv_product_para': tuple([(30, 100), (30, 50)]),
# 'xv_dlv_product_para': tuple([30,40,30,20]), # 读取生产率 np.read.
# 'xv_int_dlv_period_lam': 8.5,
# 'xv_int_create_order_lam': 2,
# 'xv_ary_price_product': tuple([0.3,0.2,0.5,1]),
# 'xv_ary_cost_material_per': tuple([0.1,0.1,0.2,0.4]),
# 'xv_ary_volume_material': tuple([1.0, 1.5]),
# 'xv_ary_volume_product': tuple([3.0, 5.0]),
# 'xv_array_lead_time': 2, # 读取原材料表格 np.read, 暂时不读 变量代表的含义
# 'xv_int_lead_time_c': 3,
# 'xv_int_lead_time_d': 1,
'xv_ary_product_id': tuple(pd.read_excel("initial_product.xlsx").iloc[:, 0]), # 产成品id顺序 'xv_ary_product_id': tuple(pd.read_excel("initial_product.xlsx").iloc[:, 0]), # 产成品id顺序
'xv_ary_material_id': tuple(pd.read_excel("initial_material.xlsx").iloc[:, 0]), # 原材料id顺序 'xv_ary_material_id': tuple(pd.read_excel("initial_material.xlsx").iloc[:, 0]), # 原材料id顺序
'xv_product_num': len(pd.read_excel("initial_product.xlsx").to_numpy()), # 产成品个数 'xv_product_num': len(pd.read_excel("initial_product.xlsx").to_numpy()), # 产成品个数
@ -150,43 +138,8 @@ def GA_run(inventory_bound=None):
'xv_ary_s': s, # s 'xv_ary_s': s, # s
'xv_ary_S': S, # S 'xv_ary_S': S, # S
# 应读取遗传算法中随机生成的s暂写为'1' 创建两个excel分别存储产品和原材料的库存 每个excel中存系统代码和库存 # 应读取遗传算法中随机生成的s暂写为'1' 创建两个excel分别存储产品和原材料的库存 每个excel中存系统代码和库存
# 'xv_flt_initial_cash': 50000.0,
# 'dct_status_info': json.dumps({ #需要引入生产状态表
# "0": {"xv_flt_produce_rate": tuple([0.0, 0.0]),
# "xv_ary_mat_material": tuple([0.0, 0.0]),
# "xv_flt_broken_rate": 0,
# "xv_flt_run_cost": 0.0,
# "name": "wait"
# },
# "1": {"xv_flt_produce_rate": tuple([90.0, 0.0]),
# "xv_ary_mat_material": tuple([4.0, 1.0]),
# "xv_flt_broken_rate": 0.03,
# "xv_flt_run_cost": 40.0,
# "name": "produceA"
# },
# "2": {"xv_flt_produce_rate": tuple([0.0, 60.0]),
# "xv_ary_mat_material": tuple([1.5, 5.0]),
# "xv_flt_broken_rate": 0.05,
# "xv_flt_run_cost": 50.0,
# "name": "produceB"
# },
# "3": {"xv_flt_produce_rate": tuple([55.0, 30.0]),
# "xv_ary_mat_material": tuple([2.0, 1.5]),
# "xv_flt_broken_rate": 0.07,
# "xv_flt_run_cost": 60.0,
# "name": "produceAB"
# },
# "-1": {"xv_flt_produce_rate": 0.0,
# "xv_ary_mat_material": tuple([0.0, 0.0]),
# "xv_flt_broken_rate": 0.1,
# "xv_flt_run_cost": 100.0,
# "name": "failed"
# }
# })
} }
sample = ap.Sample(dct_para) sample = ap.Sample(dct_para)
exp = ap.Experiment(FMSEnv, sample, iterations=1, record=True) exp = ap.Experiment(FMSEnv, sample, iterations=1, record=True)
results = exp.run() results = exp.run()
return results['variables']['FMSEnv']['op_os_all_delay_time'][dct_para['time']] / 2 return results['variables']['FMSEnv']['op_os_all_delay_time'][dct_para['time']] / 2

View File

@ -42,7 +42,7 @@ class GeneticAlgorithm:
:ivar function: Object that can be used to evaluate the objective function :ivar function: Object that can be used to evaluate the objective function
""" """
def __init__(self, function, dim, lb, ub, int_var=None, pop_size=20, num_gen=300, start="Random"): def __init__(self, function, dim, lb, ub, int_var=None, pop_size=6, num_gen=300, start="Random"):
self.nvariables = dim # column self.nvariables = dim # column
self.nindividuals = pop_size + (pop_size % 2) # Make sure this is even row self.nindividuals = pop_size + (pop_size % 2) # Make sure this is even row
@ -102,6 +102,11 @@ class GeneticAlgorithm:
ind = np.where(population[:, i] > self.upper_boundary[i]) ind = np.where(population[:, i] > self.upper_boundary[i])
population[ind, i] -= 1 population[ind, i] -= 1
for pop in population:
for i in range(len(pop) // 2):
if pop[i] >= pop[i + len(pop) // 2]:
pop[i], pop[i + len(pop) // 2] = pop[i + len(pop) // 2], pop[i]
# Evaluate all individuals # Evaluate all individuals
# function_values = self.function(population) we cannot compute in this way to ensure x is one-dim in policy # function_values = self.function(population) we cannot compute in this way to ensure x is one-dim in policy
n_row, n_dim = population.shape n_row, n_dim = population.shape
@ -125,7 +130,8 @@ class GeneticAlgorithm:
for _ in range(self.ngenerations): for _ in range(self.ngenerations):
print('------------------------------') print('------------------------------')
print("当前为第{}".format(_)) print("当前为第{}".format(_))
print("最优个体为:{}".format(best_individual)) print("最优s为:{}".format(best_individual[0:115]))
print("最优S为:{}".format(best_individual[115:230]))
print("最优值为:{}".format(best_value)) print("最优值为:{}".format(best_value))
print("------------------------------") print("------------------------------")
# Do tournament selection to select the parents # Do tournament selection to select the parents
@ -174,6 +180,11 @@ class GeneticAlgorithm:
# Keep the best individual # Keep the best individual
population[0, :] = best_individual population[0, :] = best_individual
for pop in population:
for i in range(len(pop) // 2):
if pop[i] >= pop[i + len(pop) // 2]:
pop[i], pop[i + len(pop) // 2] = pop[i + len(pop) // 2], pop[i]
# Evaluate all individuals # Evaluate all individuals
# function_values = self.function(population) we cannot compute in this way to ensure x is one-dim in policy # function_values = self.function(population) we cannot compute in this way to ensure x is one-dim in policy
n_row, n_dim = population.shape n_row, n_dim = population.shape

Binary file not shown.