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