1+ package com .baeldung .algorithms .ga .jenetics ;
2+
3+ import static java .util .Objects .requireNonNull ;
4+
5+ import java .util .function .Function ;
6+ import java .util .stream .Collectors ;
7+
8+ import org .jenetics .BitGene ;
9+ import org .jenetics .engine .Codec ;
10+ import org .jenetics .engine .Engine ;
11+ import org .jenetics .engine .EvolutionResult ;
12+ import org .jenetics .engine .Problem ;
13+ import org .jenetics .engine .codecs ;
14+ import org .jenetics .util .ISeq ;
15+
16+ public class SpringsteenProblem implements Problem <ISeq <SpringsteenRecord >, BitGene , Double > {
17+
18+ private ISeq <SpringsteenRecord > records ;
19+ private double maxPricePerUniqueSong ;
20+
21+ public SpringsteenProblem (ISeq <SpringsteenRecord > records , double maxPricePerUniqueSong ) {
22+ this .records = requireNonNull (records );
23+ this .maxPricePerUniqueSong = maxPricePerUniqueSong ;
24+ }
25+
26+ @ Override
27+ public Function <ISeq <SpringsteenRecord >, Double > fitness () {
28+ return SpringsteenRecords -> {
29+ double cost = SpringsteenRecords .stream ()
30+ .mapToDouble (r -> r .price )
31+ .sum ();
32+
33+ int uniqueSongCount = SpringsteenRecords .stream ()
34+ .flatMap (r -> r .songs .stream ())
35+ .collect (Collectors .toSet ())
36+ .size ();
37+
38+ double pricePerUniqueSong = cost / uniqueSongCount ;
39+
40+ return pricePerUniqueSong <= maxPricePerUniqueSong ? uniqueSongCount : 0.0 ;
41+ };
42+ }
43+
44+ @ Override
45+ public Codec <ISeq <SpringsteenRecord >, BitGene > codec () {
46+ return codecs .ofSubSet (records );
47+ }
48+
49+ public static void main (String [] args ) {
50+ double maxPricePerUniqueSong = 2.5 ;
51+
52+ SpringsteenProblem springsteen = new SpringsteenProblem (
53+ ISeq .of (new SpringsteenRecord ("SpringsteenRecord1" , 25 , ISeq .of ("Song1" , "Song2" , "Song3" , "Song4" , "Song5" , "Song6" )), new SpringsteenRecord ("SpringsteenRecord2" , 15 , ISeq .of ("Song2" , "Song3" , "Song4" , "Song5" , "Song6" , "Song7" )),
54+ new SpringsteenRecord ("SpringsteenRecord3" , 35 , ISeq .of ("Song5" , "Song6" , "Song7" , "Song8" , "Song9" , "Song10" )), new SpringsteenRecord ("SpringsteenRecord4" , 17 , ISeq .of ("Song9" , "Song10" , "Song12" , "Song4" , "Song13" , "Song14" )),
55+ new SpringsteenRecord ("SpringsteenRecord5" , 29 , ISeq .of ("Song1" , "Song2" , "Song13" , "Song14" , "Song15" , "Song16" )), new SpringsteenRecord ("SpringsteenRecord6" , 5 , ISeq .of ("Song18" , "Song20" , "Song30" , "Song40" ))),
56+ maxPricePerUniqueSong );
57+
58+ Engine <BitGene , Double > engine = Engine .builder (springsteen )
59+ .build ();
60+
61+ ISeq <SpringsteenRecord > result = springsteen .codec ()
62+ .decoder ()
63+ .apply (engine .stream ()
64+ .limit (10 )
65+ .collect (EvolutionResult .toBestGenotype ()));
66+
67+ double cost = result .stream ()
68+ .mapToDouble (r -> r .price )
69+ .sum ();
70+
71+ int uniqueSongCount = result .stream ()
72+ .flatMap (r -> r .songs .stream ())
73+ .collect (Collectors .toSet ())
74+ .size ();
75+
76+ double pricePerUniqueSong = cost / uniqueSongCount ;
77+
78+ System .out .println ("Overall cost: " + cost );
79+ System .out .println ("Unique songs: " + uniqueSongCount );
80+ System .out .println ("Cost per song: " + pricePerUniqueSong );
81+ System .out .println ("Records: " + result .map (r -> r .name )
82+ .toString (", " ));
83+
84+ }
85+
86+ }
0 commit comments