-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdge.java
More file actions
119 lines (104 loc) · 2.25 KB
/
Copy pathEdge.java
File metadata and controls
119 lines (104 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main;
public class Edge implements Comparable<Edge>{
private Node from;
private Node to;
private static int numEdges = 0;
private int id;
private double pheromoneLevel = 1;
private final static double PHEROMONE_DROP = .1;
public Edge(Node from, Node to){
if(from.compareTo(to) == 0){
throw new IllegalArgumentException("Cannot have edge to the same Node");
}
if(from.compareTo(to) < 0){
this.from = from;
this.to = to;
}
else{
this.from = to;
this.to = from;
}
id = numEdges;
numEdges++;
}
/**
* Raise the edge's pheromone level by the specified amount
* @return the new pheromone level
*/
public double raisePheremoneLevel(double amount){
pheromoneLevel += amount;
return pheromoneLevel;
}
/**
* Lower the edge's pheromone level by the specified amount to a minimum of 1
* @return the new pheromone level
*/
public double lowerPheremoneLevel(){
pheromoneLevel -= PHEROMONE_DROP;
if(pheromoneLevel < 1){
pheromoneLevel = 1;
}
return pheromoneLevel;
}
/**
* @return the Node of the edge opposite of the supplied Node
*/
public Node oppositeEnd(Node n){
if(from != n && to != n){
throw new IllegalArgumentException("Edge " + this + " did not contain node " + n);
}
if(from == n){
return to;
}
else{
return from;
}
}
public double getPheremoneLevel(){
return pheromoneLevel;
}
public Node from() {
return from;
}
public Node to() {
return to;
}
public int id() {
return id;
}
public boolean equals(Edge other){
if(other == null){
return false;
}
if(from == other.from() && to == other.to()){
return true;
}
if(from == other.to() && to == other.from()){
return true;
}
return false;
}
public String toString(){
int lowerId = Math.min(from.id(), to.id());
int higherId = Math.max(from.id(), to.id());
return "<" + lowerId + ", " + higherId + "> ";
}
@Override
public int compareTo(Edge other) {
if(this.from().compareTo(((Edge) other).from()) < 0){
return -1;
}
else if(this.from().compareTo(((Edge) other).from()) > 0){
return 1;
}
else{
if(this.to().compareTo(((Edge) other).to()) < 0){
return -1;
}
if(this.to().compareTo(((Edge) other).to()) > 0){
return 1;
}
return 0;
}
}
}