diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/BinaryHeap.java b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/BinaryHeap.java index a9156e409187bf8fb31c8585aa5dc1f281c5f3e1..a544813e6949fdcbf6d665f35b555baa8e115b7a 100644 --- a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/BinaryHeap.java +++ b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/BinaryHeap.java @@ -4,7 +4,7 @@ import java.util.*; public class BinaryHeap { ArrayList<Double> list = new ArrayList(); - HashMap<Integer, YoshikoEdge> map = new HashMap<>(); + HashMap<Integer, Integer> map = new HashMap<>(); public BinaryHeap(){ list.add(Double.NEGATIVE_INFINITY); @@ -14,7 +14,7 @@ public class BinaryHeap { public void add(YoshikoEdge edge){ list.add(edge.maxIcfIcp); - map.put(list.size()-1, edge); + map.put(list.size()-1, edge.edgeId); checkParent(list.size()-1); } @@ -46,7 +46,7 @@ public class BinaryHeap { } } - public YoshikoEdge popMax(){ + public int popMax(){ double maxVal = Double.NEGATIVE_INFINITY; for (Double i : list){ if (i > maxVal){ @@ -56,7 +56,7 @@ public class BinaryHeap { int index = list.indexOf(maxVal); - YoshikoEdge e = map.get(index); + int e = map.get(index); map.replace(index, map.get(map.size()-1)); map.remove(map.size()-1); @@ -111,13 +111,13 @@ public class BinaryHeap { list.set(index, list.get(parentIndex)); list.set(parentIndex, tmpVal); - YoshikoEdge tmpEdge = map.get(index); + int tmpId = map.get(index); map.replace(index, map.get(parentIndex)); - map.replace(parentIndex, tmpEdge); + map.replace(parentIndex, tmpId); } private int getIndexFromValue(YoshikoEdge edge){ - for (Map.Entry<Integer, YoshikoEdge> entry : map.entrySet()) { + for (Map.Entry<Integer, Integer> entry : map.entrySet()) { if (entry.getKey() == 0){ //Der erste DummyEintrag im Heap soll ignoriert werden } diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/ClusteringAlgorithm.java b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/ClusteringAlgorithm.java index f4cbd5a2e354103d9b52e76ffb2f0a88f4aac6cc..cca1df2f7be2fc5b0a74dd9750b2702fd82686b8 100644 --- a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/ClusteringAlgorithm.java +++ b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/ClusteringAlgorithm.java @@ -1,38 +1,38 @@ package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; -import java.util.Set; - public class ClusteringAlgorithm { - private YoshikoGraph g; private BinaryHeap bHeap; private double clusteringCost; + private YoshikoEdge[][] edgeArray; + private int numberOfNodes; + + public ClusteringAlgorithm(YoshikoEdge[][] edgeArray){ + this.edgeArray = edgeArray; + numberOfNodes = edgeArray.length; + } - public void runAlgorithm(YoshikoGraph g){ - this.g = g; + public void runAlgorithm(){ clusteringCost = 0; bHeap = new BinaryHeap(); initializeQueue(); - //workHeap(); + workHeap(); System.out.println(clusteringCost); } private void initializeQueue(){ bHeap = new BinaryHeap(); - Set<YoshikoEdge> edgeSet = g.edgeSet; - Set<Long> nodeSet = g.nodeSet; - - calculateIcf(nodeSet, edgeSet); - System.out.println(); - /*calculateIcp(nodeSet, edgeSet); - calculateMaxIcpIcf(edgeSet); - - addAllNotForbidden(edgeSet);*/ + for (int i=1; i<numberOfNodes; i++){ + for (int j=0; j<i; j++){ + initIcfIcp(i,j); + } + } + System.out.println("asd"); } /* private void addAllNotForbidden(Set<MyDefaultWeightedEdge> edgeSet){ @@ -43,30 +43,61 @@ public class ClusteringAlgorithm { } } */ - private void calculateIcf(Set<Long> nodeSet, Set<YoshikoEdge> edgeSet){ - int i=0; - for (YoshikoEdge edge : edgeSet){ - System.out.println(i); - i++; - double icf = 0; - if (edge.weight>0){ - icf = edge.weight; + private void initIcfIcp(int u, int v){ + double icf = 0; + double icp = 0; + + + YoshikoEdge edge = edgeArray[u][v]; + + if (edge.weight > 0){ + icf = edge.weight; + } + if (edge.weight < 0){ + icp = -edge.weight; + } + + for (int w=0; w < numberOfNodes; w++){ + if (w == u || w == v){ + continue; } - for (long v : nodeSet){ - long v1 = edge.source; - long v2 = edge.target; - if ((v == v1)||v == v2){ - continue; - } - if (g.getEdge(v, v1).weight > 0 && g.getEdge(v, v2).weight > 0) { - icf = icf + Math.min(g.getEdge(v, v1).weight, g.getEdge(v, v2).weight); - } + YoshikoEdge e1 = edgeArray[u][w]; + if (e1 == null){ + e1 = edgeArray[w][u]; } - edge.icf = icf; + YoshikoEdge e2 = edgeArray[v][w]; + if (e2 == null){ + e2 = edgeArray[w][v]; + } + + if (e1.weight > 0 && e2.weight > 0){ + icf = icf + Math.min(e1.weight, e2.weight); + }else if (e1.weight > 0 && e2.weight < 0){ + icp = icp + Math.min(e1.weight, -e2.weight); + }else if (e1.weight < 0 && e2.weight > 0) { + icp = icp + Math.min(-e1.weight, e2.weight); + } + } + edge.icf = icf; + edge.icp = icp; + edge.maxIcfIcp = Math.max(icf, icp); + + if (edge.maxIcfIcp > 25){ + System.out.println("asd"); + } + + bHeap.add(edge); + } + + private int makeEdgeId(int u, int v){ + if (v>u){ + return v*(v-1)+u; } + return u*(u-1)+v; } + /* private void calculateIcp(Set<Long> vertexSet, Set<YoshikoEdge> edgeSet){ for (YoshikoEdge edge : edgeSet){ @@ -97,28 +128,36 @@ public class ClusteringAlgorithm { for (YoshikoEdge edge : edgeSet){ edge.calculateMaxIcfIcp(); } - } + }*/ private void workHeap(){ while (bHeap.size() > 0){ System.out.println(bHeap.size()); - MyDefaultWeightedEdge e = bHeap.popMax(); + int edgeId = bHeap.popMax(); + YoshikoEdge e = getEdge(edgeId); if (e.icf >= e.icp){ - if(g.getEdgeWeight(e) < 0){ - clusteringCost -= g.getEdgeWeight(e); + if(e.weight < 0){ + clusteringCost -= e.weight; } - makeEdgePermanent(e); + //makeEdgePermanent(e); } else{ - if(g.getEdgeWeight(e) > 0){ - clusteringCost += g.getEdgeWeight(e); + if(e.weight > 0){ + clusteringCost +=e.weight; } makeEdgeForbidden(e); } } } + private YoshikoEdge getEdge(int edgeId){ + int u = (int) Math.ceil(Math.sqrt(2*(edgeId-1)+0.25)-0.5); + int v = edgeId - (u*(u-1))/2; + + return edgeArray[u][v]; + } + /* private void makeEdgePermanent(MyDefaultWeightedEdge e){ substactInfluenceOfVerticesOnIcfIcp(e); @@ -130,6 +169,7 @@ public class ClusteringAlgorithm { System.out.println("perm"); } + /* private MyVertex mergeVertexes(MyDefaultWeightedEdge e){ MyVertex p1 = g.getEdgeSource(e); MyVertex p2 = g.getEdgeTarget(e); @@ -264,7 +304,7 @@ public class ClusteringAlgorithm { } } - } + }*/ private void makeEdgeForbidden(MyDefaultWeightedEdge e){ editInfluenzeOfForbiddenEdge(e); @@ -273,7 +313,7 @@ public class ClusteringAlgorithm { bHeap.remove(e); System.out.println("rm"); } - +/* private void editInfluenzeOfForbiddenEdge(MyDefaultWeightedEdge forbiddenEdge){ MyVertex x1 = g.getEdgeSource(forbiddenEdge); MyVertex x2 = g.getEdgeTarget(forbiddenEdge); diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/GraphTranslator.java b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/GraphTranslator.java index ad87b23f871b3ea9d1f92b060b9e818046cb86f9..44677f8b7c01beb18b417f6542837a7dbd1dc0f1 100644 --- a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/GraphTranslator.java +++ b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/GraphTranslator.java @@ -1,13 +1,9 @@ package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; -import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; -import de.hhu.ba.yoshikoWrapper.CyActivator; import de.hhu.ba.yoshikoWrapper.core.ParameterSet; import org.cytoscape.model.*; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; public class GraphTranslator { private CyNetwork network; @@ -18,61 +14,61 @@ public class GraphTranslator { CyColumn weightColumn; + private Map<Integer, CyNode> nodeMap; + + private YoshikoEdge[][] edgeArray; + public GraphTranslator(ParameterSet parameterSet){ this.parameterSet = parameterSet; this.network = parameterSet.net; this.weightColumn = parameterSet.getWeightColumn(); this.deletionCostDefault = parameterSet.defaultDeletionCost; this.insertionCostDefault = parameterSet.defaultInsertionCost; - } - public YoshikoGraph translateGraph(){ - YoshikoGraph g = this.makeGraph(); - this.makeGraphComplete(g); - return g; + this.nodeMap = new HashMap<>(); } - private YoshikoGraph makeGraph(){ - YoshikoGraph g = new YoshikoGraph(); + public YoshikoEdge[][] translateGraph(){ + this.makeGraph(); + this.makeGraphComplete(); + return edgeArray; + } + private void makeGraph(){ List<CyNode> nodeList = network.getNodeList(); List<CyEdge> edgeList = network.getEdgeList(); + int size = nodeList.size(); + + this.edgeArray = new YoshikoEdge[size][size]; + + Map<CyNode, Integer> reverseNodeMap = new HashMap(); + int i = 0; for (CyNode n : nodeList){ - g.addNode(n.getSUID()); + nodeMap.put(i, n); + reverseNodeMap.put(n,i); + i++; } for (CyEdge e : edgeList){ - YoshikoEdge yoshikoEdge = new YoshikoEdge(e.getSource().getSUID(), e.getTarget().getSUID(), this.extractValue(e), e.getSUID().intValue()); - g.addEdge(yoshikoEdge); - } + int u = reverseNodeMap.get(e.getSource()); + int v = reverseNodeMap.get(e.getTarget()); + + YoshikoEdge yoshikoEdge = new YoshikoEdge(e.getSUID(), extractValue(e), u, v); - return g; + edgeArray[yoshikoEdge.source][yoshikoEdge.target]= yoshikoEdge; + } } - private YoshikoGraph makeGraphComplete(YoshikoGraph g){ - Set<Long> nodeSet = g.getNodeSet(); - Set<Long> usedINodes = new HashSet<>(); - - Set<YoshikoEdge> edgesToAdd = new HashSet<>(); - int edgeID = 1; - for (long i : nodeSet){ - usedINodes.add(i); - for (long j : nodeSet){ - if (!usedINodes.contains(j)){ - YoshikoEdge e = new YoshikoEdge(i, j, insertionCostDefault, -edgeID); - if (!g.containsEdge(e)){ - edgesToAdd.add(e); - System.out.println(edgeID); - edgeID++; - } + private void makeGraphComplete(){ + for (int i = 1; i < edgeArray.length; i++){ + for (int j = 0; j < i; j++){ + if (edgeArray[i][j] == null){ + YoshikoEdge yoshikoEdge = new YoshikoEdge(-1, insertionCostDefault, i, j); + edgeArray[i][j] = yoshikoEdge; } } } - - - g.addEdge(edgesToAdd); - return g; } private double extractValue(CyEdge edge){ diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/MyDefaultWeightedEdge.java b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/MyDefaultWeightedEdge.java deleted file mode 100644 index aea98ea831af0b8ae01b556e6a71f6592324b0d4..0000000000000000000000000000000000000000 --- a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/MyDefaultWeightedEdge.java +++ /dev/null @@ -1,8 +0,0 @@ -package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; - -import org.jgrapht.Graph; -import org.jgrapht.graph.DefaultWeightedEdge; - - -public class MyDefaultWeightedEdge extends DefaultWeightedEdge{ -} diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoAlgoritmController.java b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoAlgoritmController.java index 35d6bcc848c741874dc93a2905d5dfead6816690..7ea06f0ac0f3ab3d74584838603a16183cb82ff5 100644 --- a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoAlgoritmController.java +++ b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoAlgoritmController.java @@ -25,12 +25,10 @@ public class YoshikoAlgoritmController { public void controllAlgorithm(){ GraphTranslator translator = new GraphTranslator(parameterSet); - YoshikoGraph graph = translator.translateGraph(); + YoshikoEdge[][] edgeArray = translator.translateGraph(); - ClusteringAlgorithm clusteringAlgorithm = new ClusteringAlgorithm(); - clusteringAlgorithm.runAlgorithm(graph); - - System.out.println(); + ClusteringAlgorithm clusteringAlgorithm = new ClusteringAlgorithm(edgeArray); + clusteringAlgorithm.runAlgorithm(); } diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoEdge.java b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoEdge.java index 9fef387cd73633ca1003ac12f8023dab2d968396..bed8d8e272f5943a620116038f005f848891c062 100644 --- a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoEdge.java +++ b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoEdge.java @@ -1,36 +1,44 @@ package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; -public class YoshikoEdge implements Comparable{ - int id; - long source; - long target; - double weight; +public class YoshikoEdge { + long suid; + int edgeId; + + int source; + int target; + double weight; double icf; double icp; double maxIcfIcp; - public YoshikoEdge(long source, long target, Double weight, int id){ - this.source = source; - this.target = target; + public YoshikoEdge(long suid, double weight, int edgeId){ + this.suid = suid; this.weight = weight; - this.id = id; + this.edgeId = edgeId; + makeSourceAndTarget(edgeId); } - public void calculateMaxIcfIcp(){ - if (icp >icf ){ - maxIcfIcp = icp; - } else { - maxIcfIcp = icf; - } + public YoshikoEdge(long suid, double weight, int u, int v){ + this.suid = suid; + this.weight = weight; + this.edgeId = this.makeEdgeId(u, v); } - @Override - public int compareTo(Object o) { - if (o==this){ - return 0; + private int makeEdgeId(int u, int v){ + if (v>u){ + this.source = v; + this.target = u; + return (v*(v-1))/2+u; } - YoshikoEdge e = (YoshikoEdge) o; - return this.id - e.id; + this.source = u; + this.target = v; + return (u*(u-1))/2+v; + } + + private void makeSourceAndTarget(int edgeId){ + this.source = (int) Math.ceil(Math.sqrt(2*(edgeId-1)+0.25)-0.5); + this.target = edgeId - (source*(source-1))/2; } + } diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoGraph.java b/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoGraph.java deleted file mode 100644 index cc70d3ae0561b5556d2a033922ecbb701ec77443..0000000000000000000000000000000000000000 --- a/src/main/java/de/hhu/ba/yoshikoWrapper/yoshikoAlgorithm/YoshikoGraph.java +++ /dev/null @@ -1,82 +0,0 @@ -package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; - -import org.cytoscape.model.CyNode; - -import java.util.HashSet; -import java.util.Set; -import java.util.TreeSet; - -public class YoshikoGraph { - Set<Long> nodeSet; - Set<YoshikoEdge> edgeSet; - - public YoshikoGraph(){ - this.nodeSet = new HashSet<>(); - this.edgeSet = new TreeSet<>(); - } - - public void addNode(long node){ - nodeSet.add(node); - } - - public void addEdge(YoshikoEdge edge){ - edgeSet.add(edge); - } - - public void addEdge(Set<YoshikoEdge> edges){ - for (YoshikoEdge e : edges){ - edgeSet.add(e); - } - } - - public void addEdge(long n1, long n2, double value, int id){ - nodeSet.add(n1); - nodeSet.add(n2); - - YoshikoEdge edge = new YoshikoEdge(n1, n2, value, id); - edgeSet.add(edge); - } - - public boolean containsNode(CyNode node){ - return nodeSet.contains(node); - } - - public boolean containsEdge(YoshikoEdge edge){ - - return edgeSet.contains(edge); - } - - public boolean containsEdge(long n1, long n2){ - for(YoshikoEdge e : edgeSet){ - if (n1 == (e.source)){ - if (n2 == (e.target)){ - return true; - } - }else if (n2 == (e.source)){ - if (n1 == (e.target)){ - return true; - } - } - } - return false; - } - - public Set<Long> getNodeSet(){ - return nodeSet; - } - - public YoshikoEdge getEdge(long n1, long n2){ - for(YoshikoEdge e : edgeSet){ - if (n1 == (e.source)){ - if (n2 == (e.target)){ - return e; - } - }else if (n2 == (e.source)){ - if (n1 == (e.target)){ - return e; - } - } - } - return null; - } -}