Skip to content
Snippets Groups Projects
Commit d3e01da3 authored by unknown's avatar unknown
Browse files

implemented k-Clustering with icf-icp

parent 3bddc1c9
No related branches found
No related tags found
No related merge requests found
...@@ -108,7 +108,7 @@ public class ParameterSet implements TunableValidator ...@@ -108,7 +108,7 @@ public class ParameterSet implements TunableValidator
permanentColumn = new ListSingleSelection<CyColumn>(booleanColumns); permanentColumn = new ListSingleSelection<CyColumn>(booleanColumns);
//Don't select any columns by default //Don't select any columns by default
weightColumn.setSelectedValue(numericColumns.get(1)); weightColumn.setSelectedValue(null);
forbiddenColumn.setSelectedValue(null); forbiddenColumn.setSelectedValue(null);
permanentColumn.setSelectedValue(null); permanentColumn.setSelectedValue(null);
......
...@@ -5,28 +5,42 @@ import java.util.*; ...@@ -5,28 +5,42 @@ 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, YoshikoEdge> map = new HashMap<>();
HashMap<YoshikoEdge, Integer> reverseMap = new HashMap<>();
int k = -1;
public BinaryHeap(){ public BinaryHeap(){
list.add(Double.NEGATIVE_INFINITY); list.add(Double.NEGATIVE_INFINITY);
map.put(0, null); map.put(0, null);
reverseMap.put(null, 0);
} }
public void add(YoshikoEdge edge){ public void addMaxIcfIcp(YoshikoEdge edge){
list.add(edge.maxIcfIcp); list.add(edge.maxIcfIcp);
map.put(list.size()-1, edge); map.put(list.size()-1, edge);
reverseMap.put(edge, list.size()-1);
checkParent(list.size()-1); checkParent(list.size()-1);
}
public void add(Set<YoshikoEdge> edgeSet){ if (list.size()-1 == 30864){
for(YoshikoEdge edge : edgeSet){ System.out.println("asd");
add(edge); }
} }
public void addIcfMinusIcp(YoshikoEdge edge){
list.add(edge.icfMinusIcp);
map.put(list.size()-1, edge);
reverseMap.put(edge, list.size()-1);
checkParent(list.size()-1);
} }
public void remove(YoshikoEdge edge){ public void remove(YoshikoEdge edge){
int index = getIndexFromValue(edge); if (!reverseMap.containsKey(edge)){
return;
}
int index = reverseMap.get(edge);
if (edge.weight != Double.NEGATIVE_INFINITY){ if (edge.weight != Double.NEGATIVE_INFINITY){
//System.out.println("MELDUNG 5"); //System.out.println("MELDUNG 5");
...@@ -37,23 +51,7 @@ public class BinaryHeap { ...@@ -37,23 +51,7 @@ public class BinaryHeap {
return; return;
} }
map.replace(index, map.get(map.size()-1)); saveRemove(index, edge);
map.remove(map.size()-1);
list.set(index, list.get(list.size()-1));
list.remove(list.size()-1);
}
public void removeIncidentEdges(int n1, int n2){
}
public void remove(Set<YoshikoEdge> edgeSet){
for (YoshikoEdge edge : edgeSet){
remove(edge);
}
} }
public YoshikoEdge popMax(){ public YoshikoEdge popMax(){
...@@ -68,24 +66,46 @@ public class BinaryHeap { ...@@ -68,24 +66,46 @@ public class BinaryHeap {
YoshikoEdge e = map.get(index); YoshikoEdge e = map.get(index);
saveRemove(index, e);
return e;
}
private void saveRemove(int index, YoshikoEdge e) {
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);
YoshikoEdge movedEdge = map.get(index);
reverseMap.replace(movedEdge, index);
reverseMap.remove(e);
list.set(index, list.get(list.size()-1)); list.set(index, list.get(list.size()-1));
list.remove(list.size()-1); list.remove(list.size()-1);
return e;
} }
public void updateEdge(YoshikoEdge edge){
if (!reverseMap.containsKey(edge)){
return;
}
int index = reverseMap.get(edge);
if (index < 0){
return;
}
public void checkUptatedNode(YoshikoEdge edge){ if (k<0){
int index = getIndexFromValue(edge); list.set(index, edge.maxIcfIcp);
}else {
list.set(index, edge.icfMinusIcp);
}
checkParent(index); checkParent(index);
checkChild(index); checkChild(index);
} }
public int size(){ public int size(){
return list.size()-1; return list.size()-1;
} }
...@@ -121,20 +141,17 @@ public class BinaryHeap { ...@@ -121,20 +141,17 @@ 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);
map.replace(index, map.get(parentIndex));
map.replace(parentIndex, tmpEdge);
}
private int getIndexFromValue(YoshikoEdge edge){ YoshikoEdge edge1 = map.get(index);
for (Map.Entry<Integer, YoshikoEdge> entry : map.entrySet()) { YoshikoEdge edge2 = map.get(parentIndex);
if (entry.getKey() == 0){
//Der erste DummyEintrag im Heap soll ignoriert werden Integer index1 = reverseMap.get(edge1);
} Integer index2 = reverseMap.get(edge2);
else if (entry.getValue().edgeId == edge.edgeId) {
return entry.getKey(); map.replace(index, edge2);
} map.replace(parentIndex, edge1);
}
return -1; reverseMap.replace(edge1, index2);
reverseMap.replace(edge2, index1);
} }
} }
\ No newline at end of file
...@@ -12,28 +12,32 @@ public class ClusteringAlgorithm { ...@@ -12,28 +12,32 @@ public class ClusteringAlgorithm {
private int numberOfNodes; private int numberOfNodes;
List<Integer> nodeList; List<Integer> nodeList;
List<List<Integer>> clusters; List<List<Integer>> clusters;
int k=-1;
int nodeInClusters;
public ClusteringAlgorithm(YoshikoEdge[][] edgeArray){ public ClusteringAlgorithm(YoshikoEdge[][] edgeArray){
this.edgeArray = edgeArray; this.edgeArray = edgeArray;
numberOfNodes = edgeArray.length; numberOfNodes = edgeArray.length;
clusters = new ArrayList<>(); clusters = new ArrayList<>();
}
public List<List<Integer>> runAlgorithm(){
clusteringCost = 0; clusteringCost = 0;
}
bHeap = new BinaryHeap(); public List<List<Integer>> runClusteringAlgorithm(){
initializeQueue(); initializeQueue();
if (k < 0) {
while (bHeap.size() > 0) {
workHeap(); workHeap();
addSingleNodesToClusters();
int counter = 0;
List<Integer> list = new ArrayList<>();
for (List<Integer> cluster : clusters){
counter += cluster.size();
} }
}else {
while (numberOfNodes-nodeInClusters > k) {
workHeap();
}
}
addSingleNodesToClusters();
System.out.println(clusteringCost); System.out.println(clusteringCost);
return clusters; return clusters;
...@@ -41,14 +45,19 @@ public class ClusteringAlgorithm { ...@@ -41,14 +45,19 @@ public class ClusteringAlgorithm {
private void initializeQueue(){ private void initializeQueue(){
bHeap = new BinaryHeap(); bHeap = new BinaryHeap();
bHeap.k = k;
for (int i=1; i<numberOfNodes; i++){ for (int i=1; i<numberOfNodes; i++){
for (int j=0; j<i; j++){ for (int j=0; j<i; j++){
YoshikoEdge edge = initIcfIcp(i,j); YoshikoEdge edge = calculateIcfIcp(i,j);
bHeap.add(edge); if (k<1){
bHeap.addMaxIcfIcp(edge);
} else {
bHeap.addIcfMinusIcp(edge);
}
} }
} }
System.out.println("MELDUNG 2");
initializeNodeList(); initializeNodeList();
} }
...@@ -59,11 +68,10 @@ public class ClusteringAlgorithm { ...@@ -59,11 +68,10 @@ public class ClusteringAlgorithm {
} }
} }
private YoshikoEdge initIcfIcp(int u, int v){ private YoshikoEdge calculateIcfIcp(int u, int v){
double icf = 0; double icf = 0;
double icp = 0; double icp = 0;
YoshikoEdge edge = edgeArray[u][v]; YoshikoEdge edge = edgeArray[u][v];
if (edge == null){ if (edge == null){
...@@ -105,6 +113,9 @@ public class ClusteringAlgorithm { ...@@ -105,6 +113,9 @@ public class ClusteringAlgorithm {
edge.icf = icf; edge.icf = icf;
edge.icp = icp; edge.icp = icp;
edge.maxIcfIcp = Math.max(icf, icp); edge.maxIcfIcp = Math.max(icf, icp);
edge.icfMinusIcp = icf-icp;
bHeap.updateEdge(edge);
return edge; return edge;
} }
...@@ -118,36 +129,20 @@ public class ClusteringAlgorithm { ...@@ -118,36 +129,20 @@ public class ClusteringAlgorithm {
private void workHeap(){ private void workHeap(){
int minH = 99999999;
while (bHeap.size() > 0){
if (minH < bHeap.size()){
System.out.println("MELDUNG 4");
}
if (bHeap.size()==21649){
System.out.println("MELDUNG 6");
}
minH=bHeap.size();
System.out.println(bHeap.size()); System.out.println(bHeap.size());
YoshikoEdge e=bHeap.popMax(); YoshikoEdge e=bHeap.popMax();
if (e.icf >= e.icp){ if (e.icf >= e.icp|| k > -1){
if(e.weight < 0){
clusteringCost -= e.weight;
}
makeEdgePermanent(e); makeEdgePermanent(e);
} }
else{ else{
if(e.weight > 0){
clusteringCost +=e.weight;
}
makeEdgeForbidden(e); makeEdgeForbidden(e);
} }
if (clusteringCost == Double.POSITIVE_INFINITY){ if (clusteringCost == Double.POSITIVE_INFINITY){
System.out.println("MELDUNG 7"); System.out.println("MELDUNG 7");
} }
} }
}
private YoshikoEdge getEdge(int edgeId){ private YoshikoEdge getEdge(int edgeId){
int u = (int) Math.ceil(Math.sqrt(2*(edgeId-1)+0.25)-0.5); int u = (int) Math.ceil(Math.sqrt(2*(edgeId-1)+0.25)-0.5);
...@@ -164,6 +159,10 @@ public class ClusteringAlgorithm { ...@@ -164,6 +159,10 @@ public class ClusteringAlgorithm {
} }
private void makeEdgePermanent(YoshikoEdge e){ private void makeEdgePermanent(YoshikoEdge e){
if(e.weight < 0){
clusteringCost -= e.weight;
}
substactInfluenceOfVerticesOnIcfIcp(e); substactInfluenceOfVerticesOnIcfIcp(e);
mergeVertexes(e); mergeVertexes(e);
...@@ -206,8 +205,8 @@ public class ClusteringAlgorithm { ...@@ -206,8 +205,8 @@ public class ClusteringAlgorithm {
bHeap.remove(e1); bHeap.remove(e1);
} }
} }
Integer k = n2; Integer o = n2;
nodeList.remove(k); nodeList.remove(o);
} }
private void calculateNewEdgesIcfIcp(int node1){ private void calculateNewEdgesIcfIcp(int node1){
...@@ -224,7 +223,7 @@ public class ClusteringAlgorithm { ...@@ -224,7 +223,7 @@ public class ClusteringAlgorithm {
if (e.weight == Double.NEGATIVE_INFINITY){ if (e.weight == Double.NEGATIVE_INFINITY){
continue; continue;
} }
initIcfIcp(node1, node2); calculateIcfIcp(node1, node2);
} }
} }
...@@ -276,6 +275,8 @@ public class ClusteringAlgorithm { ...@@ -276,6 +275,8 @@ public class ClusteringAlgorithm {
} }
edge.maxIcfIcp = Math.max(edge.icf, edge.icp); edge.maxIcfIcp = Math.max(edge.icf, edge.icp);
edge.icfMinusIcp = edge.icf-edge.icp;
bHeap.updateEdge(edge);
} }
} }
...@@ -307,11 +308,17 @@ public class ClusteringAlgorithm { ...@@ -307,11 +308,17 @@ public class ClusteringAlgorithm {
edge.icp += Math.min(-ux.weight, vx.weight); edge.icp += Math.min(-ux.weight, vx.weight);
} }
edge.maxIcfIcp = Math.max(edge.icf, edge.icp);
edge.icfMinusIcp = edge.icf-edge.icp;
bHeap.updateEdge(edge);
} }
} }
private void makeEdgeForbidden(YoshikoEdge e) { private void makeEdgeForbidden(YoshikoEdge e) {
if(e.weight > 0){
clusteringCost +=e.weight;
}
editInfluenzeOfForbiddenEdge(e); editInfluenzeOfForbiddenEdge(e);
e.weight=Double.NEGATIVE_INFINITY; e.weight=Double.NEGATIVE_INFINITY;
...@@ -349,6 +356,7 @@ public class ClusteringAlgorithm { ...@@ -349,6 +356,7 @@ public class ClusteringAlgorithm {
edge1.icp += edge2.weight+forbiddenEdge.weight; edge1.icp += edge2.weight+forbiddenEdge.weight;
} }
} }
bHeap.updateEdge(edge1);
if (forbiddenEdge.weight>0 && edge1.weight > 0){ if (forbiddenEdge.weight>0 && edge1.weight > 0){
edge2.icf -= Math.min(forbiddenEdge.weight, edge1.weight); edge2.icf -= Math.min(forbiddenEdge.weight, edge1.weight);
...@@ -362,18 +370,17 @@ public class ClusteringAlgorithm { ...@@ -362,18 +370,17 @@ public class ClusteringAlgorithm {
edge2.icp += edge1.weight+forbiddenEdge.weight; edge2.icp += edge1.weight+forbiddenEdge.weight;
} }
} }
bHeap.updateEdge(edge2);
} }
} }
private void putInCluster(YoshikoEdge edge){ private void putInCluster(YoshikoEdge edge){
nodeInClusters++;
Integer x1 = edge.source; Integer x1 = edge.source;
Integer x2 = edge.target; Integer x2 = edge.target;
int indexOfx1 = -1; int indexOfx1 = -1;
int indexOfx2 = -1; int indexOfx2 = -1;
if (edge.source == 242|| edge.target == 242){
System.out.println("asdasda");
}
for (List<Integer> cluster : clusters){ for (List<Integer> cluster : clusters){
if (cluster.contains(x1)){ if (cluster.contains(x1)){
indexOfx1 = clusters.indexOf(cluster); indexOfx1 = clusters.indexOf(cluster);
......
package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm; package de.hhu.ba.yoshikoWrapper.yoshikoAlgorithm;
import de.hhu.ba.yoshikoWrapper.core.CyCore;
import de.hhu.ba.yoshikoWrapper.core.ParameterSet; import de.hhu.ba.yoshikoWrapper.core.ParameterSet;
import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork; import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyTable;
import org.jgrapht.Graph;
import org.jgrapht.graph.SimpleWeightedGraph;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Set;
public class YoshikoAlgoritmController { public class YoshikoAlgoritmController {
private int k = 19;
private CyNetwork network; private CyNetwork network;
private ParameterSet parameterSet; private ParameterSet parameterSet;
...@@ -28,8 +21,15 @@ public class YoshikoAlgoritmController { ...@@ -28,8 +21,15 @@ public class YoshikoAlgoritmController {
YoshikoEdge[][] edgeArray = translator.translateGraph(); YoshikoEdge[][] edgeArray = translator.translateGraph();
ClusteringAlgorithm clusteringAlgorithm = new ClusteringAlgorithm(edgeArray); ClusteringAlgorithm clusteringAlgorithm = new ClusteringAlgorithm(edgeArray);
List<List<Integer>> clusters = clusteringAlgorithm.runAlgorithm(); clusteringAlgorithm.k = k;
List<List<Integer>> clusters;
clusters = clusteringAlgorithm.runClusteringAlgorithm();
translator.makeCytoscapeGraph(clusters); translator.makeCytoscapeGraph(clusters);
System.out.println("asdadasd");
} }
......
...@@ -11,6 +11,7 @@ public class YoshikoEdge { ...@@ -11,6 +11,7 @@ public class YoshikoEdge {
double icf; double icf;
double icp; double icp;
double maxIcfIcp; double maxIcfIcp;
double icfMinusIcp;
public YoshikoEdge(long suid, double weight, int edgeId){ public YoshikoEdge(long suid, double weight, int edgeId){
this.suid = suid; this.suid = suid;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment