@@ -8,22 +8,6 @@ function generate_population(icn, pop_size)
88 return population
99end
1010
11- """
12- loss(X, X_sols, icn, weigths, metric)
13- Compute the loss of `icn`.
14- """
15- function loss (solutions, non_sltns, icn, weigths, metric, dom_size, param; samples= nothing )
16- compo = compose (icn, weigths)
17- f = composition (compo)
18- X = if isnothing (samples)
19- Iterators. flatten ((solutions, non_sltns))
20- else
21- Iterators. flatten ((solutions, rand (non_sltns, samples)))
22- end
23- σ = sum (x -> abs (f (x; param, dom_size) - metric (x, solutions)), X) + regularization (icn)
24- return σ
25- end
26-
2711"""
2812 _optimize!(icn, X, X_sols; metric = hamming, pop_size = 200)
2913Optimize and set the weigths of an ICN with a given set of configuration `X` and solutions `X_sols`.
@@ -40,11 +24,18 @@ function _optimize!(
4024 samples= nothing ,
4125 memoize= false ,
4226)
43- _metric = memoize ? (@memoize Dict memoize_metric (x, X) = metric (x, X)) : metric
44- _bias = memoize ? (@memoize Dict memoize_bias (x) = weigths_bias (x)) : weigths_bias
45- fitness =
46- w ->
47- loss (solutions, non_sltns, icn, w, _metric, dom_size, param; samples) + _bias (w)
27+ inplace = zeros (dom_size, max_icn_length ())
28+ _non_sltns = isnothing (samples) ? non_sltns : rand (non_sltns, samples)
29+
30+ function fitness (w)
31+ compo = compose (icn, w)
32+ f = composition (compo)
33+ S = Iterators. flatten ((solutions, _non_sltns))
34+ return sum (x -> abs (f (x; X= inplace, param, dom_size) - metric (x, solutions)), S) +
35+ regularization (icn) +
36+ weigths_bias (w)
37+ end
38+ _fitness = memoize ? (@memoize Dict memoize_fitness (w) = fitness (w)) : fitness
4839
4940 _icn_ga = GA (;
5041 populationSize= pop_size,
@@ -57,7 +48,7 @@ function _optimize!(
5748 )
5849
5950 pop = generate_population (icn, pop_size)
60- r = Evolutionary. optimize (fitness , pop, _icn_ga, Evolutionary. Options (; iterations))
51+ r = Evolutionary. optimize (_fitness , pop, _icn_ga, Evolutionary. Options (; iterations))
6152 return weights! (icn, Evolutionary. minimizer (r))
6253end
6354
0 commit comments