Genetic Algorithm code in ns2 – Genetic algorithm is as also a special kind of stochastic search algorithms, genetic algorithm is also a problem solving method which is based on the concept of natural selection and genetics.
What is genetic algorithm?
Functionalities of genetic algorithm:
- Population generation.
- Fitness function evaluation.
- Selection.
- Repair.
- Mutation.
- And also in Cross over.
Fields of genetic algorithm:
- For routing.
- Also For providing network security.
- In neural network.
- Also In networking communication.
- Applications also in sensor network.
- Biological in also process etc.
Sample Genetic algorithm code in ns2
puts [exec ps -C tclsh -orss,size] ;# XXX package require gaul puts "GAUL Library: [gaul version]\n" puts [exec ps -C tclsh -orss,size] ;# XXX proc genrand {max num} { set result for {set i 0} {$i < $num} {incr i} { lappend result [expr { int($max * rand()) }] } return $result } proc generation {name gen} { puts "\nGeneration: $gen\n" # XXX puts [pop list] } proc evaluate {name entity} { global goal set result [expr 100 * 10] for {set i 0} {$i < 10} {incr i} { incr result [expr -1 * abs([lindex $goal $i] - [lindex $entity $i])] } #puts "Evaluate: $result" return $result } proc seed {name entity} { set seed [genrand 100 10] #puts "Seed: $seed" return $seed } proc adapt {name entity} { ;# XXX #puts "Adapt" return $entity } proc mutate {name entity} { #puts "Mutate: original: $entity" lset entity [genrand 10 1] [genrand 100 1] #puts "Mutate: mutated: $entity" return $entity } proc crossover {name entity1 entity2} { #puts "Crossover: Father: $entity1" #puts "Crossover: Mother: $entity2" set cross [genrand 10 1] #puts "Crossover: cross: $cross" set child1 [concat [lrange $entity1 0 $cross-1] [lrange $entity2 $cross end]] set child2 [concat [lrange $entity2 0 $cross-1] [lrange $entity1 $cross end]] #puts "Crossover: Child1: $child1" #puts "Crossover: Child2: $child2" return } set goal [genrand 100 10] puts [exec ps -C tclsh -orss,size] ;# XXX gaul create pop 100 \ -generation_hook generation \ -evaluate evaluate \ -seed seed \ -adapt adapt \ -mutate mutate \ -crossover crossover \ -select_one SUS \ -select_two SUS puts [exec ps -C tclsh -orss,size] ;# XXX pop params {*}[pop params] pop seed puts [pop evolve 50] puts "Goal: $goal" puts "Solution: [pop solution]" # stats # list # rank puts [exec ps -C tclsh -orss,size] ;# XXX pop destroy puts [exec ps -C tclsh -orss,size] ;# XXX gaul create pop 100 \ -generation_hook generation \ -evaluate evaluate \ -seed seed \ -adapt adapt \ -mutate mutate \ -crossover crossover \ -select_one SUS \ -select_two SUS puts [exec ps -C tclsh -orss,size] ;# XXX rename pop {}Tweet