Genetic Algorithms - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Genetic Algorithms

Description:

Main Function (Functions are passed as arguments) 19 ... { mate1 = select (setup- population_size, ga- sum_fitness,ga- populations[ga- g_old] ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 20
Provided by: nhre
Category:

less

Transcript and Presenter's Notes

Title: Genetic Algorithms


1
Genetic Algorithms
159.734
Part 2 GA-NN
2
GA-NN
19
Main Function (Functions are passed as arguments)
typedef void (func)(int) void test1(int
a) cout ltlt "Inside Function 1" ltlt endl cout
ltlt "value is " ltlt a ltlt endl void test2(int
a) coutltlt"Inside Function 2" ltlt endl cout ltlt
"value is " ltlt a ltlt endl void test3 (func fp,
int v) (fp)(v) int main()
test3(test1, 4) return 0
const int pop_size 40,bit_len 8,vect_len 2
initialize_ga_setup(pop_size,bit_len,vect_len,
ga_setup_rec) initialize_ff_setup
(g_ff_setup) switch (prog_control) case
run_net run_ff_network (g_ff_setup) break
case train_net train_network_from_data
(g_ff_setup, backpropagation_training) br
eak case train2_net train_network_from_data
(g_ff_setup, genetic_alg_training) break
// end switch
This is just a portion of the main function and
not complete.
3
GA-NN
19
initialize_ga_setup
void initialize_ga_setup (int pop_size,int
bit_len,int vect_len, tga_setup_rec the_rec)
int i the_rec-gtpopulation_size
pop_size the_rec-gtbit_len bit_len
the_rec-gtvect_len vect_len
the_rec-gtchrom_len bit_lenvect_len
the_rec-gtmax_gens 200 the_rec-gtpick_fittest
TRUE the_rec-gtcrossover_prob 0.6
the_rec-gtmutation_prob 1.0/pop_size
the_rec-gttolerance 0.01 return
4
GA-NN
19
initialize_ff_setup
void initialize_ff_setup (ff_setup_rec
the_setup) the_setup-gterror_threshold
0.01 the_setup-gtstart_iter 0
the_setup-gtshuffle TRUE the_setup-gtno_of_inp
uts 0 the_setup-gtno_of_h1_nodes 0
the_setup-gtno_of_outputs 0
the_setup-gtin_weights_from_file FALSE
the_setup-gtsigmoid_out TRUE
the_setup-gtalpha 0.9 the_setup-gtlearning_rat
e 1.0 the_setup-gtnorm_in_min -5.0
the_setup-gtnorm_in_range 10.0
the_setup-gtnorm_out_min 0.05
the_setup-gtnorm_out_range 0.95
the_setup-gtweight_range 1.0 return
5
GA-NN
19
void genetic_alg_training (ff_setup_rec
setup, weights_rec weights,training_rec
tr_rec, get_data_proc get_network_input_output
) / 'get_network_input_output' is not
used. This paramater is / / needed to
match format of training_proc. / int
i,j,k float err d_vector
v_weights g_tr_rec tr_rec
g_no_of_weights (setup.no_of_inputs 1)
setup.no_of_h1_nodes setup.no_of_h1_nodes
setup.no_of_outputs max_ff_obj_fn_value
compute_max_obj_fn_value (setup,tr_rec.no_of_t
raining_items) ga_setup_rec.vect_len
g_no_of_weights ga_setup_rec.chrom_len
ga_setup_rec.bit_len ga_setup_rec.vect_len
ga_setup_rec.tolerance setup.error_threshold
w_range allocate_f_vector
(0,g_no_of_weights-1) w_min
allocate_f_vector (0,g_no_of_weights-1) for
(i0iltg_no_of_weightsi) w_rangei 2
setup.weight_range w_mini
-setup.weight_range genetic_algorithm
(ga_setup_rec,ga_rec,ga_obj_fn) / Get
network weights from genetic output /
v_weights allocate_d_vector (1,g_no_of_weights)
for (i0iltg_no_of_weightsi)
v_weightsi1 ((ga_rec.opt_vector)i/g_ga_max
i) w_rangei w_mini
weights_to_network_wts (setup,v_weights,weights)
free_d_vector (v_weights,1)
free_f_vector (w_range,0) free_f_vector
(w_min,0) write_weights (weights,setup.files
.out_wts_file_name,setup) / end proc /
genetic_alg_training
6
GA-NN
19
genetic_alg_training
Bias node
Calculate number of weights and chromosome length
g_no_of_weights (no_of_inputs 1)
no_of_hidden_nodes
(no_of_hidden_nodes no_of_outputs)
vector_length g_no_of_weights
8
chrom_length bit_length vector_length
tolerance error_threshold
0.01
fitness tolerance
7
GA-NN
19
genetic_alg_training
1.0
for (i0 i lt g_no_of_weights i)
w_rangei 2 setup.weight_range w_mini
- setup.weight_range genetic_algorithm
() / Get network weights from genetic output
/ for (i0 i lt g_no_of_weights i)
v_weightsi1 ((ga_rec.opt_vector)i /
g_ga_maxi) w_rangei w_mini weights_t
o_network_wts (setup, v_weights,
weights) write_weights ()
1.0
Integer equivalent of a chromosome
2.0
-1.0
Contains the largest integer that can be handled
by the algorithm, as determined by
power_of_two(bit_len)
8
GA-NN
19
weights_to_network_wts
Copy the weights to the appropriate links in the
Neural Network.
void weights_to_network_wts (ff_setup_rec setup,
d_vector weights_vect,weights_rec
network_weights) int i,j,k 0 for
(j 1jlt(setup.no_of_inputs 1)j) for (i
1iltsetup.no_of_h1_nodesi) k
network_weights.wiji weights_vectk
/ j,i / for (j 1jltsetup.no_of_h1_nod
esj) for (i 1iltsetup.no_of_outputsi)
k network_weights.woji
weights_vectk / j,i / return
/ end proc /
9
GA-NN
19
genetic_algorithm
Run the Genetic Algorithm.
for (i0iltsetup-gtvect_leni)
g_ga_maxi power_of_2(setup-gtbit_len)
int gen 0 do gen
generation (setup,ga, the_obj_funct)
ga-gtg_old (ga-gtg_old 1)2
ga-gtg_new (ga-gtg_new 1)2 while ((gen
lt setup-gtmax_gens) (!within_tolerance)
(!cancel_flag))
Used as an index to the array populationsga-gtg_ol
d
10
GA-NN
19
generation
void generation (tga_setup_rec setup,tga_rec
ga, float (the_obj_funct)(ui_vector v))
/ Note Population size
must be even / int j 0,k,mate1,mate2,jcross
ui_vector v v allocate_ui_vector
(0,setup-gtvect_len - 1) do mate1
select (setup-gtpopulation_size,
ga-gtsum_fitness,ga-gtpopulationsga-gtg_old)
mate2 select (setup-gtpopulation_size,
ga-gtsum_fitness,ga-gtpopulationsga-gtg_old)
crossover (((ga-gtpopulations)ga-gtg_old)mate1
-gtchrom, ((ga-gtpopulations)ga-gtg_old)mate2
-gtchrom, ((ga-gtpopulations)ga-gtg_new)j-gt
chrom, ((ga-gtpopulations)ga-gtg_new)j1-gt
chrom, setup-gtchrom_len,(ga-gtncross),(ga-gtn
mutation),jcross, setup-gtcrossover_prob,
setup-gtmutation_prob) for (k jk lt j
1k) decode (setup,((ga-gtpopulations)g
a-gtg_new)k-gtchrom,v) ((ga-gtpopulations)ga
-gtg_new)k-gtraw_fitness the_obj_funct
(v) ((ga-gtpopulations)ga-gtg_new)k-gtparent
1 mate1 ((ga-gtpopulations)ga-gtg_new)k-gt
parent2 mate2 ((ga-gtpopulations)ga-gtg_new
)k-gtxsite jcross j 2
while (j lt setup-gtpopulation_size)
11
GA-NN
19
generation
free_ui_vector (v,0) if
(setup-gtpick_fittest) pick_fittest
(setup,(ga-gtpopulations)ga-gtg_old,
(ga-gtpopulations)ga-gtg_new)
rescale_population_fitness (setup,ga,ga-gtpopulat
ionsga-gtg_new) return / end proc
/
12
GA-NN
19
pick_fittest
void pick_fittest (tga_setup_rec setup,
ppopulation the_old, ppopulation the_new)
int i, j, min_index 0 float min_fitness
/ Replace min fitness members of new population
with fitter members from old population /
min_fitness (the_new)0-gtraw_fitness for
(j 1 j lt setup.population_size j)
if ((the_new)j-gtraw_fitness lt min_fitness)
min_fitness (the_new)j-gtraw_fitness
min_index j for (i 0 I lt
setup.population_size i) if
((the_old)i-gtraw_fitness gt min_fitness)
/ Replace min individual with new individual
/ move_individual (setup,(the_old)i,
(the_new)min_index) min_fitness
(the_new)0-gtraw_fitness / Determine min of
new population / min_index 0 for (j
1 j lt setup.population_size j) if
((the_new)j-gtraw_fitness lt min_fitness)
min_fitness (the_new)j-gtraw_fitness
min_index j / end if, j /
/ if,i / return / end proc /
13
GA-NN
19
Rescale Fitness
void rescale_population_fitness (tga_setup_rec
setup, tga_rec ga, ppopulation
the_population) int j float
max_fitness,min_fitness min_fitness
(the_population)0-gtraw_fitness max_fitness
(the_population)0-gtraw_fitness for (j
0 j lt setup.population_size j) if
((the_population)j-gtraw_fitness lt
min_fitness) min_fitness (the_population)j
-gtraw_fitness if ((the_population)j-gtr
aw_fitness gt max_fitness) max_fitness
(the_population)j-gtraw_fitness if
(ga.opt_individual.raw_fitness gt max_fitness)
max_fitness ga.opt_individual.raw_fitness
if (max_fitness - min_fitness lt TOO_SMALL)
min_fitness 0.9 max_fitness for (j
0 j lt setup.population_size j)
(the_population)j-gtscaled_fitness
scale_fitness ((the_population)j-gtraw_fitness,
min_fitness, max_fitness) return / end
proc /
Optimum individual fitness
Operate on the whole population
14
GA-NN
19
Fitness Scaling
define TOO_SMALL 1e-6
float scale_fitness (float x, float fmin,
float fmax) if (fabs (fmax - fmin) gt
TOO_SMALL) return (x - fmin) 0.95/ (fmax
- fmin) 0.05 / else / return 1.0
/ end fn /
15
GA-NN
19
GA Objective function
float ga_obj_fn (ui_vector ga_vect) int
i d_vector weights float
ga_obj_fn_value weights allocate_d_vector
(1,g_no_of_weights) for (i0iltg_no_of_weights
i) weightsi1 (ga_vecti /
g_ga_maxi) w_rangei w_mini
ga_obj_fn_value (MAX_GA_OBJ_FN_VALUE
- ff_objective_fn (weights)/max_ff_obj_fn_value)
free_d_vector (weights,1) return
ga_obj_fn_value
16
GA-NN
19
NN Objective function
float ff_objective_fn (d_vector w) /
Note that g_tr_rec must be allocated and
established / / before calling this
procedure. / int i float sum 0.0
network_rec network 0,NULL,NULL,NULL
f_vector d_out NULL weights_rec
network_wts 0,NULL,NULL if
(!g_tr_rec.allocated) cprintf
("\r\nff_objective_fn g_tr_rec not
allocated.") getch() exit (1)
allocate_network (g_ff_setup, network)
network_wts allocate_network_weights
(g_ff_setup) d_out allocate_f_vector (1,
g_ff_setup.no_of_outputs) weights_to_network_
wts (g_ff_setup, w, network_wts) Continued...
17
GA-NN
19
NN Objective function
Continuation... for (i 1iltg_tr_rec.no_of_train
ing_itemsi) memmove (network.x_inp
1,g_tr_rec.training_arrayi 1, g_ff_setup.no_
of_inputs sizeof(float)) memmove (d_out
1,g_tr_rec.norm_answersi 1, g_ff_setup.no_of
_outputs sizeof(float)) feedforward
(network,network_wts,g_ff_setup) / Accumulate
normalized error / sum ff_error
(d_out,network.y_out,g_ff_setup) / i /
if (heapcheck() lt 0) cprintf
("\r\n\nHeapcheck lt 0 in objective_fn")
getch() exit(1)
free_f_vector (d_out,1) free_weights
(network_wts) free_network (network)
return sum / end objective_fn /
18
GA-NN
19
Sum of Squared Errors
float ff_error (f_vector desired,f_vector
net_out,ff_setup_rec setup) int i
float sum 0.0 for (i 1i lt
setup.no_of_outputsi) sum
(desiredi - net_outi) (desiredi -
net_outi) return 0.5 sum
/ end function /
19
GA-NN
19
Feed-forward Network
void feedforward (network_rec net,weights_rec
weights, ff_setup_rec setup) int i,j
double wx 0.0,wh1 0.0
net.x_inpsetup.no_of_inputs 1 1.0 /
threshold node / for (i1iltsetup.no_of_h1_n
odesi) wx 0.0 for (j1jlt(setup.no_of_i
nputs 1)j) wx weights.wijinet.x_
inpj net.h1i sigmoid(wx) / i /
for (i1iltsetup.no_of_outputsi) wh1
0.0 for (j1jltsetup.no_of_h1_nodesj)
wh1 weights.wojinet.h1j if
(setup.sigmoid_out) net.y_outi
sigmoid(wh1) else net.y_outi wh1
/ i / return / end proc /
Write a Comment
User Comments (0)
About PowerShow.com