python 遗传算法 最优化_利用Python实现基于模拟退火算法和遗传算法的最佳航班选择...

本文介绍了如何使用Python实现模拟退火算法和遗传算法来解决最佳航班选择问题。通过创建航班数据字典,利用数字序列描述解决方案,并构建成本函数进行优化。模拟退火算法和遗传算法分别进行优化,找到最小成本的航班组合。两种算法各有特点,模拟退火算法能较快找到近似最优解,遗传算法提供多样化的近似最优解选择。
摘要由CSDN通过智能技术生成

本文利用Python2.7实现了基于模拟退火算法和遗传算法这两种最优化算法的最佳航班选择。具体过程如下:首先使用字典收集航班数据,其次利用数字序列来描述题解,然后建立一个恰当的成本函数,接着利用模拟退火算法和遗传算法两种方法对其进行优化,找出具有最小代价函数值的方案,实现最佳航班的选择。

使用字典收集航班数据

为来自不同地方去往同一地点的人们制定组团航班计划,要求同一天到达且同一天离开,并且搭乘相同的交通工具往返机场,每位成员所在地均有许多航班供选择,航班的起飞时间、价格、续航时间均不同。先创建optimization.py文件,并加入以下代码:

import time

import random

import math

people = [('Seymour','BOS'),

('Franny','DAL'),

('Zooey','CAK'),

('Walt','MIA'),

('Buddy','ORD'),

('Les','OMA')]

# Laguardia

destination='LGA'1

2

3

4

5

6

7

8

9

10

11

12

下载航班样本数据,每行包括:起点、终点、起飞时间、到达时间、价格,格式如下:

LGA, MIA, 20:27, 23:42, 169

...1

2

将数据载入到以起止点为键,航班详情为值的字典中,代码实现:

flights={}

for line in file('schedule.txt'):

origin,dest,depart,arrive,price=line.strip().split(',')

flights.setdefault((origin,dest),[])

# Add details to the list of possible flights

flights[(origin,dest)].append((depart,arrive,int(price)))1

2

3

4

5

6

7

定义getminutes()函数计算某个给定时间在一天中的分钟数,以描述飞行时间和候机时间,代码实现:

def getminutes(t):

x=time.strptime(t,'%H:%M')

return x[3]*60+x[4]1

2

3

利用数字序列描述题解

利用数字序列的表达方式来描述题解,使得之后描述的优化算法不依赖于具体的问题。一个数字可代表某人选择乘坐的航班——0是这天的第一次航班,1是第二次,依次类推。由于每个人都需要往返两个航班,故列表的长度是人数的2倍。例如:[1, 4, 3, 2, 7, 3, 6, 3, 2, 4, 5, 3] 就代表了一种题解。

定义printschedule()函数,用于把人们决定搭乘的所有航班打印成表格,每行包括:人名、起点、往出发时间-到达时间、往航班票价、返出发时间-到达时间、返航班票价。

代码实现:

def printschedule(r):

for d in range(len(r)/2):

name=people[d][0]

origin=people[d][1]

out=flights[(origin,destination)][int(r[2*d])]

ret=flights[(destination,origin)][int(r[2*d+1])]

print '%10s%10s %5s-%5s $%3s %5s-%5s $%3s' % (name,origin, out[0],out[1],out[2], ret[0],ret[1],ret[2])1

2

3

4

5

6

7

建立成本函数

确定影响变量

选择价格、旅行时间(每个人在飞机上花费的总时间)、等待时间(在机场等待其他成员的时间)、出发时间、汽车租用时间(归还汽车时间必须早于租车时间,否则需要多付一天的租金)作为对成本影响的变量。

构造成本函数

根据这些变量构造一个成本函数,以描述方案的好坏。一个方案越好它的成本函数值就越小。代码实现:

def schedulecost(sol):

totalprice=0

latestarrival=0

earliestdep=24*60

for d in range(len(sol)/2):

# Get the inbound and outbound flights

origin=people[d][1]

outbound=flights[(origin,destination)][int(sol[2*d])]

returnf=flights[(destination,origin)][int(sol[2*d+1])]

# Total price is the price of all outbound and return flights

totalprice+=outbound[2]

totalprice+=returnf[2]

# Track the latest arrival and earliest departure

if latestarrival

if earliestdep>getminutes(returnf[0]): earliestdep=getminutes(returnf[0])

# Every person must wait at the airport until the latest person arrives.

