genetic algorithm tcl code in ns2
#1
Wink 

Dear,
I request you to send the Genetic Algorithm tcl code in ns2 to me.
thankyou,



regards
ameena.muruga[at]gmail.com
Reply
#2

# genetic_alg.tcl --
#
# Package for implementing simple genetic algorithms
# (Tcl-only version)
#
# Notes:
# This package is shamelessly modelled after a publically
# available program by Scott Robert Ladd (http://coyotegulch.com)
#
# Version information:
# version 0.1: initial implementation, february 2002

package provide GeneticAlgorithms 0.1

namespace eval ::GeneticAlgorithms {
variable crossover 0.5
variable mutation 0.1
variable elitism 1
variable quadweight 0

variable best_guess {}
variable population {}
variable child_pop {}

namespace export setting optimise optimiseStep

# limitvalue
# Limit the argument between two bounds
#
# Arguments:
# value New value (if not present, return current value)
# minimum Minimum bound
# maximum Maximum bound
#
# Result:
# Value or one of the bounds
#
proc limitvalue {value minimum maximum} {
if { $value < $minimum } {
return $minimum
}
if { $value > $maximum } {
return $maximum
}
return $value
}

# setting
# Set/get the settings
#
# Arguments:
# name Name of a variable to set or get
# value New value (if not present, return current value)
#
# Result:
# New value for given variable
#
# Side effects:
# Sets given variable to new value
#
proc setting {name {value NONE} } {
variable $name
if { $value != "NONE" } {
switch -- $name {
"crossover" { set $name [limitvalue $value 0.0 1.0] }
"mutation" { set $name [limitvalue $value 0.0 1.0] }
"elitism" { set $name [expr $value!=0] }
"quadweight" { set $name [expr $value!=0] }
default { error "setting: unknown parameter $name" }
}
set $name
}
}

# optimise --
# Optimise the given function using a genetic algorithm
#
# Arguments:
# pop_size Size of the population
# max_gen Maximum number of generations
# no_genes Number of "genes" - degrees of freedoms
# fitness Function of the degrees of freedom, returns the fitness
# of the solution (as a non-negative number!)
#
# Result:
# Best guess of degrees of freedom, as a list
#
proc optimise { pop_size max_gen no_genes fitness } {
variable best_guess

optimiseInit $pop_size $no_genes

for { set i 0 } { $i < $max_gen } { incr i } {
optimiseStep $pop_size $no_genes $fitness
puts "$best_guess - [$fitness $best_guess]"
}

return $best_guess
}

# optimiseInit --
# Initialise the population
#
# Arguments:
# pop_size Size of the population
# no_genes Number of "genes" - degrees of freedoms
#
# Result:
# None
#
# Side effects:
# Initialised list variable population
#
proc optimiseInit { pop_size no_genes } {
variable population
variable child_pop

set population {}

for { set i 0 } { $i < $pop_size } { incr i } {
set member {}
for { set j 0 } { $j < $no_genes } { incr j } {
lappend member [expr {int(2147483000.0*rand())}]
}
lappend population $member
}

set child_pop $population
}

# optimiseStep --
# Perform a single step in the optimisation
#
# Arguments:
# pop_size Size of the population
# no_genes Number of "genes" - degrees of freedoms
# fitness Function for determining the fitness
#
# Result:
# None
#
# Side effects:
# New population, best_guess set
#
proc optimiseStep { pop_size no_genes fitness } {
variable population
variable child_pop
variable best_guess
variable mutation
variable crossover
variable quadweight
variable elitism

#
# Copy the child population
#
set population $child_pop

#
# Determine the fitness per member
#
set high_fit -1
set tot_fit 0.0
set pop_fit {}
foreach member $population {
set fit [eval $fitness $member]
if { $high_fit < $fit } {
set high_fit $fit
set best_guess $member
}

lappend pop_fit $fit
set tot_fit [expr {$tot_fit+$fit}]
}

#
# Scale the fitness (quadratic weight)
#
# PM

#
# Elitism: keep the best in any case
#
set child_pop {}
set no_child $pop_size
if { $elitism } {
lappend child_pop $best_guess
incr no_child -1
}

#
# Breed the children
#
for { set i 0 } { $i < $no_child } { incr i } {
set selection [expr {$tot_fit*rand()}]
set father 0
set father_fit [lindex $pop_fit $father]
while { $selection > $father_fit } {
set selection [expr {$selection-$father_fit}]
incr father
set father_fit [lindex $pop_fit $father]
}

set selection [expr {$tot_fit*rand()}]
set mother 0
set mother_fit [lindex $pop_fit $mother]
while { $selection > $mother_fit } {
set selection [expr {$selection-$mother_fit}]
incr mother
set mother_fit [lindex $pop_fit $mother]
}

set child [combineGenes [lindex $population $mother] \
[lindex $population $father] ]
set child [mutateGenes $child]
lappend child_pop $child
}

#puts $population
#puts $pop_fit
}

# combineGenes --
# Combine the genes of the two parents (using cross-over)
#
# Arguments:
# mother Genes of the first parent
# father Genes of the second parent
#
# Result:
# Genes of the child
#
proc combineGenes { mother father } {
variable crossover

set all_bits_set -2147483647

set child {}
foreach first $mother second $father {
set bit_no [expr int(32.0*rand())]
set bitmask [expr {($all_bits_set>>$bit_no)<<$bit_no}]
set newgene [expr {$first&$bitmask|$second&~$bitmask}]

lappend child $newgene
}

return $child
}

# mutateGenes --
# Mutate the genes of a child (flip a bit)
#
# Arguments:
# child Genes of the child to be mutated
#
# Result:
# Mutated genes
#
proc mutateGenes { child } {
variable mutation

set newgenes {}
foreach gene $child {
if { [expr {rand()}] < $mutation } {
set bit_no [expr {int(32.0*rand())}]
set bitmask [expr {1<<$bit_no}]
set bitset [expr {($gene&$bitmask) != 0}]
if { $bitset } {
set newgene [expr {$gene&~$bitmask}]
} else {
set newgene [expr {$gene|$bitmask}]
}
} else {
set newgene $gene
}

lappend newgenes $newgene
}

return $newgenes
}

} ;# End of namespace

