$darkmode
DENOPTIM
XoverSiteTest.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;
23
24import java.util.ArrayList;
25import java.util.List;
26
27import org.junit.jupiter.api.Test;
28
29import denoptim.graph.AttachmentPoint;
30import denoptim.graph.DGraph;
31import denoptim.graph.DGraphTest;
32import denoptim.graph.Template;
33import denoptim.graph.Vertex;
34import denoptim.utils.CrossoverType;
35
42public class XoverSiteTest
43{
44
45//------------------------------------------------------------------------------
46
47 @Test
48 public void testProjectToClonedGraphs() throws Exception
49 {
53
54 // This works only because we know that the graphs have only one
55 // template per level
56 List<Template> pathA = new ArrayList<Template>();
57 List<Template> pathB = new ArrayList<Template>();
58 DGraph refToThisLayerGraphB = gB;
59 Template refToThisLayerVrtxB = null;
60 DGraph refToThisLayerGraphA = gA;
61 Template refToThisLayerVrtxA = null;
62 for (int embeddingLevel=0; embeddingLevel<9; embeddingLevel++)
63 {
64 refToThisLayerVrtxA = (Template) refToThisLayerGraphA
65 .getVertexList().stream()
66 .filter(v -> v instanceof Template)
67 .findAny()
68 .orElse(null);
69 pathA.add(refToThisLayerVrtxA);
70 refToThisLayerGraphA = refToThisLayerVrtxA.getInnerGraph();
71
72 refToThisLayerVrtxB = (Template) refToThisLayerGraphB
73 .getVertexList().stream()
74 .filter(v -> v instanceof Template)
75 .findAny()
76 .orElse(null);
77 pathB.add(refToThisLayerVrtxB);
78 refToThisLayerGraphB = refToThisLayerVrtxB.getInnerGraph();
79 }
80
81 DGraph gEmeddedA = pathA.get(8).getInnerGraph();
82 DGraph gEmeddedB = pathB.get(8).getInnerGraph();
83
84 List<Vertex> lstA = new ArrayList<Vertex>();
85 lstA.add(gEmeddedA.getVertexAtPosition(1));
86 lstA.add(gEmeddedA.getVertexAtPosition(2));
87 List<AttachmentPoint> lstNeedyAPsA =
88 new ArrayList<AttachmentPoint>();
89 lstNeedyAPsA.add(lstA.get(0).getAP(2));
90 List<Vertex> lstB = new ArrayList<Vertex>();
91 lstB.add(gEmeddedB.getVertexAtPosition(1));
92 lstB.add(gEmeddedB.getVertexAtPosition(2));
93 List<AttachmentPoint> lstNeedyAPsB =
94 new ArrayList<AttachmentPoint>();
95 lstNeedyAPsB.add(lstB.get(0).getAP(2));
96
97 XoverSite xos = new XoverSite(lstA, lstNeedyAPsA, lstB, lstNeedyAPsB,
99
100 XoverSite xosOnClone = xos.projectToClonedGraphs();
101
102 assertFalse(xos.getA().get(0)==xosOnClone.getA().get(0));
103 assertFalse(xos.getB().get(0)==xosOnClone.getB().get(0));
104
105 List<Template> pathSiteA = xos.getA().get(0).getGraphOwner()
106 .getEmbeddingPath();
107 List<Template> pathClonedSiteA = xosOnClone.getA().get(0)
108 .getGraphOwner().getEmbeddingPath();
109 assertEquals(pathSiteA.size(),pathClonedSiteA.size());
110 List<Template> pathSiteB = xos.getB().get(0).getGraphOwner()
111 .getEmbeddingPath();
112 List<Template> pathClonedSiteB = xosOnClone.getB().get(0)
113 .getGraphOwner().getEmbeddingPath();
114 assertEquals(pathSiteB.size(),pathClonedSiteB.size());
115
116 assertEquals(xosOnClone.getA().get(0).getAP(2),
117 xosOnClone.getAPsNeedingMappingA().get(0));
118 assertEquals(xosOnClone.getB().get(0).getAP(2),
119 xosOnClone.getAPsNeedingMappingB().get(0));
120 }
121
122//------------------------------------------------------------------------------
123}
This class collects the data identifying the subgraphs that would be swapped by a crossover event.
Definition: XoverSite.java:36
XoverSite projectToClonedGraphs()
Creates a new instance of this class that contains the list of vertexes that correspond to those cont...
Definition: XoverSite.java:275
List< AttachmentPoint > getAPsNeedingMappingA()
Returns the collection of attachment points that need to be mapped in order to achieve a valid crosso...
Definition: XoverSite.java:212
List< AttachmentPoint > getAPsNeedingMappingB()
Returns the collection of attachment points that need to be mapped in order to achieve a valid crosso...
Definition: XoverSite.java:226
List< Vertex > getA()
Returns the collection of vertexes belonging to the first subgraph.
Definition: XoverSite.java:187
List< Vertex > getB()
Returns the collection of vertexes belonging to the second subgraph.
Definition: XoverSite.java:198
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
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
Unit test for DENOPTIMGraph.
Definition: DGraphTest.java:72
static DGraph makeDeeplyEmbeddedGraph()
Returns a graph that contains a 10-layered recursive structure.
static FragmentSpace prepare()
Definition: DGraphTest.java:80
Types of crossover defined.
BRANCH
Swaps the entire branch starting from a given vertex.