deap实战_2017中国数学建模大赛_B题_第二题
生活随笔
收集整理的這篇文章主要介紹了
deap实战_2017中国数学建模大赛_B题_第二题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡介
補充:本例子僅僅是之前deap類庫的一個實戰例子,所以先別問我數學建模的事情,我暫時不想回答(還有為毛踩我文章…..我本來就不是寫數學建模的……╮(╯▽╰)╭)(2017/10/31)
原問題是給出一個定價策略,證明其相較于原來定價策略的優點.
那么首先我們第一題第二問得到了一個 價格-完成率 函數,此時我們需要的是給出一個新的定價函數,并利用遺傳算法得到最佳參數.
思路
Types
import randomfrom deap import base from deap import creator from deap import toolsimport timeThresholdValue = 28.6670026583creator.create("FitnessMax", base.Fitness, weights=(1.0,)) # 定義最大化適應度 creator.create("Individual", list, fitness=creator.FitnessMax) # 這里的list種群的數據類型toolbox = base.Toolbox()# Attribute generator: define 'attr_bool' to be an attribute ('gene') # which corresponds to integers sampled uniformly # from the range [0,1] (i.e. 0 or 1 with equal # probability)toolbox.register("attr_bool", random.random) # 包含了0,1的隨機整數,初始化種群# Structure initializers: define 'individual' to be an individual # consisting of 100 'attr_bool' elements ('genes') toolbox.register("individual", tools.initRepeat, creator.Individual,toolbox.attr_bool, 5)# define the population to be a list of 'individual's toolbox.register("population", tools.initRepeat, list, toolbox.individual)ReadData
讀取數據,為之后的計算做準備
import pandas as pddf2 = pd.read_csv('/home/fonttian/Data/MM2017/db.csv')npones = df2['npones'] level = df2['level'] length = df2['length'] MiDu = df2['MiDu'] npjt = df2['npjt'] listlen = df2['listlen'] price = df2['price']定義評價函數(適應度函數)
def evalOneMax(individual):newPrice = (individual[0] - 0.5) * npones * 20 + (individual[1] - 0.5) * length * 20 + (individual[2] - 0.5) * MiDu * 20 + (individual[3] - 0.5) * level * 20 + (individual[4]) * 100 +listlen * 65.7# npones,nplength,npMiDuList3w2 = [-0.01633732, 0.83539635, -0.06544261, -0.00280863]xjb = length * w2[0] + level * w2[1] + MiDu * w2[2] + npjt * w2[3]xjb = xjb * newPricesums = 0for i in range(len(xjb)):# yuzhi = 28.6670026583# if xjb[i] >= yuzhi and newPrice[i] <= price[i] *1.1: # 608# if xjb[i] >= yuzhi: # 655# yuzhi = 0.474373718686if xjb[i] >= ThresholdValue and sum(newPrice) <= 57707.5 * 1.3: # 655sums += 1else:passreturn sums, # 注意最后的 , 該文件中必須要有,不然會報錯注冊其他參數(Operator registration)
# ---------------------Operator registration--------------------- toolbox.register("evaluate", evalOneMax) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3)設計運行程序,獲取我們想要的結果
def main():random.seed(64)# hash(64)is used# random.seed方法的作用是給隨機數對象一個種子值,用于產生隨機序列。# 對于同一個種子值的輸入,之后產生的隨機數序列也一樣。# 通常是把時間秒數等變化值作為種子值,達到每次運行產生的隨機系列都不一樣# create an initial population of 300 individuals (where# each individual is a list of integers)pop = toolbox.population(n=300) # 定義300個個體的種群# CXPB is the probability with which two individuals# are crossed## MUTPB is the probability for mutating an individual## NGEN is the number of generations for which the# evolution runs 迭代次數CXPB, MUTPB, NGEN = 0.5, 0.2, 100print("Start of evolution")# Evaluate the entire populationfitnesses = list(map(toolbox.evaluate, pop))# for ind, fit in zip(pop, fitnesses):# ind.fitness.values = fitprint(" Evaluated %i individuals" % len(pop)) # 這時候,pop的長度還是300呢print(" 迭代 %i 次" % NGEN)t1 = time.clock()# Begin the evolution 開始進化了哈!!!注意注意注意!就是一個for循環里了!NGEN次--代數for g in range(NGEN):if g % 10 == 0:print("-- Generation %i --" % g)# Select the next generation individualsoffspring = toolbox.select(pop, len(pop))# Clone the selected individualsoffspring = list(map(toolbox.clone, offspring))# Apply crossover and mutation on the offspringfor child1, child2 in zip(offspring[::2], offspring[1::2]):# cross two individuals with probability CXPBif random.random() < CXPB:toolbox.mate(child1, child2)# fitness values of the children# must be recalculated laterdel child1.fitness.valuesdel child2.fitness.valuesfor mutant in offspring:# mutate an individual with probability MUTPBif random.random() < MUTPB:toolbox.mutate(mutant)del mutant.fitness.values# Evaluate the individuals with an invalid fitnessinvalid_ind = [ind for ind in offspring if not ind.fitness.valid]fitnesses = map(toolbox.evaluate, invalid_ind)for ind, fit in zip(invalid_ind, fitnesses):ind.fitness.values = fit# print(" Evaluated %i individuals" % len(invalid_ind))# The population is entirely replaced by the offspringpop[:] = offspring# Gather all the fitnesses in one list and print the statsfits = [ind.fitness.values[0] for ind in pop]length = len(pop)mean = sum(fits) / lengthsum2 = sum(x * x for x in fits)std = abs(sum2 / length - mean ** 2) ** 0.5# print(" Min %s" % min(fits))# print(" Max %s" % max(fits))# print(" Avg %s" % mean)# print(" Std %s" % std)print("-- End of (successful) evolution --")best_ind = tools.selBest(pop, 1)[0]print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))print('預測數據')# PevalOneMax([0.6222847026584997, 0.9952779203368345, 0.10901692485431957, 0.8966275594961192, 0.9692993203252058])print('該次遺傳算法的出的最好的參數的通過數:')PevalOneMax(best_ind)print('出題方給的定價規律的預測通過數',TevalOneMax())t2 = time.clock()print(t2 - t1)全部代碼
# - * - coding: utf - 8 -*- # 作者:田豐(FontTian) # 創建時間:'2017/9/17' # This file is part of DEAP. # # DEAP is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation, either version 3 of # the License, or (at your option) any later version. # # DEAP is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with DEAP. If not, see <http://www.gnu.org/licenses/>.# example which maximizes the sum of a list of integers # each of which can be 0 or 1import randomfrom deap import base from deap import creator from deap import toolsimport timeThresholdValue = 28.6670026583creator.create("FitnessMax", base.Fitness, weights=(1.0,)) # 定義最大化適應度 creator.create("Individual", list, fitness=creator.FitnessMax) # 這里的list種群的數據類型toolbox = base.Toolbox()# Attribute generator: define 'attr_bool' to be an attribute ('gene') # which corresponds to integers sampled uniformly # from the range [0,1] (i.e. 0 or 1 with equal # probability)toolbox.register("attr_bool", random.random) # 包含了0,1的隨機整數,初始化種群# Structure initializers: define 'individual' to be an individual # consisting of 100 'attr_bool' elements ('genes') toolbox.register("individual", tools.initRepeat, creator.Individual,toolbox.attr_bool, 5)# define the population to be a list of 'individual's toolbox.register("population", tools.initRepeat, list, toolbox.individual)import pandas as pddf2 = pd.read_csv('/home/fonttian/Data/MM2017/db.csv')npones = df2['npones'] level = df2['level'] length = df2['length'] MiDu = df2['MiDu'] npjt = df2['npjt'] listlen = df2['listlen'] price = df2['price']def evalOneMax(individual):newPrice = (individual[0] - 0.5) * npones * 20 + (individual[1] - 0.5) * length * 20 + (individual[2] - 0.5) * MiDu * 20 + (individual[3] - 0.5) * level * 20 + (individual[4]) * 100 +listlen * 65.7# npones,nplength,npMiDuList3w2 = [-0.01633732, 0.83539635, -0.06544261, -0.00280863]xjb = length * w2[0] + level * w2[1] + MiDu * w2[2] + npjt * w2[3]xjb = xjb * newPricesums = 0for i in range(len(xjb)):# yuzhi = 28.6670026583# if xjb[i] >= yuzhi and newPrice[i] <= price[i] *1.1: # 608# if xjb[i] >= yuzhi: # 655# yuzhi = 0.474373718686if xjb[i] >= ThresholdValue and sum(newPrice) <= 57707.5 * 1.3: # 655sums += 1else:passreturn sums,def PevalOneMax(individual):print((individual[0] - 0.5) * 20, (individual[1] - 0.5) * 20, (individual[2] - 0.5) * 20, (individual[3] - 0.5) * 20, (individual[4]) * 100)newPrice = (individual[0] - 0.5) * npones * 20 + (individual[1] - 0.5) * length * 20 + (individual[2] - 0.5) * MiDu * 20 + (individual[3] - 0.5) * level * 20 + (individual[4]) * 100w2 = [-0.01633732, 0.83539635, -0.06544261, -0.00280863]xjb = length * w2[0] + level * w2[1] + MiDu * w2[2] + npjt * w2[3]xjb = xjb * newPricesums = 0for i in range(len(xjb)):if xjb[i] >= ThresholdValue:sums += 1else:passprint(sums)print("新的總價:",sum(newPrice),'舊的總價:',sum(price))return sums,# ---------------------Operator registration--------------------- toolbox.register("evaluate", evalOneMax) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3)def TevalOneMax():# 原定價模型的通過率w2 = [-0.01633732, 0.83539635, -0.06544261, -0.00280863]xjb = length * w2[0] + level * w2[1] + MiDu * w2[2] + npjt * w2[3]xjb = xjb * pricesum = 0for i in range(len(xjb)):if xjb[i] >= ThresholdValue:sum += 1else:passreturn sum# --------------------- main ---------------------def main():random.seed(64)# hash(64)is used# random.seed方法的作用是給隨機數對象一個種子值,用于產生隨機序列。# 對于同一個種子值的輸入,之后產生的隨機數序列也一樣。# 通常是把時間秒數等變化值作為種子值,達到每次運行產生的隨機系列都不一樣# create an initial population of 300 individuals (where# each individual is a list of integers)pop = toolbox.population(n=300) # 定義300個個體的種群# CXPB is the probability with which two individuals# are crossed## MUTPB is the probability for mutating an individual## NGEN is the number of generations for which the# evolution runs 迭代次數CXPB, MUTPB, NGEN = 0.5, 0.2, 100print("Start of evolution")# Evaluate the entire populationfitnesses = list(map(toolbox.evaluate, pop))# for ind, fit in zip(pop, fitnesses):# ind.fitness.values = fitprint(" Evaluated %i individuals" % len(pop)) # 這時候,pop的長度還是300呢print(" 迭代 %i 次" % NGEN)t1 = time.clock()# Begin the evolution 開始進化了哈!!!注意注意注意!就是一個for循環里了!NGEN次--代數for g in range(NGEN):if g % 10 == 0:print("-- Generation %i --" % g)# Select the next generation individualsoffspring = toolbox.select(pop, len(pop))# Clone the selected individualsoffspring = list(map(toolbox.clone, offspring))# Apply crossover and mutation on the offspringfor child1, child2 in zip(offspring[::2], offspring[1::2]):# cross two individuals with probability CXPBif random.random() < CXPB:toolbox.mate(child1, child2)# fitness values of the children# must be recalculated laterdel child1.fitness.valuesdel child2.fitness.valuesfor mutant in offspring:# mutate an individual with probability MUTPBif random.random() < MUTPB:toolbox.mutate(mutant)del mutant.fitness.values# Evaluate the individuals with an invalid fitnessinvalid_ind = [ind for ind in offspring if not ind.fitness.valid]fitnesses = map(toolbox.evaluate, invalid_ind)for ind, fit in zip(invalid_ind, fitnesses):ind.fitness.values = fit# print(" Evaluated %i individuals" % len(invalid_ind))# The population is entirely replaced by the offspringpop[:] = offspring# Gather all the fitnesses in one list and print the statsfits = [ind.fitness.values[0] for ind in pop]length = len(pop)mean = sum(fits) / lengthsum2 = sum(x * x for x in fits)std = abs(sum2 / length - mean ** 2) ** 0.5# print(" Min %s" % min(fits))# print(" Max %s" % max(fits))# print(" Avg %s" % mean)# print(" Std %s" % std)print("-- End of (successful) evolution --")best_ind = tools.selBest(pop, 1)[0]print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))print('預測數據')# PevalOneMax([0.6222847026584997, 0.9952779203368345, 0.10901692485431957, 0.8966275594961192, 0.9692993203252058])print('該次遺傳算法的出的最好的參數的通過數:')PevalOneMax(best_ind)print('出題方給的定價規律的預測通過數',TevalOneMax())t2 = time.clock()print(t2 - t1)if __name__ == "__main__":# t1 = time.clock()main()# t2 = time.clock()# print(t2-t1)下載數據文件
鏈接: https://pan.baidu.com/s/1gfcTzpx 密碼: dibq
總結
以上是生活随笔為你收集整理的deap实战_2017中国数学建模大赛_B题_第二题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Deap : 遗传算法算法解决 背包问题
- 下一篇: 9.1 mnist_softmax 交叉