# They also must arrive at the same time and wait for their flights.

totalwait=0

for d in range(len(sol)/2):

origin=people[d][1]

outbound=flights[(origin,destination)][int(sol[2*d])]

returnf=flights[(destination,origin)][int(sol[2*d+1])]

totalwait+=latestarrival-getminutes(outbound[1])

totalwait+=getminutes(returnf[0])-earliestdep

# Does this solution require an extra day of car rental? That'll be $50!

if latestarrival

return totalprice+totalwait1

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

模型优化与求解

以下分别基于模拟退火算法和遗传算法两种优化方法,找出具有最小代价函数值的方案,实现优化过程,完成最佳航班的选择。

模拟退火算法

1.算法原理

退火算法以一个问题的随机解开始,用一个变量表示温度,这一温度开始时非常高,而后逐步降低在每一次迭代期间,算法会随机选中题解中的某个数字,然后朝某个方向细微变化。如果新的成本值更低,则新的题解将会变成当前题解,这与爬山法类似。不过,如果成本值更高的话,则新的题解仍有可能成为当前题解,这是避免局部极小值问题的一种尝试。

算法总会接受一个更优的解,而且在退火的开始阶段会接受较差的解,随着退火的不断进行,算法原来越不能接受较差的解,直到最后,它只能接受更优的解。

算法接受较差解的概率 P=e−highcost−lowcosttemperatureP=e−highcost−lowcosttemperature

2.算法流程

1) 得到一组随机解;

2) 不断循环,直到温度降到最低(这里是小于0.1);

3) 每次循环中,随机选择一个方向(列表中任意一个位置的索引值),朝某一个方向(正、负)变化一个值(步长正负值范围内的一个随机值);

4) 计算变化前后的成本,如果成本减少、或者一个随机值小于pow(math.e, -(eb-ea)/T)(避免局部最优),则将变化后的列表作为新的列表;

5) 降低温度,回到2;

6) 循环结束,返回最后得到的最优解。

3.相关参数

Args:

domain:解中每个值的取值范围

costfunc:计算成本的函数

T:初始温度

cool:退火速度

step:每次变化的步长

Returns:

vec:退火之后的最优解1

2

3

4

5

6

7

8

4.算法实现

def annealingoptimize(domain,costf,T=10000.0,cool=0.95,step=1):

# Initialize the values randomly

vec=[float(random.randint(domain[i][0],domain[i][1]))

for i in range(len(domain))]

while T>0.1:

# Choose one of the indices

i=random.randint(0,len(domain)-1)

# Choose a direction to change it

dir=random.randint(-step,step)

# Create a new list with one of the values changed

vecb=vec[:]

vecb[i]+=dir

if vecb[i]

elif vecb[i]>domain[i][1]: vecb[i]=domain[i][1]

# Calculate the current cost and the new cost

ea=costf(vec)

eb=costf(vecb)

p=pow(math.e,-(eb-ea)/T)

# Is it better, or does it make the probability

# cutoff?

if (eb

vec=vecb

# Decrease the temperature

T=T*cool

return vec1

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

5.优化求解

>>> import optimization

>>> domain=[(0,9)]*(len(optimization.people)*2)

>>> s=optimization.annealingoptimize(domain,optimization.schedulecost)

>>> optimization.schedulecost(s)

3404

>>> s

[7.0, 1.0, 5.0, 2.0, 7.0, 3.0, 6.0, 1, 6.0, 2.0, 4.0, 6.0]

>>> optimization.printschedule(s)

Seymour BOS 17:11-18:30 $108 8:23-10:28 $149

Franny DAL 13:54-18:02 $294 9:49-13:51 $229

Zooey CAK 17:08-19:08 $262 10:32-13:16 $139

Walt MIA 15:34-18:11 $326 8:23-11:07 $143

Buddy ORD 15:58-18:40 $173 9:11-10:42 $172

Les OMA 12:18-14:56 $172 15:07-17:21 $1291

2

3

4

5

6

7

8

9

10

11

12

13

14

15

遗传算法

1.算法原理

首先随机生成一组解,我们称之为种群,在优化过程的每一步,算法会计算整个种群的成本函数,从而得到一个有关题解的有序列表。随后根据种群构造进化的下一代种群,方法如下:

1) 遗传:从当前种群中选出代价最优的一部分,传入下一代种群;

2) 变异:从题解中随机选取一个数字,对其进行微小的,简单的改变;

3) 交叉:选取最优解中的两个解,将他们按照某种方式进行结合。

2.算法流程

1) 随机生成popsize个解组成的列表,作为刚开始的种群;

2) 循环maxiter次,遗传maxiter代;

3) 每次循环中,计算出种群中所有解的成本;

4) 将种群内的解按成本大小排序;

5) 取最优的(成本最小的)一些(elite*popsize决定有多少个可以胜出)胜出者作为新的种群;

6) 从新种群中随机选择一些进行变异或者交叉(修改题解的两种方法)之后添加到新种群,将新种群补齐为popsize个;

7) 回到3;

8) 循环结束后返回最后种群中的最优解。

3.相关参数

Args:

popsize:种群大小

step:每次变异大小

mutprob:新种群由变异得来的概率

elite:种群中是优解,且被允许遗传给下一代的部分

maxiter:需运行多少代

Returns:

遗传maxiter代之后的最优解1

2

3

4

5

6

7

8

4.算法实现

def geneticoptimize(domain,costf,popsize=50,step=1,

mutprob=0.2,elite=0.2,maxiter=100):

# Mutation Operation

def mutate(vec):

i=random.randint(0,len(domain)-1)

if random.random()<0.5 and vec[i]>domain[i][0]:

return vec[0:i]+[vec[i]-step]+vec[i+1:]

elif vec[i]

return vec[0:i]+[vec[i]+step]+vec[i+1:]

else:

return vec

# Crossover Operation

def crossover(r1,r2):

i=random.randint(1,len(domain)-2)

return r1[0:i]+r2[i:]

# Build the initial population

pop=[]

for i in range(popsize):

vec=[random.randint(domain[i][0],domain[i][1])

for i in range(len(domain))]

pop.append(vec)

# How many winners from each generation?

topelite=int(elite*popsize)

# Main loop

for i in range(maxiter):

#print pop

scores=[(costf(v),v) for v in pop]

scores.sort()

ranked=[v for (s,v) in scores]

# Start with the pure winners

pop=ranked[0:topelite]

# Add mutated and bred forms of the winners

while len(pop)

if random.random()

# Mutation

c=random.randint(0,topelite)

pop.append(mutate(ranked[c]))

else:

# Crossover

c1=random.randint(0,topelite)

c2=random.randint(0,topelite)

pop.append(crossover(ranked[c1],ranked[c2]))

# Print current best score

print scores[0][0]

return scores[0][1]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

5.优化求解

>>> import optimization

>>> domain=[(0,9)]*(len(optimization.people)*2)

>>> s=optimization.annealingoptimize(domain,optimization.schedulecost)

>>> optimization.schedulecost(s)

3284

>>> optimization.printschedule(s)

Seymour BOS 17:11-18:30 $108 10:33-12:03 $ 74

Franny DAL 13:54-18:02 $294 10:51-14:16 $256

Zooey CAK 15:23-17:25 $232 13:37-15:33 $142

Walt MIA 15:34-18:11 $326 12:37-15:05 $170

Buddy ORD 15:58-18:40 $173 10:33-13:11 $132

Les OMA 9:15-12:03 $ 99 15:07-17:21 $129

>>> import optimization

>>> domain=[(0,9)]*(len(optimization.people)*2)

>>> s=optimization.geneticoptimize(domain,optimization.schedulecost)

4838

4291

...

3007

3007

3007

>>> s

[4, 1, 0, 2, 4, 5, 3, 1, 4, 1, 2, 1]

>>> optimization.printschedule(s)

Seymour BOS 12:34-15:02 $109 8:23-10:28 $149

Franny DAL 6:12-10:22 $230 9:49-13:51 $229

Zooey CAK 12:08-14:59 $149 13:37-15:33 $142

Walt MIA 11:28-14:40 $248 8:23-11:07 $143

Buddy ORD 12:44-14:17 $134 7:50-10:08 $164

Les OMA 9:15-12:03 $ 99 8:04-10:59 $1361

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

两种优化算法的评价

模拟退火算法是一种随机算法,可以得到最好的解但速度比较受影响,也可比较快的找到问题的近似最优解。偶尔会得到一个较差结果,建议使用不同的参数(初始温度,冷却率,step的值等)多做试验。

遗传算法也是一种随机算法,可能一直得不到最好的解,易陷入局部最优,且效率比较低。但不依赖于具体问题,可得到广阔的近似最优解空间,使解的选择性更多。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值