-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGeneticAlgorithm.ml
More file actions
100 lines (71 loc) · 2.7 KB
/
GeneticAlgorithm.ml
File metadata and controls
100 lines (71 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
open Core.Std
open Guess
open Helpers.Helpers
open Graphics
(* Signature of a generic Genetic Algorithm Module *)
module type GENETIC_ALGORITHM =
sig
(* The type of a guess*)
type guess
(* Type of the Genetic Algorithm *)
type ga
(* fresh s n m returns a fresh new Genetic Algorithm with Standard Deviation s
* and n guesses each containing m legos *)
val fresh : float -> color array array -> int -> int -> ga
(* 'evolve g n' performs the genetic algorithm for n generations *)
val evolve : ga -> int -> ga
(* Eliminates the unfit *)
val kill_phase : ga -> ga
(* Performs random reproduction on the fitter part of the sample *)
val reproduction_phase : ga -> ga
(* Returns the current best guess of this model *)
val get_best : ga -> guess
(* Draws the best guess of this model *)
val draw_best : ga -> unit
(* Prints the current state of this genetic algorithm *)
val print : ga -> unit
(* Runs tests on this Module *)
val run_tests : unit -> unit
end
module MakeImageGeneticAlgorithm (G : GUESS) : GENETIC_ALGORITHM with type guess = G.guess =
struct
type guess = G.guess
type ga = float * guess * color array array * float
let guess (_,gs,_,_) = gs
let get_best g = guess g
let target (_,_,img,_) = img
let std_dev (s,_,_,_) = s
let last_fitness (_,_,_,f) = f
(* Initializes the random guesses *)
let fresh s t _ m =
let width, height = get_width t, get_height t in
(s, G.fresh width height m, t, 0.)
let kill_phase g = g
let reproduction_phase g =
let mother = get_best g in
let daughter = G.asexual_reproduction (std_dev g) mother in
let fit_mom = last_fitness g in
let fit_daughter = G.fitness (target g) daughter in
printf "%f, %f\n" fit_mom fit_daughter;
if fit_daughter > fit_mom then (std_dev g, daughter, target g, fit_daughter)
else g
let rec evolve g n =
if n <= 0 then g
else evolve (reproduction_phase g) (n - 1)
let draw_best g = G.draw (get_best g)
let print ga =
print_endline "################### Start of Genetic Algorithm ###################";
(*Array.iter ~f:(G.print) (guesses ga); *)
printf "Fitness of best guess: %f" (G.fitness (target ga) (get_best ga));
print_endline "################### End of Genetic Algorithm ###################";
print_endline ""
let run_tests () =
(* Since this is a probabilistic model, we test by printing *)
(* let ga = fresh 10. 1 2 in
print (ga);
let evolved = evolve ga 10 in
print evolved;*)
()
end
(* Applies the functor to make a GeneticAlgorithm using our implementation of Guess *)
module GeneticAlgorithm : GENETIC_ALGORITHM = MakeImageGeneticAlgorithm(Guess)