同样的输入得到的 结果不一样
def change(self,change_task1,change_task2):current_task1 = change_task1
current_task2 = change_task2
current_image_chance2 = current_task2.getImageChance()
optional_image_chance_list2 = current_task2.getOptionalImageChanceList()
check = check_task_conflict_t.CheckTaskConflict()
if(current_task1.id != 0):
print(1)
current_image_chance1 = current_task1.getImageChance()
optional_image_chance_list1 = current_task1.getOptionalImageChanceList()
#min_num = min(len(optional_image_chance_list1),len(optional_image_chance_list2))
before_chance1 = copy.deepcopy(current_task1.getImageChance())
before_chance2 = copy.deepcopy(current_task2.getImageChance())
#print('min_num',min_num)
for i in range(len(optional_image_chance_list1)-1):
#r1 = random.randint(0,len(optional_image_chance_list1)-1)
#if(current_image_chance1 == optional_image_chance_list1):
if(current_image_chance1 == optional_image_chance_list1):
continue
for j in range(len(optional_image_chance_list2)-1):
#r2 = random.randint(0,len(optional_image_chance_list2)-1)
if(current_image_chance2 == optional_image_chance_list2):
continue
k1 = self.solution.index(current_task1)
k2 = self.solution.index(current_task2)
if(self.solution.getImageChance() == None):
print(None)
#print('chance_begin_time_k1',self.solution.getImageChance().id)
print('chance_begin_time_k2',self.solution.getImageChance().id)
before_change_successful_task_num = check.returnTotalNumSuccessfulTask(self.solution)
print('before_num',before_change_successful_task_num)
current_task1.setImageChance(optional_image_chance_list1)
current_task2.setImageChance(optional_image_chance_list2)
print('chance_begin_time_k1',self.solution.getImageChance().id)
print('chance_begin_time_k2',self.solution.getImageChance().id)
#if(self.solution.getImageChance() != None):
# print('task_id_k1',k1,'chance_begin_time_k1',self.solution.getImageChance().id)
#else:
# print('task_id_k1',k1,None)
#if(self.solution.getImageChance() != None):
# print('task_id_k2',k2,'chance_begin_time_k2',self.solution.getImageChance().id)
#else:
# print('task_id_k2',k2,None)
after_change_successful_task_num = check.returnTotalNumSuccessfulTask(self.solution)
print('after_num',after_change_successful_task_num)
#if(after_change_successful_task_num >= before_change_successful_task_num):
if(after_change_successful_task_num > before_change_successful_task_num):
if(self.para_m >= self.para_m_limit):
self.para_m = self.para_m*self.descend_factor
self.accept_num1 += 1
#print(self.para_m, file = self.fpara_m)
#print('para_m',self.para_m)
print('self.accept_num1',self.accept_num1)
if(after_change_successful_task_num > self.global_task_num):
self.global_task_num = after_change_successful_task_num
self.global_solution = copy.deepcopy(self.solution)
return True
else:
return False
elif(after_change_successful_task_num == before_change_successful_task_num):
print(3)
current_task1.setImageChance(before_chance1)
#print()
current_task2.setImageChance(before_chance2)
self.para_m = self.para_m*self.rise_factor
#print(self.para_m, file = self.fpara_m)
#print('self.para_m',self.para_m)
self.deny_num += 1
print('self.deny_num',self.deny_num)
else:
difference_successful_num = after_change_successful_task_num - before_change_successful_task_num
r3 = random.uniform(0,1)
if(math.exp(difference_successful_num/self.para_m) >=r3):
self.accept_num2 += 1
print('self.accept_num2',self.accept_num2)
return False
else:
current_task1.setImageChance(before_chance1)
current_task2.setImageChance(before_chance2)
self.para_m = self.para_m*self.rise_factor
#print(self.para_m, file = self.fpara_m)
print('self.para_m',self.para_m)
self.deny_num += 1
print('self.deny_num',self.deny_num)
运行结果如下:
change_task1 4253 change_task2 4391
1
None
chance_begin_time_k2 57707.988687
before_num 19
chance_begin_time_k1 8537.036254
chance_begin_time_k2 58308.711166
after_num 19
3
self.deny_num 1
None
chance_begin_time_k2 57707.988687
before_num 20
chance_begin_time_k1 8537.036254
chance_begin_time_k2 57107.595882
after_num 19
def get_none_or_not_none_task_list(self):
j,k = 0,0
for i in range(len(self.solution)):
task = self.solution
if(task.getImageChance() == None):
self.none_task_list = task
j += 1
else:
self.not_none_task_list = task
k += 1
valid_not_none_num = np.count_nonzero(self.not_none_task_list)
self.not_none_task_list = self.not_none_task_list
self.none_task_list = self.none_task_list
def selectChangeTask(self):
accumulate_probility = 0.0
compare_method = defined_sort_t.userDefinedSort()
self.not_none_task_list.sort(key = compare_method.return_task_change_probility, reverse = True)
#for i in range(len(self.not_none_task_list)):
# a = self.not_none_task_list
# print(a.change_probility)
if(len(self.none_task_list) > 0):
r1 = random.randint(0,len(self.none_task_list)-1)
change_task1 = self.none_task_list
else:
change_task1 = task_k.Task()
change_task1.setId(0)
for i in range(len(self.not_none_task_list)):
r = random.uniform(0,1)
select_probility = self.not_none_task_list.change_probility + accumulate_probility
if (select_probility > r):
change_task2 = self.not_none_task_list
break
else:
accumulate_probility += self.not_none_task_list.change_probility
return change_task1,change_task2
三个函数在一个类里面,self.solution是由Task()类组成的一个列表,先将self.solution再细分为两个列表self.not_none_list和self.none_lit,再从这两个列表中选择两个元素change_task1和change_task2,其余元素的属性均不变,before_num前面的None和chance_begin_time_k2 57707.988687是change_task1和change_task2的属性,check.returnTotalNumSuccessfulTask()的输出结果只和这个属性相关,可以看到明明self.solution没发生变化,before_num的输出结果却从19变成了20,百思不得其解,求大佬帮忙看一看 百思不得其解,求大佬帮忙看一看,me too MrPencil 发表于 2023-3-12 01:46
百思不得其解,求大佬帮忙看一看,me too
老哥, 我上面代码你看了吗 https://fishc.com.cn/forum-173-1.html
另:希望本版鱼油在提问的时间注意以下几点:
1. 提问尽量不要点将,例如: 小甲鱼来问答下...XX 来回答下... (影响他人回帖积极性)
2. 代码大于 20 行以上时,最好写清自己的思路和注释,这样回答的人才能尽快给大家答案!(避免扔上一大堆代码,说是有错误,请人指点!)
3. 发代码请务必使用编辑器的 “添加代码文字”(这个符号:<>)代码不会发请点击我
页:
[1]