$darkmode
DENOPTIM
PopulationTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.ga;
20
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertFalse;
23import static org.junit.jupiter.api.Assertions.assertNull;
24import static org.junit.jupiter.api.Assertions.assertTrue;
25
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.HashMap;
29import java.util.HashSet;
30import java.util.List;
31import java.util.Map;
32
33import org.junit.jupiter.api.Test;
34
35import denoptim.exception.DENOPTIMException;
36import denoptim.fragspace.FragmentSpace;
37import denoptim.fragspace.FragmentSpaceParameters;
38import denoptim.graph.APClass;
39import denoptim.graph.Candidate;
40import denoptim.graph.DGraph;
41import denoptim.graph.Edge;
42import denoptim.graph.Edge.BondType;
43import denoptim.graph.EmptyVertex;
44import denoptim.graph.Template;
45import denoptim.graph.Template.ContractLevel;
46import denoptim.graph.Vertex;
47import denoptim.graph.Vertex.BBType;
48import denoptim.programs.RunTimeParameters.ParametersType;
49import denoptim.programs.denovo.GAParameters;
50
51
58public class PopulationTest
59{
60 private static APClass APCA, APCB, APCC, APCD;
61
62//------------------------------------------------------------------------------
63
64 public static GAParameters prepare() throws DENOPTIMException
65 {
66 APCA = APClass.make("A", 0, BondType.SINGLE);
67 APCB = APClass.make("B", 0, BondType.SINGLE);
68 APCC = APClass.make("C", 0, BondType.SINGLE);
69 APCD = APClass.make("D", 99, BondType.DOUBLE);
70
71 HashMap<APClass,ArrayList<APClass>> cpMap =
72 new HashMap<APClass,ArrayList<APClass>>();
73 ArrayList<APClass> lstA = new ArrayList<APClass>();
74 lstA.add(APCA);
75 cpMap.put(APCA, lstA);
76 ArrayList<APClass> lstB = new ArrayList<APClass>();
77 lstB.add(APCB);
78 lstB.add(APCC);
79 cpMap.put(APCB, lstB);
80 ArrayList<APClass> lstC = new ArrayList<APClass>();
81 lstC.add(APCC);
82 lstC.add(APCB);
83 cpMap.put(APCC, lstC);
84 ArrayList<APClass> lstD = new ArrayList<APClass>();
85 lstD.add(APCD);
86 cpMap.put(APCD, lstD);
87
88
89 /* Compatibility matrix
90 *
91 * | A | B | C | D |
92 * -------------------------
93 * A | T | | | |
94 * -------------------------
95 * B | | T | T | |
96 * -------------------------
97 * C | | T | T | |
98 * -------------------------
99 * D | | | | T |
100 */
101
102 HashMap<APClass,APClass> capMap = new HashMap<APClass,APClass>();
103 HashSet<APClass> forbEnds = new HashSet<APClass>();
104
106 FragmentSpace fs = new FragmentSpace(fsp,
107 new ArrayList<Vertex>(),
108 new ArrayList<Vertex>(),
109 new ArrayList<Vertex>(),
110 cpMap, capMap, forbEnds, cpMap);
112
113 GAParameters gaSettings = new GAParameters();
114 gaSettings.readParameterLine(ParametersType.RC_PARAMS.getKeywordRoot()
115 + "CLOSERINGS");
116 gaSettings.setParameters(fsp);
117 return gaSettings;
118 }
119
120//------------------------------------------------------------------------------
121
122 @Test
123 public void testXOverCompatibility() throws Exception
124 {
125 GAParameters gaparams = prepare();
127 .getParameters(ParametersType.FS_PARAMS);
128 FragmentSpace fs = fsParams.getFragmentSpace();
129
130 Population pop = new Population(gaparams);
131
132 DGraph g1 = makeGraphA();
133 // We give uniquefying properties to the vertexes so that they
134 // are not seen as v.sameAs(o).
135 String k = "Uniquefying";
136 int counter = 0;
137 for (int i=0; i<g1.getVertexCount(); i++)
138 {
139 Vertex v = g1.getVertexAtPosition(i);
141 v.setProperty(k, counter);
142 counter++;
143 }
144 Candidate c1 = new Candidate("C1",g1);
145 pop.add(c1);
146
147 DGraph g2 = makeGraphB();
148 for (int i=0; i<g2.getVertexCount(); i++)
149 {
150 Vertex v = g2.getVertexAtPosition(i);
152 v.setProperty(k, counter);
153 counter++;
154 }
155 Candidate c2 = new Candidate("C2",g2);
156 pop.add(c2);
157
158 DGraph g3 = makeGraphB();
159 for (int i=0; i<g3.getVertexCount(); i++)
160 {
161 Vertex v = g3.getVertexAtPosition(i);
163 v.setProperty(k, counter);
164 counter++;
165 }
166 Candidate c3 = new Candidate("C3",g3);
167 pop.add(c3);
168
169 DGraph g4 = makeGraphC();
170 for (int i=0; i<g4.getVertexCount(); i++)
171 {
172 Vertex v = g4.getVertexAtPosition(i);
174 v.setProperty(k, counter);
175 counter++;
176 }
177 Candidate c4 = new Candidate("C4",g4);
178 pop.add(c4);
179
180 DGraph g5 = makeGraphD();
181 for (int i=0; i<g5.getVertexCount(); i++)
182 {
183 Vertex v = g5.getVertexAtPosition(i);
185 v.setProperty(k, counter);
186 counter++;
187 }
188 Candidate c5 = new Candidate("C5",g5);
189 pop.add(c5);
190
191 List<Candidate> partnersForC1 = pop.getXoverPartners(c1,
192 new ArrayList<Candidate>(Arrays.asList(c1,c2,c3,c4,c5)), fs);
193 List<Candidate> partnersForC2 = pop.getXoverPartners(c2,
194 new ArrayList<Candidate>(Arrays.asList(c1,c2,c3,c4,c5)), fs);
195 List<Candidate> partnersForC3 = pop.getXoverPartners(c3,
196 new ArrayList<Candidate>(Arrays.asList(c1,c2,c3,c4,c5)), fs);
197 List<Candidate> partnersForC4 = pop.getXoverPartners(c4,
198 new ArrayList<Candidate>(Arrays.asList(c1,c2,c3,c4,c5)), fs);
199 List<Candidate> partnersForC5 = pop.getXoverPartners(c5,
200 new ArrayList<Candidate>(Arrays.asList(c1,c2,c3,c4,c5)), fs);
201
202 Map<Candidate,Map<Candidate,Integer>> expected =
203 new HashMap<Candidate,Map<Candidate,Integer>>();
204 Map<Candidate,Integer> expectedForC1 = new HashMap<Candidate,Integer>();
205 expectedForC1.put(c2, 9);
206 expectedForC1.put(c3, 9);
207 expectedForC1.put(c4, 5);
208 expected.put(c1, expectedForC1);
209 Map<Candidate,Integer> expectedForC2 = new HashMap<Candidate,Integer>();
210 expectedForC2.put(c1, 9);
211 //expectedForC2.put(c3, 4); NO xover between candidates with same graph!
212 expectedForC2.put(c4, 2);
213 expected.put(c2, expectedForC2);
214 Map<Candidate,Integer> expectedForC3 = new HashMap<Candidate,Integer>();
215 expectedForC3.put(c1, 9);
216 //expectedForC3.put(c2, 4); NO xover between candidates with same graph!
217 expectedForC3.put(c4, 2);
218 expected.put(c3, expectedForC3);
219 Map<Candidate,Integer> expectedForC4 = new HashMap<Candidate,Integer>();
220 expectedForC4.put(c1, 5);
221 expectedForC4.put(c2, 2);
222 expectedForC4.put(c3, 2);
223 expected.put(c4, expectedForC4);
224
225 compareSizeOfSites(c1,expectedForC1,partnersForC1,pop);
226 compareSizeOfSites(c2,expectedForC2,partnersForC2,pop);
227 compareSizeOfSites(c3,expectedForC3,partnersForC3,pop);
228 compareSizeOfSites(c4,expectedForC4,partnersForC4,pop);
229 assertEquals(partnersForC5.size(), 0, "Wrong umber of partners for C5");
230 }
231
232//------------------------------------------------------------------------------
233
234 @Test
235 public void testClone() throws Exception
236 {
237 GAParameters gaparams = prepare();
239 .getParameters(ParametersType.FS_PARAMS);
240 FragmentSpace fs = fsParams.getFragmentSpace();
241
242 Population pop = new Population(gaparams);
243
244 DGraph g1 = makeGraphA();
245 Candidate c1 = new Candidate("C1",g1);
246 pop.add(c1);
247
248 DGraph g2 = makeGraphB();
249 Candidate c2 = new Candidate("C2",g2);
250 pop.add(c2);
251
252 //Adding property to uniquefy a pair of vertexes
253 String k = "Uniquefying";
255 g1.getVertexAtPosition(1).setProperty(k, 123);
257 g2.getVertexAtPosition(1).setProperty(k, 456);
258
259 List<Candidate> partnersForC1 = pop.getXoverPartners(c1,
260 new ArrayList<Candidate>(Arrays.asList(c1,c2)), fs);
261 List<Candidate> partnersForC2 = pop.getXoverPartners(c2,
262 new ArrayList<Candidate>(Arrays.asList(c1,c2)), fs);
263
264 Map<Candidate,Map<Candidate,Integer>> expected =
265 new HashMap<Candidate,Map<Candidate,Integer>>();
266 Map<Candidate,Integer> expectedForC1 = new HashMap<Candidate,Integer>();
267 expectedForC1.put(c2, 9);
268 expected.put(c1, expectedForC1);
269 Map<Candidate,Integer> expectedForC2 = new HashMap<Candidate,Integer>();
270 expectedForC2.put(c1, 9);
271 expected.put(c2, expectedForC2);
272
273 Population clonedPop = pop.clone();
274
275 compareSizeOfSites(c1,expectedForC1,partnersForC1,clonedPop);
276 compareSizeOfSites(c2,expectedForC2,partnersForC2,clonedPop);
277
279 clonedPop.getXoverSites(c1, c2));
280 }
281
282//------------------------------------------------------------------------------
283
287 private void compareSitesLists(List<XoverSite> listA,
288 List<XoverSite> listB)
289 {
290 for (int i=0; i<listA.size(); i++)
291 {
292 assertTrue(listA.get(i).equals(listB.get(i)));
293 }
294 }
295
296//------------------------------------------------------------------------------
297
298 private void compareSizeOfSites(Candidate parentA,
299 Map<Candidate, Integer> expectedForC1,
300 List<Candidate> partnersForC1, Population pop)
301 {
302 for (Candidate c : expectedForC1.keySet())
303 {
304 assertTrue(partnersForC1.contains(c), "Missing XOver compatibility "
305 + "between '" + parentA.getName() + "' and '" + c.getName()
306 + "'.");
307 assertEquals(expectedForC1.get(c).intValue(),
308 pop.getXoverSites(parentA, c).size(),
309 "Wrong number of sites between '" + parentA.getName()
310 + "' and '" + c.getName() + "'.");
311 }
312 }
313
314//------------------------------------------------------------------------------
315
325 {
326 DGraph graphA = new DGraph();
327 EmptyVertex v0 = new EmptyVertex(0);
329 v0.addAP(APCA);
330 v0.addAP(APCA);
331 graphA.addVertex(v0);
332 EmptyVertex v1 = new EmptyVertex(1);
333 v1.addAP(APCA);
334 v1.addAP(APCA);
335 graphA.addVertex(v1);
336 EmptyVertex v2 = new EmptyVertex(2);
337 v2.addAP(APCA);
338 v2.addAP(APCA);
339 graphA.addVertex(v2);
340 EmptyVertex v3 = new EmptyVertex(3);
341 v3.addAP(APCA);
342 v3.addAP(APCB);
343 graphA.addVertex(v3);
344 EmptyVertex v4 = new EmptyVertex(4);
345 v4.addAP(APCB);
346 v4.addAP(APCB);
347 graphA.addVertex(v4);
348 EmptyVertex v5 = new EmptyVertex(5);
349 v5.addAP(APCB);
350 v5.addAP(APCB);
351 graphA.addVertex(v5);
352
353 graphA.addEdge(new Edge(v0.getAP(1), v1.getAP(0)));
354 graphA.addEdge(new Edge(v1.getAP(1), v2.getAP(0)));
355 graphA.addEdge(new Edge(v2.getAP(1), v3.getAP(0)));
356 graphA.addEdge(new Edge(v3.getAP(1), v4.getAP(0)));
357 graphA.addEdge(new Edge(v4.getAP(1), v5.getAP(0)));
358
359 graphA.renumberGraphVertices();
360
361 return graphA;
362 }
363
364//------------------------------------------------------------------------------
365
374 {
375 DGraph graphB = new DGraph();
376 EmptyVertex v0 = new EmptyVertex(0);
378 v0.addAP(APCA);
379 graphB.addVertex(v0);
380 EmptyVertex v1 = new EmptyVertex(1);
381 v1.addAP(APCA);
382 v1.addAP(APCA);
383 graphB.addVertex(v1);
384 EmptyVertex v2 = new EmptyVertex(2);
385 v2.addAP(APCA);
386 graphB.addVertex(v2);
387
388 graphB.addEdge(new Edge(v0.getAP(0), v1.getAP(0)));
389 graphB.addEdge(new Edge(v1.getAP(1), v2.getAP(0)));
390
391 graphB.renumberGraphVertices();
392
393 return graphB;
394 }
395
396//------------------------------------------------------------------------------
397
406 {
407 DGraph graphC = new DGraph();
408 EmptyVertex v0 = new EmptyVertex(0);
410 v0.addAP(APCC);
411 v0.addAP(APCC);
412 graphC.addVertex(v0);
413 EmptyVertex v1 = new EmptyVertex(1);
414 v1.addAP(APCC);
415 v1.addAP(APCA);
416 graphC.addVertex(v1);
417 EmptyVertex v2 = new EmptyVertex(2);
418 v2.addAP(APCA);
419 graphC.addVertex(v2);
420
421 graphC.addEdge(new Edge(v0.getAP(1), v1.getAP(0)));
422 graphC.addEdge(new Edge(v1.getAP(1), v2.getAP(0)));
423
424 graphC.renumberGraphVertices();
425
426 return graphC;
427 }
428
429//------------------------------------------------------------------------------
430
439 {
440 DGraph graphD = new DGraph();
441 EmptyVertex v0 = new EmptyVertex(0);
443 v0.addAP(APCD);
444 graphD.addVertex(v0);
445 EmptyVertex v1 = new EmptyVertex(1);
446 v1.addAP(APCD);
447
448 graphD.addEdge(new Edge(v0.getAP(0), v1.getAP(0)));
449
450 graphD.renumberGraphVertices();
451
452 return graphD;
453 }
454
455//------------------------------------------------------------------------------
456
465 {
466 DGraph graphA = new DGraph();
467 EmptyVertex v0 = new EmptyVertex(0);
469 v0.addAP(APCB);
470 graphA.addVertex(v0);
471 EmptyVertex v1 = new EmptyVertex();
472 v1.addAP(APCB);
473 v1.addAP(APCA);
474 graphA.addVertex(v1);
475 EmptyVertex v2 = new EmptyVertex();
476 v2.addAP(APCA);
477 v2.addAP(APCB);
478 graphA.addVertex(v2);
479 EmptyVertex v3 = new EmptyVertex();
480 v3.addAP(APCB);
481 v3.addAP(APCA);
482 graphA.addVertex(v3);
483 EmptyVertex v4 = new EmptyVertex();
484 v4.addAP(APCA);
485 v4.addAP(APCB);
486 graphA.addVertex(v4);
487 EmptyVertex v5 = new EmptyVertex();
488 v5.addAP(APCB);
489 graphA.addVertex(v5);
490
491 graphA.addEdge(new Edge(v0.getAP(0), v1.getAP(0)));
492 graphA.addEdge(new Edge(v1.getAP(1), v2.getAP(0)));
493 graphA.addEdge(new Edge(v2.getAP(1), v3.getAP(0)));
494 graphA.addEdge(new Edge(v3.getAP(1), v4.getAP(0)));
495 graphA.addEdge(new Edge(v4.getAP(1), v5.getAP(0)));
496
497 return graphA;
498 }
499
500//------------------------------------------------------------------------------
501
511 {
512 DGraph graphA = new DGraph();
513 EmptyVertex v0 = new EmptyVertex(0);
515 v0.addAP(APCA);
516 v0.addAP(APCA);
517 graphA.addVertex(v0);
518 EmptyVertex v1 = new EmptyVertex(1);
519 v1.addAP(APCA);
520 v1.addAP(APCA);
521 graphA.addVertex(v1);
522 EmptyVertex v2 = new EmptyVertex(2);
523 v2.addAP(APCA);
524 v2.addAP(APCA);
525 graphA.addVertex(v2);
526 EmptyVertex v3 = new EmptyVertex(3);
527 v3.addAP(APCA);
528 v3.addAP(APCA);
529 graphA.addVertex(v3);
530 EmptyVertex v4 = new EmptyVertex(4);
531 v4.addAP(APCA);
532 v4.addAP(APCA);
533 graphA.addVertex(v4);
534 EmptyVertex v5 = new EmptyVertex(5);
535 v5.addAP(APCA);
536 v5.addAP(APCA);
537 graphA.addVertex(v5);
538 EmptyVertex v6 = new EmptyVertex(6);
539 v6.addAP(APCA);
540 v6.addAP(APCA);
541 graphA.addVertex(v6);
542 EmptyVertex v7 = new EmptyVertex(7);
543 v7.addAP(APCA);
544 v7.addAP(APCA);
545 graphA.addVertex(v7);
546
547 graphA.addEdge(new Edge(v0.getAP(1), v1.getAP(0)));
548 graphA.addEdge(new Edge(v1.getAP(1), v2.getAP(0)));
549 graphA.addEdge(new Edge(v2.getAP(1), v3.getAP(0)));
550 graphA.addEdge(new Edge(v3.getAP(1), v4.getAP(0)));
551 graphA.addEdge(new Edge(v4.getAP(1), v5.getAP(0)));
552 graphA.addEdge(new Edge(v5.getAP(1), v6.getAP(0)));
553 graphA.addEdge(new Edge(v6.getAP(1), v7.getAP(0)));
554
555 graphA.renumberGraphVertices();
556
557 return graphA;
558 }
559
560//------------------------------------------------------------------------------
561
562 @Test
563 public void testGetMinMax() throws Exception
564 {
565 double trsh = 0.001;
566 GAParameters gaparams = prepare();
567 Population pop = new Population(gaparams);
568
569 DGraph g1 = makeGraphA();
570 Candidate c1 = new Candidate("C1",g1);
571 c1.setFitness(-1.0);
572 pop.add(c1);
573
574 DGraph g2 = makeGraphB();
575 Candidate c2 = new Candidate("C2",g2);
576 c2.setFitness(0.02);
577 pop.add(c2);
578
579 DGraph g3 = makeGraphB();
580 Candidate c3 = new Candidate("C3",g3);
581 c3.setFitness(2.0);
582 pop.add(c3);
583
584 DGraph g4 = makeGraphC();
585 Candidate c4 = new Candidate("C4",g4);
586 c4.setFitness(0.5);
587 pop.add(c4);
588
589 DGraph g5 = makeGraphD();
590 Candidate c5 = new Candidate("C5",g5);
591 c5.setFitness(-0.5);
592 pop.add(c5);
593
594 assertTrue(Math.abs(pop.getMinFitness()-(-1.0)) < trsh,
595 "getting min fitness");
596 assertTrue(Math.abs(pop.getMaxFitness()-(2.0)) < trsh,
597 "getting max fitness");
598 }
599
600//------------------------------------------------------------------------------
601
602 @Test
603 public void testIsInPercentile() throws Exception
604 {
605 GAParameters gaparams = prepare();
606 Population pop = new Population(gaparams);
607
608 DGraph g1 = makeGraphA();
609 Candidate c1 = new Candidate("C1",g1);
610 c1.setFitness(20.0);
611 pop.add(c1);
612
613 DGraph g2 = makeGraphB();
614 Candidate c2 = new Candidate("C2",g2);
615 c2.setFitness(120);
616 pop.add(c2);
617
618 assertTrue(pop.isWithinPercentile(116, 0.05), "116 is in 5%");
619 assertTrue(pop.isWithinPercentile(70.1, 0.5), "70.1 is in 50%");
620 assertFalse(pop.isWithinPercentile(69, 0.5), "69 is not in 50%");
621 assertFalse(pop.isWithinPercentile(114, 0.05), "114 is not in 5%");
622
623 pop = new Population(gaparams);
624
625 DGraph g1b = makeGraphA();
626 Candidate c1b = new Candidate("C1",g1b);
627 c1b.setFitness(-20.0);
628 pop.add(c1b);
629
630 DGraph g2b = makeGraphB();
631 Candidate c2b = new Candidate("C2",g2b);
632 c2b.setFitness(80);
633 pop.add(c2b);
634
635 assertTrue(pop.isWithinPercentile(76, 0.05), "76 is in 5%");
636 assertTrue(pop.isWithinPercentile(30.1, 0.5), "30.1 is in 50%");
637 assertFalse(pop.isWithinPercentile(29, 0.5), "29 is not in 50%");
638 assertFalse(pop.isWithinPercentile(74, 0.05), "74 is not in 5%");
639 }
640
641//------------------------------------------------------------------------------
642
643 @Test
644 public void testPopulationVersion() throws Exception
645 {
646 GAParameters gaparams = prepare();
647 Population pop = new Population(gaparams);
648 int v0 = pop.getVersionID();
649
650 DGraph g1 = makeGraphA();
651 Candidate c1 = new Candidate("C1",g1);
652 pop.add(c1);
653 int v1 = pop.getVersionID();
654 assertTrue(v1>v0,"Version change 1");
655
656 DGraph g2 = makeGraphB();
657 Candidate c2 = new Candidate("C2",g2);
658 pop.add(c2);
659 int v2 = pop.getVersionID();
660 assertTrue(v2>v1,"Version change 2");
661
662 DGraph g3 = makeGraphB();
663 Candidate c3 = new Candidate("C3",g3);
664 pop.add(c3);
665 int v3 = pop.getVersionID();
666 assertTrue(v3>v2,"Version change 3");
667
668 pop.remove(c1);
669 int v4 = pop.getVersionID();
670 assertTrue(v4>v3,"Version change 4");
671
672 DGraph g4 = makeGraphB();
673 Candidate c4 = new Candidate("C4",g4);
674 pop.add(0,c4);
675 int v5 = pop.getVersionID();
676 assertTrue(v5>v4,"Version change 5");
677
678 DGraph g5 = makeGraphC();
679 Candidate c5 = new Candidate("C5",g5);
680 pop.set(1,c5);
681 int v6 = pop.getVersionID();
682 assertTrue(v6>v5,"Version change 6");
683 }
684
685//------------------------------------------------------------------------------
686
736 public static DGraph[] getPairOfTestGraphsB() throws Exception
737 {
738 // Prepare special building block: template T1
739 EmptyVertex v0 = new EmptyVertex(0);
740 v0.addAP(APCA);
741 v0.addAP(APCA);
742 v0.addAP(APCA);
743 v0.setProperty("Label", "tv0");
744
745 EmptyVertex v1 = new EmptyVertex(1);
746 v1.addAP(APCA);
747 v1.addAP(APCA);
748 v1.addAP(APCB);
749 v1.setProperty("Label", "tv1");
750
751 EmptyVertex v2 = new EmptyVertex(2);
752 v2.addAP(APCB);
753 v2.addAP(APCC);
754 v2.setProperty("Label", "tv2");
755
756 EmptyVertex v3 = new EmptyVertex(3);
757 v3.addAP(APCA);
758 v3.addAP(APCA);
759 v3.setProperty("Label", "tv3");
760
761 EmptyVertex v4 = new EmptyVertex(4);
762 v4.addAP(APCA);
763 v4.addAP(APCB);
764 v4.addAP(APCA);
765 v4.setProperty("Label", "tv4");
766
767 EmptyVertex v5 = new EmptyVertex(5);
768 v5.addAP(APCA);
769 v5.setProperty("Label", "tv5");
770 v5.setAsRCV(true);
771
772 EmptyVertex v6 = new EmptyVertex(6);
773 v6.addAP(APCA);
774 v6.setProperty("Label", "tv6");
775 v6.setAsRCV(true);
776
777 DGraph g = new DGraph();
778 g.addVertex(v0);
779 g.setGraphId(-1);
780 g.appendVertexOnAP(v0.getAP(1), v1.getAP(0));
781 g.appendVertexOnAP(v0.getAP(0), v3.getAP(0));
782 g.appendVertexOnAP(v3.getAP(1), v4.getAP(0));
783 g.appendVertexOnAP(v4.getAP(2), v6.getAP(0));
784 g.appendVertexOnAP(v1.getAP(2), v2.getAP(0));
785 g.appendVertexOnAP(v1.getAP(1), v5.getAP(0));
786 g.addRing(v5, v6, BondType.SINGLE);
787
788 Template t1 = new Template(BBType.NONE);
789 t1.setInnerGraph(g);
790 t1.setProperty("Label", "t1");
792
793 // Assemble the first graph: graphA
794 EmptyVertex m0 = new EmptyVertex(100);
795 m0.addAP(APCA);
796 m0.setProperty("Label", "m100");
797
798 EmptyVertex m1 = new EmptyVertex(101);
799 m1.addAP(APCC);
800 m1.setProperty("Label", "m101");
801
802 EmptyVertex m2 = new EmptyVertex(102);
803 m2.addAP(APCB);
804 m2.addAP(APCC);
805 m2.setProperty("Label", "m102");
806
807 EmptyVertex m3 = new EmptyVertex(103);
808 m3.addAP(APCC);
809 m3.setProperty("Label", "m103");
810
811 DGraph graphA = new DGraph();
812 graphA.addVertex(m0);
813 graphA.appendVertexOnAP(m0.getAP(0), t1.getAP(0)); // A on T1
814 graphA.appendVertexOnAP(t1.getAP(1), m1.getAP(0)); // C on T1
815 graphA.appendVertexOnAP(t1.getAP(2), m2.getAP(0)); // B on T1
816 graphA.appendVertexOnAP(m2.getAP(1), m3.getAP(0));
817 graphA.setGraphId(11111);
818
819 // Done with GraphA!
820
821 // Prepare special building block: template T2
822 EmptyVertex w0 = new EmptyVertex(0);
823 w0.addAP(APCA);
824 w0.addAP(APCA);
825 w0.addAP(APCA);
826 w0.setProperty("Label", "tw0");
827
828 EmptyVertex w1 = new EmptyVertex(1);
829 w1.addAP(APCA);
830 w1.addAP(APCB);
831 w1.setProperty("Label", "tw1");
832
833 EmptyVertex w2 = new EmptyVertex(2);
834 w2.addAP(APCB);
835 w2.setProperty("Label", "tw2");
836
837 EmptyVertex w3 = new EmptyVertex(3);
838 w3.addAP(APCA);
839 w3.addAP(APCA);
840 w3.setProperty("Label", "tw3");
841
842 EmptyVertex w4 = new EmptyVertex(4);
843 w4.addAP(APCA);
844 w4.addAP(APCB);
845 w4.addAP(APCA);
846 w4.setProperty("Label", "tw4");
847
848 EmptyVertex w5 = new EmptyVertex(5);
849 w5.addAP(APCA);
850 w5.setProperty("Label", "tw5");
851
852 DGraph g2 = new DGraph();
853 g2.addVertex(w0);
854 g2.setGraphId(-1);
855 g2.appendVertexOnAP(w0.getAP(1), w1.getAP(0));
856 g2.appendVertexOnAP(w1.getAP(1), w2.getAP(0));
857 g2.appendVertexOnAP(w0.getAP(0), w3.getAP(0));
858 g2.appendVertexOnAP(w3.getAP(1), w4.getAP(0));
859 g2.appendVertexOnAP(w4.getAP(2), w5.getAP(0));
860
861 Template t2 = new Template(BBType.NONE);
862 t2.setInnerGraph(g2);
863 t2.setProperty("Label", "t2");
865
866 // Assemble the first graph: graphA
867 EmptyVertex n0 = new EmptyVertex(200);
868 n0.addAP(APCA);
869 n0.setProperty("Label", "m200");
870
871 EmptyVertex n2 = new EmptyVertex(202);
872 n2.addAP(APCB);
873 n2.addAP(APCC);
874 n2.setProperty("Label", "m202");
875
876 EmptyVertex n3 = new EmptyVertex(203);
877 n3.addAP(APCC);
878 n3.setProperty("Label", "m203");
879
880 DGraph graphB = new DGraph();
881 graphB.addVertex(n0);
882 graphB.appendVertexOnAP(n0.getAP(0), t2.getAP(0)); // A on T2
883 graphB.appendVertexOnAP(t2.getAP(1), n2.getAP(0)); // B on T2
884 graphB.appendVertexOnAP(n2.getAP(1), n3.getAP(0));
885 graphB.setGraphId(22222);
886
887 // Done with GraphB!
888
889 DGraph[] pair = new DGraph[2];
890 pair[0] = graphA;
891 pair[1] = graphB;
892
893 return pair;
894 }
895
896//------------------------------------------------------------------------------
897
945 public static DGraph[] getPairOfTestGraphsBxo() throws Exception
946 {
947 // Prepare special building block: template T1
948 EmptyVertex v0 = new EmptyVertex(0);
949 v0.addAP(APCA);
950 v0.addAP(APCA);
951 v0.addAP(APCA);
952 v0.setProperty("Label", "tv0");
953
954 EmptyVertex v1 = new EmptyVertex(1);
955 v1.addAP(APCA);
956 v1.addAP(APCA);
957 v1.setProperty("Label", "tv1");
958
959 EmptyVertex v2 = new EmptyVertex(2);
960 v2.addAP(APCA);
961 v2.addAP(APCB);
962 v2.addAP(APCA);
963 v2.setProperty("Label", "tv2");
964
965 EmptyVertex v3 = new EmptyVertex(3);
966 v3.addAP(APCA);
967 v3.setProperty("Label", "tv3");
968 v3.setAsRCV(true);
969
970 EmptyVertex v4 = new EmptyVertex(4);
971 v4.addAP(APCA);
972 v4.addAP(APCB);
973 v4.addAP(APCA);
974 v4.setProperty("Label", "tv4");
975
976 EmptyVertex v5 = new EmptyVertex(5);
977 v5.addAP(APCA);
978 v5.setProperty("Label", "tv5");
979
980 DGraph g = new DGraph();
981 g.addVertex(v0);
982 g.setGraphId(-1);
983 g.appendVertexOnAP(v0.getAP(0), v1.getAP(0));
984 g.appendVertexOnAP(v1.getAP(1), v2.getAP(0));
985 g.appendVertexOnAP(v2.getAP(2), v3.getAP(0));
986 g.appendVertexOnAP(v0.getAP(1), v4.getAP(0));
987 g.appendVertexOnAP(v4.getAP(2), v5.getAP(0));
988
989 Template t1 = new Template(BBType.NONE);
990 t1.setInnerGraph(g);
991 t1.setProperty("Label", "t1");
993
994 // Assemble the first graph: graphA
995 EmptyVertex m0 = new EmptyVertex(100);
996 m0.addAP(APCA);
997 m0.setProperty("Label", "m100");
998
999 EmptyVertex m1 = new EmptyVertex(101);
1000 m1.addAP(APCC);
1001 m1.setProperty("Label", "m101");
1002
1003 EmptyVertex m2 = new EmptyVertex(102);
1004 m2.addAP(APCB);
1005 m2.addAP(APCC);
1006 m2.setProperty("Label", "m102");
1007
1008 EmptyVertex m3 = new EmptyVertex(103);
1009 m3.addAP(APCC);
1010 m3.setProperty("Label", "m103");
1011
1012 DGraph graphA = new DGraph();
1013 graphA.addVertex(m0);
1014 graphA.appendVertexOnAP(m0.getAP(0), t1.getAP(0)); // A on T1
1015 graphA.appendVertexOnAP(t1.getAP(1), m1.getAP(0)); // B on T1
1016 graphA.appendVertexOnAP(t1.getAP(2), m2.getAP(0)); // B on T1
1017 graphA.appendVertexOnAP(m2.getAP(1), m3.getAP(0));
1018 graphA.setGraphId(33333);
1019
1020 // Done with GraphA!
1021
1022 // Prepare special building block: template T2
1023 EmptyVertex w0 = new EmptyVertex(0);
1024 w0.addAP(APCA);
1025 w0.addAP(APCA);
1026 w0.addAP(APCA);
1027 w0.setProperty("Label", "tw0");
1028
1029 EmptyVertex w1 = new EmptyVertex(1);
1030 w1.addAP(APCA);
1031 w1.addAP(APCB);
1032 w1.setProperty("Label", "tw1");
1033
1034 EmptyVertex w2 = new EmptyVertex(2);
1035 w2.addAP(APCB);
1036 w2.setProperty("Label", "tw2");
1037
1038 EmptyVertex w3 = new EmptyVertex(3);
1039 w3.addAP(APCA);
1040 w3.addAP(APCA);
1041 w3.setProperty("Label", "tw3");
1042
1043 EmptyVertex w4 = new EmptyVertex(4);
1044 w4.addAP(APCA);
1045 w4.addAP(APCA);
1046 w4.addAP(APCB);
1047 w4.setProperty("Label", "tw4");
1048
1049 EmptyVertex w5 = new EmptyVertex(5);
1050 w5.addAP(APCA);
1051 w5.setProperty("Label", "tw5");
1052 w5.setAsRCV(true);
1053
1054 EmptyVertex w6 = new EmptyVertex(6);
1055 w6.addAP(APCB);
1056 w6.addAP(APCC);
1057 w6.setProperty("Label", "tw6");
1058
1059 DGraph g2 = new DGraph();
1060 g2.addVertex(w0);
1061 g2.setGraphId(-1);
1062 g2.appendVertexOnAP(w0.getAP(1), w1.getAP(0));
1063 g2.appendVertexOnAP(w1.getAP(1), w2.getAP(0));
1064 g2.appendVertexOnAP(w0.getAP(0), w3.getAP(0));
1065 g2.appendVertexOnAP(w3.getAP(1), w4.getAP(0));
1066 g2.appendVertexOnAP(w4.getAP(1), w5.getAP(0));
1067 g2.appendVertexOnAP(w4.getAP(2), w6.getAP(0));
1068
1069 Template t2 = new Template(BBType.NONE);
1070 t2.setInnerGraph(g2);
1071 t2.setProperty("Label", "tw2");
1073
1074 // Assemble the first graph: graphA
1075 EmptyVertex n0 = new EmptyVertex(200);
1076 n0.addAP(APCA);
1077 n0.setProperty("Label", "m400");
1078
1079 EmptyVertex n2 = new EmptyVertex(202);
1080 n2.addAP(APCB);
1081 n2.addAP(APCC);
1082 n2.setProperty("Label", "m402");
1083
1084 EmptyVertex n3 = new EmptyVertex(203);
1085 n3.addAP(APCC);
1086 n3.setProperty("Label", "m403");
1087
1088 DGraph graphB = new DGraph();
1089 graphB.addVertex(n0);
1090 graphB.appendVertexOnAP(n0.getAP(0), t2.getAP(0)); // A on T2
1091 graphB.appendVertexOnAP(t2.getAP(1), n2.getAP(0)); // B on T2
1092 graphB.appendVertexOnAP(n2.getAP(1), n3.getAP(0));
1093 graphB.setGraphId(44444);
1094
1095 // Done with GraphB!
1096
1097 DGraph[] pair = new DGraph[2];
1098 pair[0] = graphA;
1099 pair[1] = graphB;
1100
1101 return pair;
1102 }
1103
1104//------------------------------------------------------------------------------
1105
1159 public static DGraph[] getPairOfTestGraphsBxoxo() throws Exception
1160 {
1161 // Prepare special building block: template T1
1162 // In the template we want APS in this order:
1163 // A(from v0), B(from v2), C(from v6)
1164
1165 EmptyVertex v0 = new EmptyVertex(0);
1166 v0.addAP(APCA);
1167 v0.addAP(APCA);
1168 v0.addAP(APCA);
1169 v0.setProperty("Label", "tv0");
1170
1171 EmptyVertex v1 = new EmptyVertex(1);
1172 v1.addAP(APCA);
1173 v1.addAP(APCA);
1174 v1.setProperty("Label", "tv1");
1175
1176 EmptyVertex v2 = new EmptyVertex(2);
1177 v2.addAP(APCA);
1178 v2.addAP(APCB);
1179 v2.addAP(APCA);
1180 v2.setProperty("Label", "tv2");
1181
1182 EmptyVertex v3 = new EmptyVertex(3);
1183 v3.addAP(APCA);
1184 v3.setProperty("Label", "tv3");
1185 v3.setAsRCV(true);
1186
1187 EmptyVertex v4 = new EmptyVertex(4);
1188 v4.addAP(APCA);
1189 v4.addAP(APCA);
1190 v4.addAP(APCB);
1191 v4.setProperty("Label", "tv4");
1192
1193 EmptyVertex v5 = new EmptyVertex(5);
1194 v5.addAP(APCA);
1195 v5.setProperty("Label", "tv5");
1196 v5.setAsRCV(true);
1197
1198 EmptyVertex v6 = new EmptyVertex(6);
1199 v6.addAP(APCB);
1200 v6.addAP(APCC);
1201 v6.setProperty("Label", "tv6");
1202
1203 DGraph g = new DGraph();
1204 g.addVertex(v0);
1205 g.setGraphId(-1);
1206 g.appendVertexOnAP(v0.getAP(0), v1.getAP(0));
1207 g.appendVertexOnAP(v1.getAP(1), v2.getAP(0));
1208 g.appendVertexOnAP(v2.getAP(2), v3.getAP(0));
1209 g.appendVertexOnAP(v0.getAP(1), v4.getAP(0));
1210 g.appendVertexOnAP(v4.getAP(2), v6.getAP(0));
1211 g.appendVertexOnAP(v4.getAP(1), v5.getAP(0));
1212 g.addRing(v5, v3, BondType.SINGLE);
1213
1214 Template t1 = new Template(BBType.NONE);
1215 t1.setInnerGraph(g);
1216 t1.setProperty("Label", "t1");
1218
1219 // Assemble the first graph: graphA
1220 EmptyVertex m0 = new EmptyVertex(100);
1221 m0.addAP(APCA);
1222 m0.setProperty("Label", "m100");
1223
1224 EmptyVertex m1 = new EmptyVertex(101);
1225 m1.addAP(APCC);
1226 m1.setProperty("Label", "m101");
1227
1228 EmptyVertex m2 = new EmptyVertex(102);
1229 m2.addAP(APCB);
1230 m2.addAP(APCC);
1231 m2.setProperty("Label", "m102");
1232
1233 EmptyVertex m3 = new EmptyVertex(103);
1234 m3.addAP(APCC);
1235 m3.setProperty("Label", "m103");
1236
1237 DGraph graphA = new DGraph();
1238 graphA.addVertex(m0);
1239 graphA.appendVertexOnAP(m0.getAP(0), t1.getAP(0)); // A on T1
1240 graphA.appendVertexOnAP(t1.getAP(1), m1.getAP(0)); // B on T1
1241 graphA.appendVertexOnAP(t1.getAP(2), m2.getAP(0)); // C on T1
1242 graphA.appendVertexOnAP(m2.getAP(1), m3.getAP(0));
1243 graphA.setGraphId(55555);
1244
1245 // Done with GraphA!
1246
1247 // GraphB is the same as from getPairOfTestGraphsB()
1248 DGraph[] initialGraphs = getPairOfTestGraphsB();
1249 DGraph graphB = initialGraphs[1];
1250 graphB.setGraphId(66666);
1251
1252 // Done with GraphB!
1253
1254 DGraph[] pair = new DGraph[2];
1255 pair[0] = graphA;
1256 pair[1] = graphB;
1257
1258 return pair;
1259 }
1260
1261//------------------------------------------------------------------------------
1262
1263}
Class defining a space of building blocks.
void setAPclassBasedApproach(boolean useAPC)
Set the fragment space to behave according to APClass-based approach.
Parameters defining the fragment space.
A collection of candidates.
Definition: Population.java:48
double getMinFitness()
Gets the minimum value of the fitness in this population.
List< XoverSite > getXoverSites(Candidate parentA, Candidate parentB)
Returns a list of crossover sites between the two given parents.
List< Candidate > getXoverPartners(Candidate memberA, List< Candidate > eligibleParents, FragmentSpace fragSpace)
Returns a list of population members that can do crossover with the specified member.
int getVersionID()
Returns an integer that represent the current status of the population.
boolean isWithinPercentile(double value, double percentile)
Checks if a given fitness value if within the given percentile of best candidates.
boolean add(Candidate c)
Definition: Population.java:87
Population clone()
Does not clone the cross-over compatibility relations between each pairs of population members.
Candidate set(int index, Candidate c)
Candidate remove(int index)
double getMaxFitness()
Gets the maximum value of the fitness in this population.
static DGraph makeGraphD()
Produced a graph like this:
static DGraph makeGraphC()
Produced a graph like this:
static GAParameters prepare()
static DGraph makeGraphB()
Produced a graph like this:
static DGraph[] getPairOfTestGraphsBxoxo()
Builds a pair of graphs that contain templates with ContractLevel#FREE contract.
void compareSizeOfSites(Candidate parentA, Map< Candidate, Integer > expectedForC1, List< Candidate > partnersForC1, Population pop)
void compareSitesLists(List< XoverSite > listA, List< XoverSite > listB)
Assumes the two lists have equal size.
static DGraph makeGraphE()
Produced a graph like this:
static DGraph makeGraphF()
Produced a graph like this:
static DGraph[] getPairOfTestGraphsB()
Builds a pair of graphs that contain templates with ContractLevel#FREE contract.
static DGraph[] getPairOfTestGraphsBxo()
Builds a pair of graphs that contain templates with ContractLevel#FREE contract.
static DGraph makeGraphA()
Produced a graph like this:
static APClass make(String ruleAndSubclass)
Creates an APClass if it does not exist already, or returns the reference to the existing instance.
Definition: APClass.java:136
A candidate is the combination of a denoptim graph with molecular representation and may include also...
Definition: Candidate.java:40
void setFitness(double fitness)
Definition: Candidate.java:480
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
void addVertex(Vertex vertex)
Appends a vertex to this graph without creating any edge.
Definition: DGraph.java:1097
void setGraphId(int id)
Definition: DGraph.java:258
Vertex getVertexAtPosition(int pos)
Returns the vertex that is in the given position of the list of vertices belonging to this graph.
Definition: DGraph.java:2514
void appendVertexOnAP(AttachmentPoint srcAP, AttachmentPoint trgAP)
Append a vertex to this graph: adds the new vertex to the list of vertices belonging to the graph,...
Definition: DGraph.java:5776
void renumberGraphVertices()
Reassign vertex IDs to all vertices of this graph.
Definition: DGraph.java:5283
void addRing(Ring ring)
Definition: DGraph.java:1030
void addEdge(Edge edge)
Adds the edge to the list of edges belonging to this graph.
Definition: DGraph.java:1021
This class represents the edge between two vertices.
Definition: Edge.java:38
An empty vertex has the behaviors of a vertex, but has no molecular structure.
void addAP()
Adds an attachment point with no APClass or other attribute.
void setInnerGraph(DGraph innerGraph)
Definition: Template.java:298
void setContractLevel(ContractLevel contract)
Imposes the given contract to this template.
Definition: Template.java:214
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
void setAsRCV(boolean isRCV)
Definition: Vertex.java:254
void setUniquefyingProperty(String key)
Add the given key among the properties that are checked for equality when comparing vertices with the...
Definition: Vertex.java:1112
void setBuildingBlockType(Vertex.BBType buildingBlockType)
Definition: Vertex.java:305
void setProperty(Object key, Object property)
Definition: Vertex.java:1148
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Definition: Vertex.java:920
void setParameters(RunTimeParameters otherParams)
Parameters for genetic algorithm.
Possible chemical bond types an edge can represent.
Definition: Edge.java:303
Enum specifying to what extent the template's inner graph can be changed.
Definition: Template.java:104
FREE
Inner graphs are free to change within the confines of the required AttachmentPoints.
Definition: Template.java:109
The type of building block.
Definition: Vertex.java:86
FS_PARAMS
Parameters pertaining the definition of the fragment space.
RC_PARAMS
Parameters pertaining to ring closures in graphs.