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
Branches
No related tags found
No related merge requests found
......@@ -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
}
......
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++;
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 (long v : nodeSet){
long v1 = edge.source;
long v2 = edge.target;
if ((v == v1)||v == v2){
for (int w=0; w < numberOfNodes; w++){
if (w == u || w == v){
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.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);
......
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());
return g;
}
YoshikoEdge yoshikoEdge = new YoshikoEdge(e.getSUID(), extractValue(e), u, v);
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++;
edgeArray[yoshikoEdge.source][yoshikoEdge.target]= yoshikoEdge;
}
}
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){
......
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 {
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();
}
......
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;
}
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