Skip to content

Commit

Permalink
Add the dynamic size limit fitness for regexp-search
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakarah committed May 30, 2017
1 parent ee33d61 commit 42fcc13
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
100 changes: 100 additions & 0 deletions config/regexp_search/dynamic_size_limit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"pop_size": 1000,
"growth_factor": 2.0,
"crossover_ratio": 0.5,
"mutation_ratio": 0.45,
"remove_duplicates": true,

"plugin_dir":"_build/plugins/regexp_search/",
"plugins":
[
"../selection/Tournament.cmxs",
"../parent_chooser/UniformParentChooser.cmxs",
"creation/RandomCreation.cmxs",
"creation/CreationFromExample.cmxs",
"mutation/RebuildSubtreeMutation.cmxs",
"mutation/ReduceMutation.cmxs",
"crossover/TakeSubtreeCrossover.cmxs",
"crossover/CombineCrossover.cmxs",
"fitness/MultiobjectiveFitness.cmxs",
"fitness/DynamicSizeLimitFitness.cmxs",
"simplification/RepeatSimplification.cmxs",
"simplification/GroupChars.cmxs"
],

"creation":
[
{
"proba":0.1, "method":"from_example",
"params":
{
"replacement_patterns":
[
{ "proba":0.5, "regexp":"[A-Z]" },
{ "proba":0.5, "regexp":"[a-z]" },
{ "proba":0.7, "regexp":"[0-9]" },
{ "proba":0.5, "regexp":"[ \t]" },
{ "proba":0.8, "regexp":"[A-Za-z]" },
{ "proba":0.2, "regexp":"." }
]
}
},
{ "proba":0.4, "method":"fill", "params":"&random_gen" },
{ "proba":0.5, "method":"grow", "params":"&random_gen" }
],
"mutation":
[
{ "proba":0.7, "method":"rebuild_subtree", "params":"&random_gen" },
{ "proba":0.3, "method":"reduce" }
],
"crossover":
[
{ "proba":0.9, "method":"take_subtree" },
{ "proba":0.1, "method":"combine", "params":{"alt_proba":0.1} }
],
"fitness":
{
"method":"dynamic_size_limit",
"params":
{
"full_example_score":10.0,
"full_counter_example_score":50.0,
"partial_example_score":5.0,
"partial_counter_example_score":0.0,
"size_score":1.0,
"elim_size":150,
"initial_size_limit":25
}
},
"simplifications":
[
{ "schedule":25, "method":"repeat_simplification" },
{ "schedule":50, "method":"group_chars" }
],
"selection" : { "method":"tournament" },
"parent_choice": { "method":"uniform" },

"&random_gen":
{
"min_init_depth": 3,
"max_init_depth": 5,
"max_depth": 10,

"concat_proba": 0.5,
"alt_proba": 0.05,
"opt_proba": 0.05,
"plus_proba": 0.05,
"star_proba": 0.05,
"rand_char_proba": 0.25,
"predefined_proba": 0.05,
"predefined_list":
[
{ "proba":0.2, "regexp":"." },
{ "proba":0.3, "regexp":"[A-Za-z]" },
{ "proba":0.1, "regexp":"[A-Z]" },
{ "proba":0.1, "regexp":"[a-z]" },
{ "proba":0.2, "regexp":"[0-9]" },
{ "proba":0.1, "regexp":"[ \t]" }
]
}
}
52 changes: 52 additions & 0 deletions plugins/regexp_search/fitness/DynamicSizeLimitFitness.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(** This fitness function is the same as MultiobjectiveFitness except it gives a null fitness value to all individuals bigger and not better than previous encountered ones with a dynamic size limit.
It uses internally the MultiobjectiveFitness plugin and accept the same parameters.
It is compatible with a static size threshold and is intended to prevent bloat. *)
let build_dynamic_size_limit_fitness json =
let open Yojson.Basic.Util in
let init_size_limit = json |> member "initial_size_limit" |> to_int in

let module MultiobjectiveFitness = (val RegexpSearchHooks.Fitness.get "multiobjective" json) in

(module struct
(** The fitness contains Evaluated with the real fitness value if the individual is small enough and Rejected if it is not. *)
type t =
| Rejected
| Evaluated of MultiobjectiveFitness.t
;;

type individual = RegexpTree.t
type target_data = ExampleList.t

let to_float = function
| Rejected -> 0.
| Evaluated fit -> MultiobjectiveFitness.to_float fit
;;

let to_string = function
| Rejected -> "Above dynamic size limit"
| Evaluated fit -> MultiobjectiveFitness.to_string fit
;;

let compare x y = Pervasives.compare (to_float x) (to_float y);;

let best_fitness = ref 0.;;
let size_limit = ref init_size_limit;;

let compute examples regexp =
let fitness = MultiobjectiveFitness.compute examples regexp in
let float_fitness = MultiobjectiveFitness.to_float fitness in
let size = RegexpTree.size regexp in
if float_fitness > !best_fitness then
(
best_fitness := float_fitness;
size_limit := max !size_limit size
);
if size > !size_limit then Rejected
else Evaluated fitness
;;
end : EvolParams.Fitness with type individual = RegexpTree.t and type target_data = ExampleList.t)
;;

let () =
RegexpSearchHooks.Fitness.register "dynamic_size_limit" build_dynamic_size_limit_fitness
;;
1 change: 1 addition & 0 deletions plugins/regexp_search/regexp_search.itarget
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ mutation/ReduceMutation.cmxs
fitness/FullMatchFitness.cmxs
fitness/PartialMatchFitness.cmxs
fitness/MultiobjectiveFitness.cmxs
fitness/DynamicSizeLimitFitness.cmxs
simplification/RepeatSimplification.cmxs
simplification/GroupChars.cmxs

0 comments on commit 42fcc13

Please sign in to comment.