[10267] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
[10271] | 28 | namespace HeuristicLab.EvolutionTracking {
|
---|
[10267] | 29 | [StorableClass]
|
---|
| 30 | public class Vertex : Item, IVertex {
|
---|
| 31 | [Storable]
|
---|
| 32 | private string id;
|
---|
| 33 | public string Id {
|
---|
| 34 | get { return id; }
|
---|
| 35 | set { id = value; }
|
---|
| 36 | }
|
---|
| 37 | [Storable]
|
---|
| 38 | private string label;
|
---|
| 39 | public string Label { get { return label; } set { label = value; } }
|
---|
| 40 | [Storable]
|
---|
| 41 | private double weight;
|
---|
| 42 | public double Weight { get { return weight; } set { weight = value; } }
|
---|
| 43 | [Storable]
|
---|
| 44 | private List<IArc> inArcs;
|
---|
| 45 | public List<IArc> InArcs { get { return inArcs; } protected set { inArcs = value; } }
|
---|
| 46 | [Storable]
|
---|
| 47 | private List<IArc> outArcs;
|
---|
| 48 | public List<IArc> OutArcs { get { return outArcs; } protected set { outArcs = value; } }
|
---|
| 49 |
|
---|
| 50 | [StorableConstructor]
|
---|
| 51 | public Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 52 |
|
---|
| 53 | public Vertex() {
|
---|
| 54 | id = Guid.NewGuid().ToString();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | protected Vertex(Vertex original, Cloner cloner)
|
---|
| 58 | : base(original, cloner) {
|
---|
| 59 | Id = Guid.NewGuid().ToString();
|
---|
| 60 | Label = original.Label;
|
---|
| 61 | InArcs = new List<IArc>(original.InArcs);
|
---|
| 62 | OutArcs = new List<IArc>(original.OutArcs);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 66 | return new Vertex(this, cloner);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 70 | protected void AfterDeserialization() {
|
---|
| 71 | if (Id == null) {
|
---|
| 72 | Id = Guid.NewGuid().ToString();
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // this method can be used to add arcs towards targets that are not visible in the graph
|
---|
| 77 | // (they do not appear in the nodes Dictionary). It is the programmers responsibility to
|
---|
| 78 | // enforce the correct and desired behavior.
|
---|
| 79 | public void AddForwardArc(IVertex target, double weight = 0.0, object data = null) {
|
---|
| 80 | var e = new Arc { Source = this, Target = target, Data = data, Weight = weight };
|
---|
| 81 | if (OutArcs == null) OutArcs = new List<IArc> { e };
|
---|
| 82 | else OutArcs.Add(e);
|
---|
| 83 | }
|
---|
| 84 | public void AddForwardArc(IArc arc) {
|
---|
| 85 | if (arc.Source == null) { arc.Source = this; }
|
---|
| 86 | if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
|
---|
| 87 | if (OutArcs == null) { OutArcs = new List<IArc> { arc }; } else { OutArcs.Add(arc); }
|
---|
| 88 | }
|
---|
| 89 | public void AddReverseArc(IArc arc) {
|
---|
| 90 | if (arc.Target == null) { arc.Target = this; }
|
---|
| 91 | if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
|
---|
| 92 | if (InArcs == null) { InArcs = new List<IArc> { arc }; } else { InArcs.Add(arc); }
|
---|
| 93 | }
|
---|
| 94 | public void AddReverseArc(IVertex source, double weight = 0.0, object data = null) {
|
---|
| 95 | var e = new Arc { Source = source, Target = this, Data = data, Weight = weight };
|
---|
| 96 | if (InArcs == null) InArcs = new List<IArc> { e };
|
---|
| 97 | else InArcs.Add(e);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public int InDegree { get { return InArcs == null ? 0 : InArcs.Count; } }
|
---|
| 101 | public int OutDegree { get { return OutArcs == null ? 0 : OutArcs.Count; } }
|
---|
| 102 | public int Degree { get { return InDegree + OutDegree; } }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | [StorableClass]
|
---|
| 107 | public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
|
---|
| 108 | [Storable]
|
---|
| 109 | private T content;
|
---|
| 110 | public T Content {
|
---|
| 111 | get { return content; }
|
---|
| 112 | set {
|
---|
| 113 | if (value == null)
|
---|
| 114 | throw new ArgumentException("Node content cannot be null.");
|
---|
| 115 | content = value;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public Vertex() { }
|
---|
| 120 |
|
---|
| 121 | [StorableConstructor]
|
---|
| 122 | protected Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 123 |
|
---|
| 124 | protected Vertex(Vertex<T> original, Cloner cloner)
|
---|
| 125 | : base(original, cloner) {
|
---|
| 126 | Content = original.Content; // not sure if to Clone()
|
---|
| 127 |
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 131 | return new Vertex<T>(this, cloner);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public Vertex(Vertex<T> node) {
|
---|
| 135 | Id = Guid.NewGuid().ToString();
|
---|
| 136 | Label = node.Label;
|
---|
| 137 | Content = node.Content;
|
---|
| 138 | InArcs = new List<IArc>(node.InArcs);
|
---|
| 139 | OutArcs = new List<IArc>(node.OutArcs);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|