#namespace import ::GeneticAlgorithms::*

proc testFunc { var } {
expr {1.0-abs($var/2147483647.0-0.5)}
}

puts [::GeneticAlgorithms::optimise 100 40 1 testFunc]
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: ecc algorithm source code in tcl, code tcl to create a wsn, secure aodv ns2 code with tcl full, vehicles in ns2 tcl, genetic algorithm ns2 tcl code, tcl code for ns2 projects, ns2 bluetooth examples tcl script,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  image encryption and decryption using rsa algorithm in matlab 2 7,916 29-05-2017, 04:17 PM
Last Post: Priyanka Bidikar
  source code for task scheduling using genetic algorithm using java 2 8,535 11-04-2017, 08:31 PM
Last Post: Guest
  vhdl code for radix 2 modified booth algorithm 4 1,023 04-04-2017, 10:24 AM
Last Post: Garlapati nikitha
  secure chat using RSA algorithm karthik1218 2 2,583 14-10-2016, 02:48 PM
Last Post: info togel
  tcl code for trust value calculation 2 764 08-10-2016, 11:17 AM
Last Post: [email protected]
  f5 algorithm steganography matlab code 2 871 04-10-2016, 03:00 AM
Last Post: [email protected]
  color image segmentation using jseg algorithm in matlab code 2 868 29-09-2016, 12:07 PM
Last Post: Guest
  matlab xy routing algorithm 2 830 12-08-2016, 09:16 PM
Last Post: khant
  matlab code energy based spectrum sensing in cognitive radio energy threshold based algorithm 2 1,063 06-08-2016, 03:30 PM
Last Post: murthyhs
  solved examples on audio steganography using genetic algorithm 2 628 21-07-2016, 03:12 PM
Last Post: dhanabhagya

Forum Jump: