集體智慧編程(四)優化

博客地址:http://andyheart.me

如果有任何錯誤,請私信或者評論,多謝。

本章主要介紹了幾種常用的優化算法,優化技術擅長處理:受多種變量影響,存在多種可能解的問題。

優化算法的關鍵在於找到成本函數

涉及到的算法如下:

  1. 隨機搜索
  2. 爬山法(隨機重複爬山法)
  3. 模擬退火算法
  4. 遺傳算法

組團旅遊

本章從一個組團旅遊的問題引出。

描述:來自美國各地的家庭成員要在同一天乘坐飛機到達同一個地方,並且在同一天離開,設計一個合理的方案。

分析:在實現過程中,首先應該知道成員名稱以及對應的地點;其次應該掌握航班信息。

相應的Python代碼如下:

import time
import random
import math

people = [('Seymour','BOS'),
          ('Franny','DAL'),
          ('Zooey','CAK'),
          ('Walt','MIA'),
          ('Buddy','ORD'),
          ('Les','OMA')]
# Laguardia
destination='LGA'

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)))

def getminutes(t):
  x=time.strptime(t,'%H:%M')
  return x[3]*60+x[4]

def printschedule(r):
  for d in range(len(r)/2):
    name=people[d][0]
    origin=people[d][1]
    out=flights[(origin,destination)][int(r[d])]
    ret=flights[(destination,origin)][int(r[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. 汽車租用時間(不懂)

在找到影響成本函數的因素之後,我們就需要找到辦法將他們組合在一起形成一個值(應該爲一個函數對應的值),例如在本例中,我們可以假定在飛機上的飛行時間每一分鐘價值1美元,在機場等待的時間每一分鐘等於0.5美元,這樣,問題的成本函數就會輕易的用一個值來代替。

在代碼中加入如下函數:

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[d])]
    returnf=flights[(destination,origin)][int(sol[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<getminutes(outbound[1]): latestarrival=getminutes(outbound[1])
    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[d])]
    returnf=flights[(destination,origin)][int(sol[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>earliestdep: totalprice+=50

  return totalprice+totalwait

在建立了成本函數以後,我們的目標就是需要對函數的值進行優化從而達到最小值。

優化算法

優化算法主要解決成本函數確定的情況下儘量得到最小值的問題。

隨機搜索

顧名思義,隨機搜索就是一種隨機嘗試的方法,在實現過程中隨機的產生一定數量的解,並且對這些解一一進行成本值的計算,取最小值。Python代碼如下:

def randomoptimize(domain,costf):
  best=999999999
  bestr=None
  for i in range(0,1000):
    # Create a random solution
    r=[float(random.randint(domain[i][0],domain[i][1])) 
       for i in range(len(domain))]

    # Get the cost
    cost=costf(r)

    # Compare it to the best one so far
    if cost<best:
      best=cost
      bestr=r 
  return r

爬山法(隨機重複爬山法)

爬山法從一個隨機解開始,然後在其鄰近的解集中尋找更好的題解(具有更低的成本),從而找到局部最小值,作爲最優解。算法容易找到局部最優解,而不是全局最優解。解決這個問題的辦法可以使用隨機重複爬山法,即讓爬山法以多個隨機生成的初始解爲起點運行多次,藉此希望找到一個全局最優解。Python代碼如下:

def hillclimb(domain,costf):
  # 創建一個隨機解
  sol=[random.randint(domain[i][0],domain[i][1])
      for i in range(len(domain))]
  # 主循環
  while 1:
    # 創建一個相鄰解的列表
    neighbors=[]

    for j in range(len(domain)):
      # 在每個方向上相對於原值偏離一點點
      if sol[j]>domain[j][0]:
        neighbors.append(sol[0:j]+[sol[j]+1]+sol[j+1:])
      if sol[j]<domain[j][1]:
        neighbors.append(sol[0:j]+[sol[j]-1]+sol[j+1:])

    # 在相鄰解中尋找最優解
    current=costf(sol)
    best=current
    for j in range(len(neighbors)):
      cost=costf(neighbors[j])
      if cost<best:
        best=cost
        sol=neighbors[j]

    # 如果沒有最優解,退出循環
    if best==current:
      break
  return sol

模擬退火算法

原理:從某一個隨機解開始,用某一個變量代表溫度,開始時非常高,爾後逐漸變低,每一次迭代期間,算法會隨機選中題解中的某個數字,然後朝着某個方向變化。該算法關鍵在於,如果新的成本更低,則新的題解稱爲當前題解,如果新的成本更高,新的題解仍可能稱爲當前題解,這是避免局部最優的嘗試。

開始階段,算法接受差的題解能力較強,隨着算法的深入,越來越不能接受差的題解。其被接受的概率由一下公式得到:

P=e((highcostlowcost)/temperature)

因此,該算法在示例中的Python代碼如下:

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]<domain[i][0]: vecb[i]=domain[i][0]
    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<ea or random.random()<p):
      vec=vecb      

    # Decrease the temperature
    T=T*cool
  return vec

遺傳算法

原理:首先,算法隨機生成一組解(稱爲種羣),從中選取成本函數最低的解(精英選拔法),然後修改題解,生成新的種羣,修改題解方法有兩種,變異和交叉(配對)。變異就是隨機改變題解某一個特徵的值。交叉就是兩個題解的特徵值進行交叉 。代碼生成可以如下:

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]<domain[i][1]:
      return vec[0:i]+[vec[i]+step]+vec[i+1:]

  # 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):
    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)<popsize:
      if random.random()<mutprob:

        # 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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章