航班优化算法

航班优化算法源代码以及数据
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])

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

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

def hillclimb(domain,costf):
# Create a random solution
sol=random.randint(domain[i,domaini)

  for i in range(len(domain))]

# Main loop
while 1:

# Create list of neighboring solutions
neighbors=[]

for j in range(len(domain)):
  # One away in each direction
  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:])

# See what the best solution amongst the neighbors is
current=costf(sol)
best=current
for j in range(len(neighbors)):
  cost=costf(neighbors[j])
  if cost<best:
    best=cost
    sol=neighbors[j]

# If there's no improvement, then we've reached the top
if best==current:
  break

return sol

def annealingoptimize(domain,costf,T=10000.0,cool=0.95,step=1):
# Initialize the values randomly
vec=float(random.randint(domain[i,domaini))

   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 scores0

源数据
LGA,OMA,6:19,8:13,239
OMA,LGA,6:11,8:31,249
LGA,OMA,8:04,10:59,136
OMA,LGA,7:39,10:24,219
LGA,OMA,9:31,11:43,210
OMA,LGA,9:15,12:03,99
LGA,OMA,11:07,13:24,171
OMA,LGA,11:08,13:07,175
LGA,OMA,12:31,14:02,234
OMA,LGA,12:18,14:56,172
LGA,OMA,14:05,15:47,226
OMA,LGA,13:37,15:08,250
LGA,OMA,15:07,17:21,129
OMA,LGA,15:03,16:42,135
LGA,OMA,16:35,18:56,144
OMA,LGA,16:51,19:09,147
LGA,OMA,18:25,20:34,205
OMA,LGA,18:12,20:17,242
LGA,OMA,20:05,21:44,172
OMA,LGA,20:05,22:06,261
LGA,ORD,6:03,8:43,219
ORD,LGA,6:05,8:32,174
LGA,ORD,7:50,10:08,164
ORD,LGA,8:25,10:34,157
LGA,ORD,9:11,10:42,172
ORD,LGA,9:42,11:32,169
LGA,ORD,10:33,13:11,132
ORD,LGA,11:01,12:39,260
LGA,ORD,12:08,14:47,231
ORD,LGA,12:44,14:17,134
LGA,ORD,14:19,17:09,190
ORD,LGA,14:22,16:32,126
LGA,ORD,15:04,17:23,189
ORD,LGA,15:58,18:40,173
LGA,ORD,17:06,20:00,95
ORD,LGA,16:43,19:00,246
LGA,ORD,18:33,20:22,143
ORD,LGA,18:48,21:45,246
LGA,ORD,19:32,21:25,160
ORD,LGA,19:50,22:24,269
LGA,MIA,6:33,9:14,172
MIA,LGA,6:25,9:30,335
LGA,MIA,8:23,11:07,143
MIA,LGA,7:34,9:40,324
LGA,MIA,9:25,12:46,295
MIA,LGA,9:15,12:29,225
LGA,MIA,11:08,14:38,262
MIA,LGA,11:28,14:40,248
LGA,MIA,12:37,15:05,170
MIA,LGA,12:05,15:30,330
LGA,MIA,14:08,16:09,232
MIA,LGA,14:01,17:24,338
LGA,MIA,15:23,18:49,150
MIA,LGA,15:34,18:11,326
LGA,MIA,16:50,19:26,304
MIA,LGA,17:07,20:04,291
LGA,MIA,18:07,21:30,355
MIA,LGA,18:23,21:35,134
LGA,MIA,20:27,23:42,169
MIA,LGA,19:53,22:21,173
LGA,BOS,6:39,8:09,86
BOS,LGA,6:17,8:26,89
LGA,BOS,8:23,10:28,149
BOS,LGA,8:04,10:11,95
LGA,BOS,9:58,11:18,130
BOS,LGA,9:45,11:50,172
LGA,BOS,10:33,12:03,74
BOS,LGA,11:16,13:29,83
LGA,BOS,12:08,14:05,142
BOS,LGA,12:34,15:02,109
LGA,BOS,13:39,15:30,74
BOS,LGA,13:40,15:37,138
LGA,BOS,15:25,16:58,62
BOS,LGA,15:27,17:18,151
LGA,BOS,17:03,18:03,103
BOS,LGA,17:11,18:30,108
LGA,BOS,18:24,20:49,124
BOS,LGA,18:34,19:36,136
LGA,BOS,19:58,21:23,142
BOS,LGA,20:17,22:22,102
LGA,DAL,6:09,9:49,414
DAL,LGA,6:12,10:22,230
LGA,DAL,7:57,11:15,347
DAL,LGA,7:53,11:37,433
LGA,DAL,9:49,13:51,229
DAL,LGA,9:08,12:12,364
LGA,DAL,10:51,14:16,256
DAL,LGA,10:30,14:57,290
LGA,DAL,12:20,16:34,500
DAL,LGA,12:19,15:25,342
LGA,DAL,14:20,17:32,332
DAL,LGA,13:54,18:02,294
LGA,DAL,15:49,20:10,497
DAL,LGA,15:44,18:55,382
LGA,DAL,17:14,20:59,277
DAL,LGA,16:52,20:48,448
LGA,DAL,18:44,22:42,351
DAL,LGA,18:26,21:29,464
LGA,DAL,19:57,23:15,512
DAL,LGA,20:07,23:27,473
LGA,CAK,6:58,9:01,238
CAK,LGA,6:08,8:06,224
LGA,CAK,8:19,11:16,122
CAK,LGA,8:27,10:45,139
LGA,CAK,9:58,12:56,249
CAK,LGA,9:15,12:14,247
LGA,CAK,10:32,13:16,139
CAK,LGA,10:53,13:36,189
LGA,CAK,12:01,13:41,267
CAK,LGA,12:08,14:59,149
LGA,CAK,13:37,15:33,142
CAK,LGA,13:40,15:38,137
LGA,CAK,15:50,18:45,243
CAK,LGA,15:23,17:25,232
LGA,CAK,16:33,18:15,253
CAK,LGA,17:08,19:08,262
LGA,CAK,18:17,21:04,259
CAK,LGA,18:35,20:28,204
LGA,CAK,19:46,21:45,214
CAK,LGA,20:30,23:11,114

下载新闻数据
Obesity not a problem
0.689921777771 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.616521773806 ['with', 'your', 'weight', 'have', 'control', 'about']
0.594775751071 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

Fitness equipment
0.336438029037 ['with', 'your', 'weight', 'have', 'control', 'about']
0.0336830699618 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.0323861213375 ['that', 'much', 'does', 'exercise', 'this', 'morning']

1000 Atkins Recipes E-Book
2.056067447 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
1.81222264198 ['with', 'your', 'weight', 'have', 'control', 'about']
0.31319239108 ['quot', 'they', 'money', 'want', 'very', 'best']

saturday
7.46811621754 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.174282882652 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.00317828003493 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

Food & Exercise -- 10/13/2007
4.73555293191 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.937525542474 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.10240571114 ['with', 'your', 'weight', 'have', 'control', 'about']

Food & exercise -- 10/12/2007 (yesterday)
3.97594760235 ['food', 'home', 'then', 'exercise', 'morning', 'went']
1.01018312908 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.0198736807467 ['food', 'calories', 'than', 'easy', 'high', 'come']

Have you been enslaved and confused by the omniscience myth?
1.14697423243 ['with', 'your', 'weight', 'have', 'control', 'about']
0.548717665826 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.159098480615 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']

High or low fat food? Easy trick for figuring it out
9.98464450123 ['food', 'calories', 'than', 'easy', 'high', 'come']
4.04959173123 ['quot', 'they', 'money', 'want', 'very', 'best']
0.123588233146 ['fats', 'quot', 'this', 'good', 'about', 'like']

Absolutely Free People Search
1.39249472006 ['with', 'your', 'weight', 'have', 'control', 'about']
0.459779859548 ['fats', 'quot', 'this', 'good', 'about', 'like']
0.4371224863 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Friday
4.35052263015 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.609863992308 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
0.249760019695 ['food', 'calories', 'than', 'easy', 'high', 'come']

Food and Workout Log 10.11.07
4.76278425737 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
1.18573164731 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.559740845941 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

LIL / Biggie's October Bulletin - UK
1.6407957576 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.09371385364 ['with', 'your', 'weight', 'have', 'control', 'about']
0.0997065733116 ['food', 'home', 'then', 'exercise', 'morning', 'went']

How accurate are the calorie meters on gym equipment?
1.68385026718 ['with', 'your', 'weight', 'have', 'control', 'about']
1.24336224612 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.472039508303 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

diet-exercise thursday
5.62839188358 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
1.42876311885 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.451891791988 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

Fast Food Meat
3.96657604228 ['quot', 'they', 'money', 'want', 'very', 'best']
1.56912835469 ['with', 'your', 'weight', 'have', 'control', 'about']
0.945562729964 ['food', 'calories', 'than', 'easy', 'high', 'come']

Food & Exercise -- 10/11/2007
2.06565313343 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.915925841734 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.852089104271 ['food', 'calories', 'than', 'easy', 'high', 'come']

sleepy food/fitness thursday
5.29370213306 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.821758436298 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
0.373361576129 ['fats', 'quot', 'this', 'good', 'about', 'like']

6 Dollars! You Can't Lose!! AS SEEN ON OPRAH & 20/20
3.50452080121 ['quot', 'they', 'money', 'want', 'very', 'best']
1.48000806252 ['with', 'your', 'weight', 'have', 'control', 'about']
0.353120143386 ['that', 'much', 'does', 'exercise', 'this', 'morning']

Looking for mediterranean buffet restaurants in Toronto, Canada
0.766709189825 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.488536397538 ['with', 'your', 'weight', 'have', 'control', 'about']
0.305836578699 ['quot', 'they', 'money', 'want', 'very', 'best']

Food and Workout Log 10.10.07
5.10395750879 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.931990921746 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.0751093197335 ['with', 'your', 'weight', 'have', 'control', 'about']

Food and Workout Log 10.9.07
3.66128126402 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.924777033606 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.46368820747 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

Food & Exercise -- 10/10/2007
2.09636617791 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.777930860455 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.234412590473 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

rainy diet/exercise
2.42408643655 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.79759287175 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
1.44383382428 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Whatever happened to Kaleb?
1.62771768736 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.424792812054 ['with', 'your', 'weight', 'have', 'control', 'about']
3.00050522053e-008 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Food & Exercise -- 10/9/2007
2.67051008267 ['that', 'much', 'does', 'exercise', 'this', 'morning']
2.25685573791 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.962829471038 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

Way of getting my veggies
2.51102412355 ['quot', 'they', 'money', 'want', 'very', 'best']
1.82193456941 ['with', 'your', 'weight', 'have', 'control', 'about']
1.20974377068 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Food & Exercise -- 10/8/2007 (yesterday)
3.57035878288 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.581070745119 ['with', 'your', 'weight', 'have', 'control', 'about']
0.151621405217 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Oatmeal, cereal of choice.
3.41252863148 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.482857491594 ['with', 'your', 'weight', 'have', 'control', 'about']
0.21056938621 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Whatever happened to Dally?
0.38943120274 ['with', 'your', 'weight', 'have', 'control', 'about']
0.38027115946 ['quot', 'they', 'money', 'want', 'very', 'best']
0.176713051522 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']

More about the Chicago marathon
6.1620884463 ['quot', 'they', 'money', 'want', 'very', 'best']
0.268050785403 ['with', 'your', 'weight', 'have', 'control', 'about']
0.0210462038578 ['fats', 'quot', 'this', 'good', 'about', 'like']

Food and Workout Log 10.8.07
3.19119866786 ['food', 'calories', 'than', 'easy', 'high', 'come']
2.68113794132 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.31607800222 ['fats', 'quot', 'this', 'good', 'about', 'like']

diet/exercise 10/8
4.35583316205 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.56546955704 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
1.25839277593 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

I did'nt diet to get in shape for Trinidad's Carnival.....
5.9231935598 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.567204076047 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.0169687217709 ['with', 'your', 'weight', 'have', 'control', 'about']

I got in shape and took part in Trinidad Carnival!
1.02074036539 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.930265487859 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.266432773175 ['fats', 'quot', 'this', 'good', 'about', 'like']

THE ULTIMATE FAT-BURNING DAY
1.72598890408 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.528141703291 ['with', 'your', 'weight', 'have', 'control', 'about']
0.390073319858 ['fats', 'quot', 'this', 'good', 'about', 'like']

Control ur Weight
6.78756986407 ['with', 'your', 'weight', 'have', 'control', 'about']
0.000529137198612 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.00038074933869 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

BrainStimPro Binaural Brainwave Generator
0.533630276909 ['with', 'your', 'weight', 'have', 'control', 'about']
0.37841909077 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.116016288049 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Food & Exercise -- 10/7/2007
7.73926153154 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.470298707782 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.233105196286 ['food', 'calories', 'than', 'easy', 'high', 'come']

food/exercise Friday 10/7
4.69100441998 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.64398092185 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.689120996726 ['food', 'calories', 'than', 'easy', 'high', 'come']

Should we ban marathons?
2.44173145283 ['quot', 'they', 'money', 'want', 'very', 'best']
1.81373140989 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.3775418859 ['fats', 'quot', 'this', 'good', 'about', 'like']

Abstinence 3 (8 October to 24 October)
0.969974503706 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
0.912311154908 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.371633089984 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

Food & Exercise -- 10/6/2007 (yesterday)
2.63514100937 ['food', 'home', 'then', 'exercise', 'morning', 'went']
1.80605150884 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.426403502815 ['food', 'calories', 'than', 'easy', 'high', 'come']

Food and Workout Log 10.5.07
2.03340244602 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.321040122788 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.286990704435 ['food', 'calories', 'than', 'easy', 'high', 'come']

Food and Workout Log 10.4.07
2.32606586074 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
2.23872352546 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.991619356436 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']

Exercise
1.82773959677 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.05124941331 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.420038570854 ['with', 'your', 'weight', 'have', 'control', 'about']

food/exercise Friday 10/5
5.3332773133 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.381638768 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.590183487282 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']

Food & Exercise -- 10/5/2007
5.22083940456 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.29336324721 ['fats', 'quot', 'this', 'good', 'about', 'like']
0.279839860069 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

Fitness and wellness is here to help you, about fittnes, nutrition, health, everything is here...
1.47212290909 ['with', 'your', 'weight', 'have', 'control', 'about']
0.581092551305 ['fats', 'quot', 'this', 'good', 'about', 'like']
0.22366446507 ['that', 'much', 'does', 'exercise', 'this', 'morning']

Live healthy and disease free
1.33925118974 ['fats', 'quot', 'this', 'good', 'about', 'like']
0.735241239185 ['with', 'your', 'weight', 'have', 'control', 'about']
4.48445780778e-005 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

maintain ur diet dailyu
1.37867872087 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
7.75955547582e-008 ['with', 'your', 'weight', 'have', 'control', 'about']
8.66186281206e-016 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

Food & Exercise -- 10/4/2007
5.16310413391 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.108950865658 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.103411657525 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

diet/exercise 10/4
5.94642162786 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.15981737715 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.0648977104196 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']

sad to say
2.0658103969 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.0211752756 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
0.606678422181 ['food', 'home', 'then', 'exercise', 'morning', 'went']

Food and Workout Log 10.3.07
5.48488799917 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
1.04086527858 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.86250634261 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Walking As Exercise
2.49352373509 ['quot', 'they', 'money', 'want', 'very', 'best']
2.18075570265 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.613516861795 ['with', 'your', 'weight', 'have', 'control', 'about']

food/exercise wednesday 10/3
5.07554844226 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.23323613477 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.351030687614 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

How much proteins leak into water when cooking vegetables ?
2.93064858147 ['with', 'your', 'weight', 'have', 'control', 'about']
1.18774119665 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.00439761415131 ['that', 'much', 'does', 'exercise', 'this', 'morning']

Food & Exercise -- 10/3/2007
3.50183444986 ['food', 'home', 'then', 'exercise', 'morning', 'went']
2.21222380937 ['quot', 'they', 'money', 'want', 'very', 'best']
1.37194239805 ['fats', 'quot', 'this', 'good', 'about', 'like']

The truth about exercising and your body as a whole
3.21386971879 ['that', 'much', 'does', 'exercise', 'this', 'morning']
3.15819744924 ['with', 'your', 'weight', 'have', 'control', 'about']
0.270507407515 ['fats', 'quot', 'this', 'good', 'about', 'like']

Got the new Elliptical
1.14114086054 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.10131650413 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.477565648015 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

Fingerstick cholesterol tests accurate?
2.14611673458 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
1.26970417226 ['fats', 'quot', 'this', 'good', 'about', 'like']
0.750416051713 ['that', 'much', 'does', 'exercise', 'this', 'morning']

Weight Loss Tips
5.21079777525 ['with', 'your', 'weight', 'have', 'control', 'about']
1.59092846403 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
0.00134310805496 ['food', 'home', 'then', 'exercise', 'morning', 'went']

Obesity Driving Rising U.S. Health Costs
1.25615188819 ['with', 'your', 'weight', 'have', 'control', 'about']
0.712235310825 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.238899338741 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Flu-Busting Chicken Soup
5.54567450388 ['with', 'your', 'weight', 'have', 'control', 'about']
1.21893998075 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.451175568656 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

Re: My Healing Story
1.96323375291 ['with', 'your', 'weight', 'have', 'control', 'about']
0.491756558034 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.477646079039 ['fats', 'quot', 'this', 'good', 'about', 'like']

Food and Workout Log 10.2.07
2.60282810129 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
1.20884355476 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.950088631141 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

food/exercise Tuesday 10/2
2.64289395866 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
0.651445733893 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.371530809297 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

Food & Exercise -- 10/2/2007
4.03395454429 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.733387053358 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.335622901915 ['that', 'much', 'does', 'exercise', 'this', 'morning']

Diet Recommendations following stoppage of activity
2.76689639493 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
2.66796965036 ['quot', 'they', 'money', 'want', 'very', 'best']
0.00214949067482 ['fats', 'quot', 'this', 'good', 'about', 'like']

why I'm succeeding, finally, with my fitness
3.81276353396 ['that', 'much', 'does', 'exercise', 'this', 'morning']
2.28727363664 ['with', 'your', 'weight', 'have', 'control', 'about']
0.0084896973916 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

food/exercise Monday 10/1
6.52183126318 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.04845803053 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.220817568443 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']

Food & Exercise -- 10/1/2007
3.41693152333 ['food', 'home', 'then', 'exercise', 'morning', 'went']
1.43659575232 ['with', 'your', 'weight', 'have', 'control', 'about']
0.0107024339333 ['that', 'much', 'does', 'exercise', 'this', 'morning']

Good fats bad fats
14.9233786406 ['fats', 'quot', 'this', 'good', 'about', 'like']
0.12157320235 ['quot', 'they', 'money', 'want', 'very', 'best']
0.000388079511473 ['food', 'calories', 'than', 'easy', 'high', 'come']

milk products
2.91179410526 ['quot', 'they', 'money', 'want', 'very', 'best']
1.35981372517 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.894968443359 ['with', 'your', 'weight', 'have', 'control', 'about']

Food and Workout Log 10.1.07
4.86163252456 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
3.04379043965 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.288092400057 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

< 1g, etc.
5.96451663382 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.757014711498 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.0106873617525 ['with', 'your', 'weight', 'have', 'control', 'about']

peanut butter
2.79640093073 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.974232666377 ['fats', 'quot', 'this', 'good', 'about', 'like']
0.20210540615 ['food', 'home', 'then', 'exercise', 'morning', 'went']

food/exercise Sunday 9/30
3.13525705865 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.77396028396 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.84994166551 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']

Food and Workout Log 9.30.07
4.16473442796 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
1.1093666032 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.0787803510605 ['food', 'calories', 'than', 'easy', 'high', 'come']

Food & Exercise -- 9/30/2007
4.50185209238 ['food', 'home', 'then', 'exercise', 'morning', 'went']
1.04931983732 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.691962870134 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

Food and Workout Log 9.29.07
4.84939396321 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
2.39617281343 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.816992749371 ['with', 'your', 'weight', 'have', 'control', 'about']

Food and Workout Log 9.28.07
4.67171965065 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.282691470562 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.200338717705 ['food', 'home', 'then', 'exercise', 'morning', 'went']

LOUIE + LINESMAKER =

$$ $$

5.58276496802 ['quot', 'they', 'money', 'want', 'very', 'best']
0.342556596947 ['with', 'your', 'weight', 'have', 'control', 'about']
0.179902643439 ['food', 'home', 'then', 'exercise', 'morning', 'went']

Food & Exercise -- 9/29/2007 (yesterday)
2.58541666839 ['quot', 'they', 'money', 'want', 'very', 'best']
2.09030954926 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.852396204369 ['that', 'much', 'does', 'exercise', 'this', 'morning']

ASDLC has changed
0.112818164838 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.102149673333 ['with', 'your', 'weight', 'have', 'control', 'about']
0.0271390834142 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']

diet/exercise Saturday 9/29
2.11947826799 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
1.44172267631 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.589173976223 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

Sensible Diet & Exercise
5.04673654071 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
3.61357653903 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.40785970283 ['with', 'your', 'weight', 'have', 'control', 'about']

Abstinence 2.- (20 September to 7 October )
0.968219095052 ['food', 'calories', 'than', 'easy', 'high', 'come']
0.949878615175 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
0.46422787912 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

Evolution and Weight
2.20784229869 ['quot', 'they', 'money', 'want', 'very', 'best']
1.18857451599 ['fats', 'quot', 'this', 'good', 'about', 'like']
1.00060668406 ['with', 'your', 'weight', 'have', 'control', 'about']

Food & Exercise -- 9/28/2007 (yesterday)
4.75585045074 ['food', 'home', 'then', 'exercise', 'morning', 'went']
0.840897380766 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.0455304461165 ['quot', 'they', 'money', 'want', 'very', 'best']

The Abs Diet by David Zinczenko
6.58003120192 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.00218473474643 ['fats', 'quot', 'this', 'good', 'about', 'like']
0.00140268366152 ['food', 'calories', 'than', 'easy', 'high', 'come']

Re: ABC News Nightline: Carbohydrates Make You Fat, and Perhaps Sick
2.50629631142 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.32614477527 ['with', 'your', 'weight', 'have', 'control', 'about']
0.194215052468 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']

Sensible Eating
2.67280083529 ['quot', 'they', 'money', 'want', 'very', 'best']
1.95758534619 ['with', 'your', 'weight', 'have', 'control', 'about']
0.272657483613 ['fats', 'quot', 'this', 'good', 'about', 'like']

3 slices of ff cheese on a poached egg
0.925803022044 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
0.158529510462 ['food', 'home', 'then', 'exercise', 'morning', 'went']
1.69625785908e-009 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']

food/exercise Friday 9/28
4.2243066995 ['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
1.69260968796 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.355277035138 ['food', 'home', 'then', 'exercise', 'morning', 'went']

money making opportunity
1.52941598839 ['that', 'much', 'does', 'exercise', 'this', 'morning']
1.23535469791 ['quot', 'they', 'money', 'want', 'very', 'best']
0.292057461928 ['with', 'your', 'weight', 'have', 'control', 'about']

The Benefits of Biotechnology For Mankind
0.543967592519 ['with', 'your', 'weight', 'have', 'control', 'about']
0.389852100811 ['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
0.208865237821 ['fats', 'quot', 'this', 'good', 'about', 'like']

Food and Workout Log 9.27.08
5.58477112035 ['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
0.093698650419 ['food', 'home', 'then', 'exercise', 'morning', 'went']
1.16281432958e-005 ['quot', 'they', 'money', 'want', 'very', 'best']

Re: My First Century Ride
2.7939634836 ['quot', 'they', 'money', 'want', 'very', 'best']
1.98080235676 ['with', 'your', 'weight', 'have', 'control', 'about']
0.0869645267992 ['fats', 'quot', 'this', 'good', 'about', 'like']

A Special Update from Matty V.
1.64986127649 ['that', 'much', 'does', 'exercise', 'this', 'morning']
0.635897197115 ['with', 'your', 'weight', 'have', 'control', 'about']
0.337529069245 ['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']

新闻特征数据
['diet', 'with', 'great', 'what', 'trinidad', 'carnival']
(6.58003120192, u'The Abs Diet by David Zinczenko')
(5.9231935598, u"I did'nt diet to get in shape for Trinidad's Carnival.....")
(5.04673654071, u'Sensible Diet & Exercise')

['coffee', 'black', 'exercise', 'minutes', 'olive', 'small']
(6.52183126318, u'food/exercise Monday 10/1')
(5.94642162786, u'diet/exercise 10/4')
(5.3332773133, u'food/exercise Friday 10/5')

['food', 'calories', 'than', 'easy', 'high', 'come']
(9.98464450123, u'High or low fat food? Easy trick for figuring it out')
(3.41252863148, u'Oatmeal, cereal of choice.')
(3.19119866786, u'Food and Workout Log 10.8.07')

['cheese', 'black', 'salad', 'coffee', 'broccoli', 'tomato']
(7.46811621754, u'saturday')
(5.62839188358, u'diet-exercise thursday')
(5.29370213306, u'sleepy food/fitness thursday')

['food', 'home', 'then', 'exercise', 'morning', 'went']
(5.22083940456, u'Food & Exercise -- 10/5/2007')
(5.16310413391, u'Food & Exercise -- 10/4/2007')
(4.75585045074, u'Food & Exercise -- 9/28/2007 (yesterday)')

['fats', 'quot', 'this', 'good', 'about', 'like']
(14.9233786406, u'Good fats bad fats')
(1.3775418859, u'Should we ban marathons?')
(1.37194239805, u'Food & Exercise -- 10/3/2007')

['quot', 'they', 'money', 'want', 'very', 'best']
(6.1620884463, u'More about the Chicago marathon')
(5.58276496802, u'LOUIE + LINESMAKER =

$$ $$

')
(4.04959173123, u'High or low fat food? Easy trick for figuring it out')

['that', 'much', 'does', 'exercise', 'this', 'morning']
(7.73926153154, u'Food & Exercise -- 10/7/2007')
(5.96451663382, u'< 1g, etc.')
(3.81276353396, u"why I'm succeeding, finally, with my fitness")

['with', 'your', 'weight', 'have', 'control', 'about']
(6.78756986407, u'Control ur Weight')
(5.54567450388, u'Flu-Busting Chicken Soup')
(5.21079777525, u'Weight Loss Tips')

['with', 'lunch', 'workout', 'food', 'butter', 'peanut']
(5.58477112035, u'Food and Workout Log 9.27.08')
(5.48488799917, u'Food and Workout Log 10.3.07')
(5.10395750879, u'Food and Workout Log 10.10.07')

股票模拟数据
5.26743580154e+017
3.93402025291e+016
2.21688612312e+016
1.71500393528e+016
1.49411594165e+016
[[ 2.33322541e+06 2.07819608e+06 2.51935438e+06 2.96234043e+06

1.75536111e+06   7.86146406e+06   2.63057169e+06   2.15047807e+06
5.08400536e+06   7.00030282e+06   1.85413701e+07   3.38175040e+06]

[ 4.39522609e+06 3.06456173e+05 1.01774069e+06 5.95775828e+05

4.58278700e+05   2.44897111e+06   6.88990546e+05   9.20287049e+05
4.92159041e+06   2.73739991e+06   3.16536914e+06   1.59875019e+07]

[ 1.94852289e+07 2.76219783e+05 2.65520981e+03 3.05103534e+05

1.98473327e+05   3.64804329e+05   1.19037805e+05   1.98460099e+02
3.76011874e+05   1.43281935e+05   1.39846581e+06   3.84252682e+05]

[ 1.17533915e+07 3.03635741e+05 5.79421694e+05 4.36884572e+05

3.06811879e+05   9.98011680e+05   5.08825718e+05   2.75383182e+05
5.01943100e+06   1.15884764e+06   1.40079467e+06   1.47720209e+04]

[ 1.05481574e+07 3.70822814e+05 6.30403606e+05 7.01379744e+05

1.69117963e+05   1.67921090e+06   6.68489498e+05   3.92653670e+05
1.29366132e+03   1.25383449e+06   4.57858763e+06   4.65246631e+05]]

[[ 0.43597457 0.05871326 0.06516699 0.03360034 0.52074744]
[ 0.44059965 0.1652264 0.29512033 0.11415813 0.55129002]
[ 0.71651796 0.14618471 0.76636368 0.58318813 0.27252264]
...,
[ 1.28562362 0.84010606 0.65675734 0.2187646 0.68153007]
[ 0.78639688 0.40560653 1.21738032 1.17089036 2.06706388]
[ 2.45069957 0.00640682 0.86072825 0.10106403 1.12640551]]
Feature 0
(18541370.141110275, 'XOM')
(7861464.0553792343, 'CVX')
(7000302.8181583285, 'PG')
(5084005.3613334689, 'GOOG')
(3381750.4044293971, 'AMGN')
(2962340.4315599473, 'BP')
(2630571.6923459047, 'DNA')
(2519354.3804378472, 'BIIB')
(2333225.4065250917, 'YHOO')
(2150478.0737609738, 'EXPE')
(2078196.0848287165, 'AVP')
(1755361.1131727577, 'CL')

[(2.4506995728828622, '18-Oct-05'), (1.7327784403764923, '11-Sep-06'), (1.5111300572258395, '8-Jun-06')]

Feature 1
(15987501.883808712, 'AMGN')
(4921590.4116128432, 'GOOG')
(4395226.0932264365, 'YHOO')
(3165369.1418494503, 'XOM')
(2737399.9096869556, 'PG')
(2448971.1065134653, 'CVX')
(1017740.6942413859, 'BIIB')
(920287.04939950886, 'EXPE')
(688990.54637332377, 'DNA')
(595775.82846660342, 'BP')
(458278.69976566656, 'CL')
(306456.1727793481, 'AVP')

[(5.5183934865182875, '15-Feb-06'), (2.138473391072961, '1-Feb-06'), (1.9475044925471119, '26-Jan-06')]

Feature 2
(19485228.873686153, 'YHOO')
(1398465.8074515802, 'XOM')
(384252.68231490435, 'AMGN')
(376011.87440058013, 'GOOG')
(364804.32850560133, 'CVX')
(305103.53400016041, 'BP')
(276219.78349040612, 'AVP')
(198473.32671485722, 'CL')
(143281.93458262246, 'PG')
(119037.80463716132, 'DNA')
(2655.2098122150296, 'BIIB')
(198.46009910268154, 'EXPE')

[(8.3018051767438337, '19-Jul-06'), (4.5697390847378792, '19-Sep-06'), (2.506039176128628, '19-Apr-06')]

Feature 3
(11753391.461576829, 'YHOO')
(5019430.9962252304, 'GOOG')
(1400794.6664170395, 'XOM')
(1158847.6445206082, 'PG')
(998011.67965212127, 'CVX')
(579421.69354580715, 'BIIB')
(508825.71818347432, 'DNA')
(436884.5719282077, 'BP')
(306811.8787867761, 'CL')
(303635.74069823755, 'AVP')
(275383.18216351332, 'EXPE')
(14772.020946359988, 'AMGN')

[(6.9635400795449733, '18-Jan-06'), (4.4080426022720891, '18-Oct-06'), (3.6766225277997848, '20-Jan-06')]

Feature 4
(10548157.403712066, 'YHOO')
(4578587.6349422066, 'XOM')
(1679210.8955857321, 'CVX')
(1253834.4926672454, 'PG')
(701379.74418841151, 'BP')
(668489.49759360566, 'DNA')
(630403.60590710363, 'BIIB')
(465246.63059152756, 'AMGN')
(392653.67018991744, 'EXPE')
(370822.8136748391, 'AVP')
(169117.96293892173, 'CL')
(1293.6613221068894, 'GOOG')

[(3.2242716186256213, '19-Jul-06'), (2.4565899212822875, '18-Oct-06'), (2.2169891870590743, '17-Oct-06')]

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
遗传算法可以用于航班优化问题,以下是一个基于遗传算法的航班优化代码示例: ```python import random # 航班信息(起飞时间、到达时间、出发地、目的地) flights = [('BA001', '08:00', '10:00', 'LHR', 'JFK'), ('BA002', '12:00', '14:00', 'JFK', 'LHR'), ('BA003', '09:00', '12:00', 'LHR', 'CDG'), ('BA004', '11:00', '14:00', 'CDG', 'LHR'), ('BA005', '10:00', '12:00', 'LHR', 'FRA'), ('BA006', '13:00', '15:00', 'FRA', 'LHR'), ('BA007', '10:00', '13:00', 'LHR', 'MAD'), ('BA008', '14:00', '17:00', 'MAD', 'LHR'), ('BA009', '11:00', '13:00', 'LHR', 'AMS'), ('BA010', '14:00', '16:00', 'AMS', 'LHR')] # 目标函数,评估航班安排的优劣 def fitness(solution): total_time = 0 for i in range(len(solution)-1): flight1 = flights[solution[i]] flight2 = flights[solution[i+1]] if flight1[3] != flight2[2]: total_time += 2 # 转机时间为2小时 total_time += (int(flight2[2][:2]) - int(flight1[1][:2])) # 飞行时间 return -total_time # 目标是最小化总时间 # 遗传算法 def genetic_algorithm(population_size, fitness_func, gene_size, generation_num): # 初始化种群 population = [[i for i in range(gene_size)] for _ in range(population_size)] for i in range(population_size): random.shuffle(population[i]) # 迭代 for generation in range(generation_num): # 评估种群 scores = [fitness_func(solution) for solution in population] # 选择 elites = [] for i in range(population_size // 5): # 选择前20%的精英个体 max_index = scores.index(max(scores)) elites.append(population[max_index]) del population[max_index] del scores[max_index] # 交叉 for i in range(population_size - len(elites)): parent1 = random.choice(population) parent2 = random.choice(population) child = [0] * gene_size start = random.randint(0, gene_size-1) end = random.randint(start, gene_size-1) child[start:end+1] = parent1[start:end+1] for j in range(gene_size): if parent2[j] not in child: for k in range(gene_size): if child[k] == 0: child[k] = parent2[j] break population.append(child) # 精英保留策略 population = elites + population[:population_size-len(elites)] # 返回最优解 scores = [fitness_func(solution) for solution in population] max_index = scores.index(max(scores)) return population[max_index] # 调用遗传算法寻找最优解 solution = genetic_algorithm(population_size=100, fitness_func=fitness, gene_size=len(flights), generation_num=1000) print(solution) ``` 在上面的代码中,我们定义了一个航班信息列表 `flights`,每个元素包含航班号、起飞时间、到达时间、出发地和目的地。然后定义了一个目标函数 `fitness`,用于评估航班安排的优劣,目标是最小化总时间。接着定义了一个遗传算法函数 `genetic_algorithm`,其中包括种群初始化、迭代、选择、交叉和精英保留策略等操作。最后调用遗传算法函数寻找最优解,并将结果打印出来。 需要注意的是,这只是一个简单的示例代码,实际应用中还需要考虑诸如航班延误、航班取消等情况,以及更加复杂的约束条件和目标函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值