# -*- coding: utf-8 -*-
"""Copyright 2015 Ericsson AB

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the 
License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License.
"""
import struct
import time
import random
from dbConnection import DB
from deap import base
from deap import creator
from deap import tools
from datetime import datetime, timedelta
from itertools import repeat
from collections import Sequence
import mutation 


MUTPB = 0.5
from deap import base, creator, tools
from itertools import repeat
from collections import Sequence

from dbConnection import DB
from fitness import Fitness

# Initialize the look ahead class
lookAhead = DB()
fitness = Fitness()
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
# Register genes on the toolbox
toolbox = base.Toolbox()
# toolbox.register("line", lookAhead.getRoute, "line")
# The parameter here is the number of the line
# Define the genes on every chromosome
toolbox.register("attribute", lookAhead.generateTripTimeTable, 2)
toolbox.register("individual", tools.initRepeat, creator.Individual,
                 toolbox.attribute, 5)
toolbox.register("population", tools.initRepeat, list, toolbox.individual, 10)
# Operator registering
toolbox.register("evaluate", fitness.evalIndividual)
toolbox.register("mate", tools.cxOnePoint)
toolbox.register("mutate", mutation.mutUniformTime)
toolbox.register("select", tools.selTournament, tournsize=3)
# ind = toolbox.individual()
# print ind
# Generate the population
pop = toolbox.population()
# print pop
# Evaluate the entire population
fitnesses = list(map(toolbox.evaluate, pop))
for ind, fit in zip(pop, fitnesses):
    ind.fitness.values = fit
#print("  Evaluated %i individuals" % len(pop))

print len(pop)
offspring = toolbox.select(pop, len(pop))
print offspring
offspring = list(map(toolbox.clone, offspring))
print offspring
'''

# Testing mutation
for mutant in pop:
    if random.random() < MUTPB:
        toolbox.mutate(mutant)
        del mutant.fitness.values
'''
invalids = [ind for ind in pop if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalids)
for ind, fit in zip(invalids, fitnesses):
    ind.fitness.values = fit

'''
print " Mutation done"

# for child1, child2 in zip(pop[::2], pop[1::2]):
    # print "Child 1"
    # print len(child1)
    # print "Child 2"
    # print len(child2)

for child1, child2 in zip(pop[::2], pop[1::2]):
    print "Child 1"
    print len(child1)
    print "Child 2"
    print len(child2)
    #print(toolbox.mate(child1, child2))
    '''

@@ -579,200 +579,85 @@
 %22%22%22%0A
-import struct%0Aimport time%0Aimport random%0Afrom dbConnection import DB%0Afrom deap import base%0Afrom deap import creator%0Afrom deap import tools%0Afrom datetime import datetime, timedelta
+from deap import base, creator, tools%0Afrom dbConnection import DB
 %0Afrom 
+f
 it
-ertool
+nes
 s im
@@ -665,47 +665,15 @@
 ort 
-repeat%0Afrom collections import Sequence
+Fitness
 %0Aimp
@@ -688,11 +688,20 @@
 tion
- %0A%0A
+%0A# Variables
 %0AMUT
@@ -713,236 +713,146 @@
 0.5%0A
-from deap import base, creator, tools%0Afrom itertools import repeat%0Afrom collections import Sequence%0A%0Afrom dbConnection import DB%0Afrom fitness import Fitness%0A%0A# Initialize the look ahead class%0AlookAhead = DB()%0Afitness = Fitness()
+BUSLINE = 2%0ANUMTRIP = 5%0APOPSIZE = 10%0A%0A# Initialize the DB and Fitness classes%0Adb = DB()%0Afitness = Fitness()%0A# Create individuals structure
 %0Acre
@@ -976,29 +976,23 @@
 )%0A# 
-Register genes on the
+Initialize DEAP
  too
@@ -1027,146 +1027,33 @@
 )%0A# 
-toolbox.register(%22line%22, lookAhead.getRoute, %22line%22)%0A# The parameter here is the number of the line%0A# Define the genes on every chromosome
+Create initial population
 %0Atoo
@@ -1083,17 +1083,10 @@
 e%22, 
-lookAhea
 d
+b
 .gen
@@ -1105,17 +1105,23 @@
 eTable, 
-2
+BUSLINE
 )%0Atoolbo
@@ -1219,17 +1219,23 @@
 ribute, 
-5
+NUMTRIP
 )%0Atoolbo
@@ -1307,34 +1307,45 @@
 al, 
-10)%0A# Operator registering
+POPSIZE)%0A# Register genetic operators
 %0Atoo
@@ -1554,49 +1554,8 @@
 =3)%0A
-# ind = toolbox.individual()%0A# print ind%0A
 # Ge
@@ -1609,20 +1609,8 @@
 )%0A# 
-print pop%0A# 
 Eval
@@ -1617,16 +1617,35 @@
 uate the
+ fitness values for
  entire 
@@ -1771,70 +1771,34 @@
 it%0A#
-print(%22  Evaluated %25i individuals%22 %25 len(pop))%0A
+ Apply crossover
 %0Aprint 
-len(
 pop
-)
 %0Aoff
@@ -1836,32 +1836,16 @@
 n(pop))%0A
-print offspring%0A
 offsprin
@@ -1888,29 +1888,13 @@
 g))%0A
-print offspring
 %0A'''%0A
-%0A
 # Te
@@ -2240,152 +2240,8 @@
 ne%22%0A
-%0A# for child1, child2 in zip(pop%5B::2%5D, pop%5B1::2%5D):%0A    # print %22Child 1%22%0A    # print len(child1)%0A    # print %22Child 2%22%0A    # print len(child2)%0A%0A
 for 

Format main file code