Skip to content
Snippets Groups Projects
Commit 2d316c89 authored by LukasEttel's avatar LukasEttel
Browse files

programm saves the edges now in a 2-dimensionl array

parent 2721a070
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ import java.util.*; ...@@ -4,7 +4,7 @@ import java.util.*;
public class BinaryHeap { public class BinaryHeap {
ArrayList<Double> list = new ArrayList(); ArrayList<Double> list = new ArrayList();
HashMap<Integer, YoshikoEdge> map = new HashMap<>(); HashMap<Integer, Integer> map = new HashMap<>();
public BinaryHeap(){ public BinaryHeap(){
list.add(Double.NEGATIVE_INFINITY); list.add(Double.NEGATIVE_INFINITY);
...@@ -14,7 +14,7 @@ public class BinaryHeap { ...@@ -14,7 +14,7 @@ public class BinaryHeap {
public void add(YoshikoEdge edge){ public void add(YoshikoEdge edge){
list.add(edge.maxIcfIcp); list.add(edge.maxIcfIcp);
map.put(list.size()-1, edge); map.put(list.size()-1, edge.edgeId);
checkParent(list.size()-1); checkParent(list.size()-1);
} }
...@@ -46,7 +46,7 @@ public class BinaryHeap { ...@@ -46,7 +46,7 @@ public class BinaryHeap {
} }
} }
public YoshikoEdge popMax(){ public int popMax(){
double maxVal = Double.NEGATIVE_INFINITY; double maxVal = Double.NEGATIVE_INFINITY;
for (Double i : list){ for (Double i : list){
if (i > maxVal){ if (i > maxVal){
...@@ -56,7 +56,7 @@ public class BinaryHeap { ...@@ -56,7 +56,7 @@ public class BinaryHeap {
int index = list.indexOf(maxVal); int index = list.indexOf(maxVal);
YoshikoEdge e = map.get(index); int e = map.get(index);
map.replace(index, map.get(map.size()-1)); map.replace(index, map.get(map.size()-1));
map.remove(map.size()-1); map.remove(map.size()-1);
...@@ -111,13 +111,13 @@ public class BinaryHeap { ...@@ -111,13 +111,13 @@ public class BinaryHeap {
list.set(index, list.get(parentIndex)); list.set(index, list.get(parentIndex));
list.set(parentIndex, tmpVal); list.set(parentIndex, tmpVal);
YoshikoEdge tmpEdge = map.get(index); int tmpId = map.get(index);
map.replace(index, map.get(parentIndex)); map.replace(index, map.get(parentIndex));
map.replace(parentIndex, tmpEdge); map.replace(parentIndex, tmpId);
} }
private int getIndexFromValue(YoshikoEdge edge){ 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){ if (entry.getKey() == 0){
//Der erste DummyEintrag im Heap soll ignoriert werden //Der erste DummyEintrag im Heap soll ignoriert werden
} }
......
package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm;
import java.util.Set;
public class ClusteringAlgorithm { public class ClusteringAlgorithm {
private YoshikoGraph g;
private BinaryHeap bHeap; private BinaryHeap bHeap;
private double clusteringCost; private double clusteringCost;
private YoshikoEdge[][] edgeArray;
private int numberOfNodes;
public ClusteringAlgorithm(YoshikoEdge[][] edgeArray){
this.edgeArray = edgeArray;
numberOfNodes = edgeArray.length;
}
public void runAlgorithm(YoshikoGraph g){ public void runAlgorithm(){
this.g = g;
clusteringCost = 0; clusteringCost = 0;
bHeap = new BinaryHeap(); bHeap = new BinaryHeap();
initializeQueue(); initializeQueue();
//workHeap(); workHeap();
System.out.println(clusteringCost); System.out.println(clusteringCost);
} }
private void initializeQueue(){ private void initializeQueue(){
bHeap = new BinaryHeap(); bHeap = new BinaryHeap();
Set<YoshikoEdge> edgeSet = g.edgeSet; for (int i=1; i<numberOfNodes; i++){
Set<Long> nodeSet = g.nodeSet; for (int j=0; j<i; j++){
initIcfIcp(i,j);
calculateIcf(nodeSet, edgeSet); }
System.out.println(); }
/*calculateIcp(nodeSet, edgeSet); System.out.println("asd");
calculateMaxIcpIcf(edgeSet);
addAllNotForbidden(edgeSet);*/
} }
/* /*
private void addAllNotForbidden(Set<MyDefaultWeightedEdge> edgeSet){ private void addAllNotForbidden(Set<MyDefaultWeightedEdge> edgeSet){
...@@ -43,30 +43,61 @@ public class ClusteringAlgorithm { ...@@ -43,30 +43,61 @@ public class ClusteringAlgorithm {
} }
} }
*/ */
private void calculateIcf(Set<Long> nodeSet, Set<YoshikoEdge> edgeSet){
int i=0; private void initIcfIcp(int u, int v){
for (YoshikoEdge edge : edgeSet){
System.out.println(i);
i++;
double icf = 0; double icf = 0;
double icp = 0;
YoshikoEdge edge = edgeArray[u][v];
if (edge.weight > 0){ if (edge.weight > 0){
icf = edge.weight; icf = edge.weight;
} }
if (edge.weight < 0){
icp = -edge.weight;
}
for (long v : nodeSet){ for (int w=0; w < numberOfNodes; w++){
long v1 = edge.source; if (w == u || w == v){
long v2 = edge.target;
if ((v == v1)||v == v2){
continue; 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];
}
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.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){ private void calculateIcp(Set<Long> vertexSet, Set<YoshikoEdge> edgeSet){
for (YoshikoEdge edge : edgeSet){ for (YoshikoEdge edge : edgeSet){
...@@ -97,28 +128,36 @@ public class ClusteringAlgorithm { ...@@ -97,28 +128,36 @@ public class ClusteringAlgorithm {
for (YoshikoEdge edge : edgeSet){ for (YoshikoEdge edge : edgeSet){
edge.calculateMaxIcfIcp(); edge.calculateMaxIcfIcp();
} }
} }*/
private void workHeap(){ private void workHeap(){
while (bHeap.size() > 0){ while (bHeap.size() > 0){
System.out.println(bHeap.size()); System.out.println(bHeap.size());
MyDefaultWeightedEdge e = bHeap.popMax(); int edgeId = bHeap.popMax();
YoshikoEdge e = getEdge(edgeId);
if (e.icf >= e.icp){ if (e.icf >= e.icp){
if(g.getEdgeWeight(e) < 0){ if(e.weight < 0){
clusteringCost -= g.getEdgeWeight(e); clusteringCost -= e.weight;
} }
makeEdgePermanent(e); //makeEdgePermanent(e);
} }
else{ else{
if(g.getEdgeWeight(e) > 0){ if(e.weight > 0){
clusteringCost += g.getEdgeWeight(e); clusteringCost +=e.weight;
} }
makeEdgeForbidden(e); 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){ private void makeEdgePermanent(MyDefaultWeightedEdge e){
substactInfluenceOfVerticesOnIcfIcp(e); substactInfluenceOfVerticesOnIcfIcp(e);
...@@ -130,6 +169,7 @@ public class ClusteringAlgorithm { ...@@ -130,6 +169,7 @@ public class ClusteringAlgorithm {
System.out.println("perm"); System.out.println("perm");
} }
/*
private MyVertex mergeVertexes(MyDefaultWeightedEdge e){ private MyVertex mergeVertexes(MyDefaultWeightedEdge e){
MyVertex p1 = g.getEdgeSource(e); MyVertex p1 = g.getEdgeSource(e);
MyVertex p2 = g.getEdgeTarget(e); MyVertex p2 = g.getEdgeTarget(e);
...@@ -264,7 +304,7 @@ public class ClusteringAlgorithm { ...@@ -264,7 +304,7 @@ public class ClusteringAlgorithm {
} }
} }
} }*/
private void makeEdgeForbidden(MyDefaultWeightedEdge e){ private void makeEdgeForbidden(MyDefaultWeightedEdge e){
editInfluenzeOfForbiddenEdge(e); editInfluenzeOfForbiddenEdge(e);
...@@ -273,7 +313,7 @@ public class ClusteringAlgorithm { ...@@ -273,7 +313,7 @@ public class ClusteringAlgorithm {
bHeap.remove(e); bHeap.remove(e);
System.out.println("rm"); System.out.println("rm");
} }
/*
private void editInfluenzeOfForbiddenEdge(MyDefaultWeightedEdge forbiddenEdge){ private void editInfluenzeOfForbiddenEdge(MyDefaultWeightedEdge forbiddenEdge){
MyVertex x1 = g.getEdgeSource(forbiddenEdge); MyVertex x1 = g.getEdgeSource(forbiddenEdge);
MyVertex x2 = g.getEdgeTarget(forbiddenEdge); MyVertex x2 = g.getEdgeTarget(forbiddenEdge);
......
package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; 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 de.hhu.ba.yoshikoWrapper.core.ParameterSet;
import org.cytoscape.model.*; import org.cytoscape.model.*;
import java.util.HashSet; import java.util.*;
import java.util.List;
import java.util.Set;
public class GraphTranslator { public class GraphTranslator {
private CyNetwork network; private CyNetwork network;
...@@ -18,61 +14,61 @@ public class GraphTranslator { ...@@ -18,61 +14,61 @@ public class GraphTranslator {
CyColumn weightColumn; CyColumn weightColumn;
private Map<Integer, CyNode> nodeMap;
private YoshikoEdge[][] edgeArray;
public GraphTranslator(ParameterSet parameterSet){ public GraphTranslator(ParameterSet parameterSet){
this.parameterSet = parameterSet; this.parameterSet = parameterSet;
this.network = parameterSet.net; this.network = parameterSet.net;
this.weightColumn = parameterSet.getWeightColumn(); this.weightColumn = parameterSet.getWeightColumn();
this.deletionCostDefault = parameterSet.defaultDeletionCost; this.deletionCostDefault = parameterSet.defaultDeletionCost;
this.insertionCostDefault = parameterSet.defaultInsertionCost; this.insertionCostDefault = parameterSet.defaultInsertionCost;
}
public YoshikoGraph translateGraph(){ this.nodeMap = new HashMap<>();
YoshikoGraph g = this.makeGraph();
this.makeGraphComplete(g);
return g;
} }
private YoshikoGraph makeGraph(){ public YoshikoEdge[][] translateGraph(){
YoshikoGraph g = new YoshikoGraph(); this.makeGraph();
this.makeGraphComplete();
return edgeArray;
}
private void makeGraph(){
List<CyNode> nodeList = network.getNodeList(); List<CyNode> nodeList = network.getNodeList();
List<CyEdge> edgeList = network.getEdgeList(); 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){ for (CyNode n : nodeList){
g.addNode(n.getSUID()); nodeMap.put(i, n);
reverseNodeMap.put(n,i);
i++;
} }
for (CyEdge e : edgeList){ for (CyEdge e : edgeList){
YoshikoEdge yoshikoEdge = new YoshikoEdge(e.getSource().getSUID(), e.getTarget().getSUID(), this.extractValue(e), e.getSUID().intValue()); int u = reverseNodeMap.get(e.getSource());
g.addEdge(yoshikoEdge); int v = reverseNodeMap.get(e.getTarget());
}
return g; YoshikoEdge yoshikoEdge = new YoshikoEdge(e.getSUID(), extractValue(e), u, v);
}
private YoshikoGraph makeGraphComplete(YoshikoGraph g){ edgeArray[yoshikoEdge.source][yoshikoEdge.target]= yoshikoEdge;
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){ private double extractValue(CyEdge edge){
......
package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultWeightedEdge;
public class MyDefaultWeightedEdge extends DefaultWeightedEdge{
}
...@@ -25,12 +25,10 @@ public class YoshikoAlgoritmController { ...@@ -25,12 +25,10 @@ public class YoshikoAlgoritmController {
public void controllAlgorithm(){ public void controllAlgorithm(){
GraphTranslator translator = new GraphTranslator(parameterSet); GraphTranslator translator = new GraphTranslator(parameterSet);
YoshikoGraph graph = translator.translateGraph(); YoshikoEdge[][] edgeArray = translator.translateGraph();
ClusteringAlgorithm clusteringAlgorithm = new ClusteringAlgorithm(); ClusteringAlgorithm clusteringAlgorithm = new ClusteringAlgorithm(edgeArray);
clusteringAlgorithm.runAlgorithm(graph); clusteringAlgorithm.runAlgorithm();
System.out.println();
} }
......
package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm;
public class YoshikoEdge implements Comparable{ public class YoshikoEdge {
int id; long suid;
long source; int edgeId;
long target;
double weight; int source;
int target;
double weight;
double icf; double icf;
double icp; double icp;
double maxIcfIcp; double maxIcfIcp;
public YoshikoEdge(long source, long target, Double weight, int id){ public YoshikoEdge(long suid, double weight, int edgeId){
this.source = source; this.suid = suid;
this.target = target;
this.weight = weight; this.weight = weight;
this.id = id; this.edgeId = edgeId;
makeSourceAndTarget(edgeId);
} }
public void calculateMaxIcfIcp(){ public YoshikoEdge(long suid, double weight, int u, int v){
if (icp >icf ){ this.suid = suid;
maxIcfIcp = icp; this.weight = weight;
} else { this.edgeId = this.makeEdgeId(u, v);
maxIcfIcp = icf;
}
} }
@Override private int makeEdgeId(int u, int v){
public int compareTo(Object o) { if (v>u){
if (o==this){ this.source = v;
return 0; this.target = u;
return (v*(v-1))/2+u;
}
this.source = u;
this.target = v;
return (u*(u-1))/2+v;
} }
YoshikoEdge e = (YoshikoEdge) o;
return this.id - e.id; 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;
} }
} }
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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment