1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| def check_tabu(tabu_list,mid_result): if len(tabu_list)<1: return False for i in range(len(tabu_list)): if (tabu_list[i][0]==mid_result).all(): return True return False def multiple_knapsack_tabu(bags_set=[], goods_set=[],tabu_length=1): bagsWeightList=[bag.original_capacity for bag in bags_set] bagsWeightList=np.array(bagsWeightList) goodsWeightMat=np.zeros(shape=(len(goods_set),len(bags_set))) goodsValueMat=np.zeros(shape=(len(goods_set),len(bags_set))) for i in range(len(goods_set)): for j in range(len(bags_set)): goodsWeightMat[i][j]=goods_set[i].weight for i in range(len(goods_set)): for j in range(len(bags_set)): goodsValueMat[i][j]=goods_set[i].value all_zero_result = np.zeros(shape=(len(goods_set),len(bags_set))) current_result,current_total_value = np.zeros(shape=(len(goods_set),len(bags_set))),0 best_result,best_total_value =current_result.copy(),current_total_value iterateNum,isEnd,tabu_list=1,False,[] if tabu_length>=1: tabu_list.append((current_result.copy(),current_total_value)) print("--------------------------------") print("the initial total value is: {}".format(current_total_value)) print("the initial putting matrix is:") print(current_result) while not isEnd and iterateNum<15: next_result,next_total_value = np.zeros(shape=(len(goods_set),len(bags_set))),0 allZeroIndexList,haveOneIndexList=[],[] if len(haveOneIndexList)==len(goods_set): break for i in range(len(goods_set)): if np.sum(current_result[i:i+1,::],axis=1)[0]==0: allZeroIndexList.append(i) else: haveOneIndexList.append(i) if len(haveOneIndexList)==0: for i in allZeroIndexList: for j in range(len(bags_set)): mid_result=current_result.copy() mid_result[i][j]=1 if judgeWeight(mid_result,bagsWeightList,goodsWeightMat): mid_total_value=calValue(mid_result,goodsValueMat) if mid_total_value>next_total_value and not check_tabu(tabu_list,mid_result): next_result = mid_result next_total_value = mid_total_value break elif len(haveOneIndexList)==1: for i in allZeroIndexList: for j in range(len(bags_set)): mid_result=current_result.copy() mid_result[i][j]=1 if judgeWeight(mid_result,bagsWeightList,goodsWeightMat): mid_total_value=calValue(mid_result,goodsValueMat) if mid_total_value>next_total_value and not check_tabu(tabu_list,mid_result): next_result = mid_result next_total_value = mid_total_value break for i in allZeroIndexList: for j in haveOneIndexList: mid_result=current_result.copy() mid_result[j,::]=current_result[i,::] mid_result[i,::]=current_result[j,::] if judgeWeight(mid_result,bagsWeightList,goodsWeightMat): mid_total_value=calValue(mid_result,goodsValueMat) if mid_total_value>next_total_value and not check_tabu(tabu_list,mid_result): next_result = mid_result next_total_value = mid_total_value else: for i in allZeroIndexList: for j in range(len(bags_set)): mid_result=current_result.copy() mid_result[i][j]=1 if judgeWeight(mid_result,bagsWeightList,goodsWeightMat): mid_total_value=calValue(mid_result,goodsValueMat) if mid_total_value>next_total_value and not check_tabu(tabu_list,mid_result): next_result = mid_result next_total_value = mid_total_value break for i in allZeroIndexList: for j in haveOneIndexList: mid_result=current_result.copy() mid_result[j,::]=current_result[i,::] mid_result[i,::]=current_result[j,::] if judgeWeight(mid_result,bagsWeightList,goodsWeightMat): mid_total_value=calValue(mid_result,goodsValueMat) if mid_total_value>next_total_value and not check_tabu(tabu_list,mid_result): next_result = mid_result next_total_value = mid_total_value for i in range(len(haveOneIndexList)): for j in range(i,len(haveOneIndexList)): index1=haveOneIndexList[i] index2=haveOneIndexList[j] for k in allZeroIndexList: for case in range(2): if(case==1): index1,index2=index2,index1 mid_result=current_result.copy() mid_result[k,::]=current_result[index1,::] mid_result[index1,::]=current_result[index2,::] mid_result[index2,::]=current_result[k,::] if judgeWeight(mid_result,bagsWeightList,goodsWeightMat): mid_total_value=calValue(mid_result,goodsValueMat) if mid_total_value>next_total_value and not check_tabu(tabu_list,mid_result): next_result = mid_result next_total_value = mid_total_value if (next_result == all_zero_result).all(): isEnd = True else: if tabu_length>=1: if len(tabu_list)>=tabu_length: tabu_list.remove(tabu_list[0]) tabu_list.append((current_result,current_total_value)) else: tabu_list.append((current_result,current_total_value))
current_total_value=next_total_value current_result=next_result if current_total_value>best_total_value: best_result=current_result.copy() best_total_value=current_total_value print("--------------------------------") print("iteration {}:".format(iterateNum)) print("the current total value is: {}".format(current_total_value)) print("the current putting matrix is:") print(current_result) iterateNum+=1 print("--------------------------------") print("--------------------------------") print("--------------------------------") print("The final result is:") print("the finial total value is: {}".format(best_total_value)) print("the finial puttting matrix is:") print(best_result) print() for i in range(len(bags_set)): print("bag{} contains:".format(i+1)) for j in range(len(goods_set)): if best_result[j][i]==1: print(" good{} has weight of {} and value of {}".format(j+1,goods_set[j].weight,goods_set[j].value)) return best_result,best_total_value
|