$darkmode
DENOPTIM
XoverSite.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 java.util.ArrayList;
22import java.util.List;
23
24import denoptim.exception.DENOPTIMException;
25import denoptim.graph.AttachmentPoint;
26import denoptim.graph.DGraph;
27import denoptim.graph.Template;
28import denoptim.graph.Vertex;
29import denoptim.utils.CrossoverType;
30
35public class XoverSite implements Cloneable
36{
40 private List<Vertex> subGraphA = null;
41
50 private List<AttachmentPoint> needyAPsOnA = null;
51
55 private List<Vertex> subGraphB = null;
56
65 private List<AttachmentPoint> needyAPsOnB = null;
66
70 private CrossoverType xoverType = null;
71
72//------------------------------------------------------------------------------
73
77 public XoverSite()
78 {
79 subGraphA = new ArrayList<Vertex>();
80 subGraphB = new ArrayList<Vertex>();
81 needyAPsOnA = new ArrayList<AttachmentPoint>();
82 needyAPsOnB = new ArrayList<AttachmentPoint>();
83 }
84
85//------------------------------------------------------------------------------
86
99 public XoverSite(List<Vertex> subGraphA,
100 List<Vertex> subGraphB,
102 {
103 this.subGraphA = new ArrayList<Vertex>(subGraphA);
104 this.subGraphB = new ArrayList<Vertex>(subGraphB);
105 DGraph gA = subGraphA.get(0).getGraphOwner();
106 this.needyAPsOnA = gA.getInterfaceAPs(subGraphA);
107 DGraph gB = subGraphB.get(0).getGraphOwner();
108 this.needyAPsOnB = gB.getInterfaceAPs(subGraphB);
109 this.xoverType = xoverType;
110 }
111
112//------------------------------------------------------------------------------
113
130 public XoverSite(List<Vertex> subGraphA,
131 List<AttachmentPoint> needyAPsOnA,
132 List<Vertex> subGraphB,
133 List<AttachmentPoint> needyAPsOnB,
135 {
136 this.subGraphA = new ArrayList<Vertex>(subGraphA);
137 this.subGraphB = new ArrayList<Vertex>(subGraphB);
138 this.needyAPsOnA = new ArrayList<AttachmentPoint>(needyAPsOnA);
139 this.needyAPsOnB = new ArrayList<AttachmentPoint>(needyAPsOnB);
140 this.xoverType = xoverType;
141 }
142
143//------------------------------------------------------------------------------
144
150 {
151 XoverSite mirror = new XoverSite();
152 mirror.subGraphA = new ArrayList<Vertex>(this.subGraphB);
153 mirror.subGraphB = new ArrayList<Vertex>(this.subGraphA);
154 mirror.needyAPsOnA = new ArrayList<AttachmentPoint>(
155 this.needyAPsOnB);
156 mirror.needyAPsOnB = new ArrayList<AttachmentPoint>(
157 this.needyAPsOnA);
158 mirror.xoverType = xoverType;
159 return mirror;
160 }
161
162//------------------------------------------------------------------------------
163
169 {
170 XoverSite clone = new XoverSite();
171 clone.subGraphA = new ArrayList<Vertex>(this.subGraphA);
172 clone.subGraphB = new ArrayList<Vertex>(this.subGraphB);
173 clone.needyAPsOnA = new ArrayList<AttachmentPoint>(
174 this.needyAPsOnA);
175 clone.needyAPsOnB = new ArrayList<AttachmentPoint>(
176 this.needyAPsOnB);
177 clone.xoverType = xoverType;
178 return clone;
179 }
180
181//------------------------------------------------------------------------------
182
187 public List<Vertex> getA()
188 {
189 return subGraphA;
190 }
191
192//------------------------------------------------------------------------------
193
198 public List<Vertex> getB()
199 {
200 return subGraphB;
201 }
202
203//------------------------------------------------------------------------------
204
212 public List<AttachmentPoint> getAPsNeedingMappingA()
213 {
214 return needyAPsOnA;
215 }
216
217//------------------------------------------------------------------------------
218
226 public List<AttachmentPoint> getAPsNeedingMappingB()
227 {
228 return needyAPsOnB;
229 }
230
231//------------------------------------------------------------------------------
232
238 {
239 return xoverType;
240 }
241
242//------------------------------------------------------------------------------
243
251 @Override
252 public boolean equals(Object o)
253 {
254 if (! (o instanceof XoverSite))
255 return false;
256 XoverSite other = (XoverSite) o;
257 return this.xoverType==other.xoverType
258 && this.subGraphA.equals(other.subGraphA)
259 && this.subGraphB.equals(other.subGraphB)
260 && this.needyAPsOnA.equals(other.needyAPsOnA)
261 && this.needyAPsOnB.equals(other.needyAPsOnB);
262 }
263
264//------------------------------------------------------------------------------
265
276 {
277 DGraph gA = subGraphA.get(0).getGraphOwner();
278 DGraph gB = subGraphB.get(0).getGraphOwner();
279 List<Template> embeddingPathA = gA.getEmbeddingPath();
280 List<Template> embeddingPathB = gB.getEmbeddingPath();
281
282 DGraph cloneOutermostGraphA, cloneA;
283 DGraph cloneOutermostGraphB, cloneB;
284 if (embeddingPathA.size()==0)
285 {
286 cloneOutermostGraphA = gA.clone();
287 cloneA = cloneOutermostGraphA;
288 } else {
289 cloneOutermostGraphA = embeddingPathA.get(0)
290 .getGraphOwner().clone();
291 cloneA = DGraph.getEmbeddedGraphInClone(cloneOutermostGraphA,
292 embeddingPathA.get(0).getGraphOwner(), embeddingPathA);
293 }
294 if (embeddingPathB.size()==0)
295 {
296 cloneOutermostGraphB = gB.clone();
297 cloneB = cloneOutermostGraphB;
298 } else {
299 cloneOutermostGraphB = embeddingPathB.get(0)
300 .getGraphOwner().clone();
301 cloneB = DGraph.getEmbeddedGraphInClone(cloneOutermostGraphB,
302 embeddingPathB.get(0).getGraphOwner(), embeddingPathB);
303 }
304
305 cloneA.renumberGraphVertices();
306 cloneB.renumberGraphVertices();
307 List<Vertex> refsOnCloneA = new ArrayList<Vertex>();
308 List<AttachmentPoint> needyOnCloneA =
309 new ArrayList<AttachmentPoint>();
310 for (Vertex vA : subGraphA)
311 {
312 Vertex vCloneA = cloneA.getVertexAtPosition(
313 vA.getGraphOwner().indexOf(vA));
314 refsOnCloneA.add(vCloneA);
315 for (AttachmentPoint ap : vA.getAttachmentPoints())
316 {
317 if (needyAPsOnA.contains(ap))
318 {
319 needyOnCloneA.add(vCloneA.getAP(ap.getIndexInOwner()));
320 }
321 }
322 }
323 List<Vertex> refsOnCloneB = new ArrayList<Vertex>();
324 List<AttachmentPoint> needyOnCloneB =
325 new ArrayList<AttachmentPoint>();
326 for (Vertex vB : subGraphB)
327 {
328 Vertex vCloneB = cloneB.getVertexAtPosition(
329 vB.getGraphOwner().indexOf(vB));
330 refsOnCloneB.add(vCloneB);
331 for (AttachmentPoint ap : vB.getAttachmentPoints())
332 {
333 if (needyAPsOnB.contains(ap))
334 {
335 needyOnCloneB.add(vCloneB.getAP(ap.getIndexInOwner()));
336 }
337 }
338 }
339 XoverSite xos = new XoverSite(refsOnCloneA, needyOnCloneA,
340 refsOnCloneB, needyOnCloneB, xoverType);
341 return xos;
342 }
343
344//------------------------------------------------------------------------------
345
349 public String toString()
350 {
351 return "[XoverSite-" + xoverType + ": "+subGraphA+", "+subGraphB+"]";
352 }
353
354//------------------------------------------------------------------------------
355
356}
This class collects the data identifying the subgraphs that would be swapped by a crossover event.
Definition: XoverSite.java:36
List< AttachmentPoint > needyAPsOnB
List of attachment points on subgraph A and that need to be replaced by a corresponding attachment po...
Definition: XoverSite.java:65
List< Vertex > subGraphB
The other of the two subgraphs.
Definition: XoverSite.java:55
XoverSite clone()
Creates a new XoverSite that contains the same information of this one, i.e., a shallow copy.
Definition: XoverSite.java:168
List< Vertex > subGraphA
One of the two subgraphs.
Definition: XoverSite.java:40
XoverSite projectToClonedGraphs()
Creates a new instance of this class that contains the list of vertexes that correspond to those cont...
Definition: XoverSite.java:275
XoverSite()
Initiate an empty data structure.
Definition: XoverSite.java:77
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 > needyAPsOnA
List of attachment points on subgraph B and that need to be replaced by a corresponding attachment po...
Definition: XoverSite.java:50
XoverSite(List< Vertex > subGraphA, List< AttachmentPoint > needyAPsOnA, List< Vertex > subGraphB, List< AttachmentPoint > needyAPsOnB, CrossoverType xoverType)
Create a site by listing the vertexes that belong to each of the subgraphs that should be swapped by ...
Definition: XoverSite.java:130
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
CrossoverType getType()
Returns the type of crossover.
Definition: XoverSite.java:237
String toString()
Produced a string for showing what this object is.
Definition: XoverSite.java:349
XoverSite createMirror()
Creates a new XoverSite that considers the opposite order of candidates of this one.
Definition: XoverSite.java:149
List< Vertex > getA()
Returns the collection of vertexes belonging to the first subgraph.
Definition: XoverSite.java:187
boolean equals(Object o)
Compares this and another instance.
Definition: XoverSite.java:252
XoverSite(List< Vertex > subGraphA, List< Vertex > subGraphB, CrossoverType xoverType)
Create a site by listing the vertexes that belong to each of the subgraphs that should be swapped by ...
Definition: XoverSite.java:99
CrossoverType xoverType
Type of crossover.
Definition: XoverSite.java:70
List< Vertex > getB()
Returns the collection of vertexes belonging to the second subgraph.
Definition: XoverSite.java:198
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
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
static DGraph getEmbeddedGraphInClone(DGraph graphY, DGraph graphB, List< Template > path)
Searches for a graphs (X) embedded at any level in a graph (Y) by knowing.
Definition: DGraph.java:7085
List< AttachmentPoint > getInterfaceAPs(List< Vertex > subGraphB)
Searches for all AttachmentPoints that represent the interface between a subgraph,...
Definition: DGraph.java:7113
DGraph clone()
Returns almost "deep-copy" of this graph.
Definition: DGraph.java:3186
void renumberGraphVertices()
Reassign vertex IDs to all vertices of this graph.
Definition: DGraph.java:5283
List< Template > getEmbeddingPath()
Find the path that one has to traverse to reach this graph from any template-embedding structure.
Definition: DGraph.java:6965
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Definition: Vertex.java:920
Types of crossover defined.