Search in sources :

Example 1 with DefaultWeightedEdge

use of org.jgrapht.graph.DefaultWeightedEdge in project polyGembler by c-zhou.

the class JGraphTraverse method main.

public static void main(String[] args) {
    for (int i = 0; i != 5; i++) assembly_graph.addVertex((char) ('A' + i));
    DefaultWeightedEdge e;
    e = assembly_graph.addEdge('A', 'B');
    assembly_graph.setEdgeWeight(e, 1);
    e = assembly_graph.addEdge('A', 'C');
    assembly_graph.setEdgeWeight(e, 2);
    e = assembly_graph.addEdge('C', 'D');
    assembly_graph.setEdgeWeight(e, 7);
    e = assembly_graph.addEdge('C', 'E');
    assembly_graph.setEdgeWeight(e, 3);
    e = assembly_graph.addEdge('D', 'B');
    assembly_graph.setEdgeWeight(e, 3);
    final Set<Character> visited = new HashSet<Character>();
    char v = 'A';
    int r = 8;
    ClosestFirstIterator<Character, DefaultWeightedEdge> radius_search = new ClosestFirstIterator<Character, DefaultWeightedEdge>(assembly_graph, v, r);
    visited.clear();
    while (radius_search.hasNext()) visited.add(radius_search.next());
    System.out.print("radius search from vtex " + v + ", " + visited.size() + " vtex within " + r + " radius: ");
    for (Character vv : visited) System.out.print(vv + ";");
    System.out.println();
}
Also used : DefaultWeightedEdge(org.jgrapht.graph.DefaultWeightedEdge) ClosestFirstIterator(org.jgrapht.traverse.ClosestFirstIterator) HashSet(java.util.HashSet)

Example 2 with DefaultWeightedEdge

use of org.jgrapht.graph.DefaultWeightedEdge in project polyGembler by c-zhou.

the class Traceable method main.

public static void main(String[] args) {
    final BidiMap<String, Integer> bidi = new TreeBidiMap<String, Integer>();
    bidi.put("a", 1);
    bidi.put("a", 2);
    bidi.put("b", 2);
    System.out.println(bidi.size());
    final DirectedWeightedPseudograph<TraceableVertex<String>, DefaultWeightedEdge> razor = new DirectedWeightedPseudograph<TraceableVertex<String>, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    // JGraphModelAdapter<TraceableVertex<String>, DefaultWeightedEdge> jgAdapter =
    // new JGraphModelAdapter<TraceableVertex<String>, DefaultWeightedEdge>(razor);
    // JGraph jgraph = new JGraph(jgAdapter);
    TraceableVertex<String> a = new TraceableVertex<String>("a");
    TraceableVertex<String> b = new TraceableVertex<String>("b");
    TraceableVertex<String> c = new TraceableVertex<String>("c");
    TraceableVertex<String> d = new TraceableVertex<String>("d");
    razor.addVertex(a);
    razor.addVertex(b);
    razor.addVertex(c);
    razor.addVertex(d);
    razor.setEdgeWeight(razor.addEdge(a, a), 1);
    razor.setEdgeWeight(razor.addEdge(a, b), 1);
    razor.setEdgeWeight(razor.addEdge(a, c), 1);
    razor.setEdgeWeight(razor.addEdge(a, d), 1);
    razor.setEdgeWeight(razor.addEdge(b, a), 1);
    razor.setEdgeWeight(razor.addEdge(b, b), 1);
    razor.setEdgeWeight(razor.addEdge(b, c), 1);
    razor.setEdgeWeight(razor.addEdge(b, d), 1);
    razor.setEdgeWeight(razor.addEdge(c, a), 1);
    razor.setEdgeWeight(razor.addEdge(c, b), 1);
    razor.setEdgeWeight(razor.addEdge(c, c), 1);
    razor.setEdgeWeight(razor.addEdge(c, d), 1);
    razor.setEdgeWeight(razor.addEdge(d, a), 1);
    razor.setEdgeWeight(razor.addEdge(d, b), 1);
    razor.setEdgeWeight(razor.addEdge(d, c), 1);
    razor.setEdgeWeight(razor.addEdge(d, d), 1);
    // JFrame frame = new JFrame();
    // frame.getContentPane().add(jgraph);
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // frame.pack();
    // frame.setVisible(true);
    Set<TraceableVertex<String>> visited = new HashSet<TraceableVertex<String>>();
    Deque<TraceableVertex<String>> queue = new ArrayDeque<TraceableVertex<String>>();
    queue.push(a);
    visited.add(a);
    a.setScore(10);
    Set<DefaultWeightedEdge> out_edges;
    double max_score = Double.NEGATIVE_INFINITY, source_score, target_score, score;
    int source_ln;
    TraceableVertex<String> max_vertex = null;
    boolean isLeaf;
    TraceableVertex<String> source_vertex, target_vertex;
    double edge_weight;
    while (!queue.isEmpty()) {
        source_vertex = queue.pop();
        source_ln = 10;
        source_score = source_vertex.getScore() - source_ln;
        isLeaf = true;
        out_edges = razor.outgoingEdgesOf(source_vertex);
        for (DefaultWeightedEdge out : out_edges) {
            target_vertex = razor.getEdgeTarget(out);
            target_score = target_vertex.getScore();
            edge_weight = razor.getEdgeWeight(out);
            score = source_score + edge_weight;
            if (visited.contains(target_vertex) && (score <= target_score || isLoopback(razor, source_vertex, target_vertex)))
                continue;
            isLeaf = false;
            target_vertex.setBackTrace(source_vertex);
            target_vertex.setScore(score);
            queue.push(target_vertex);
            visited.add(target_vertex);
        }
        if (isLeaf && source_vertex.getScore() > max_score) {
            max_score = source_vertex.getScore();
            max_vertex = source_vertex;
        }
    }
}
Also used : ArrayDeque(java.util.ArrayDeque) DefaultWeightedEdge(org.jgrapht.graph.DefaultWeightedEdge) DirectedWeightedPseudograph(org.jgrapht.graph.DirectedWeightedPseudograph) TraceableVertex(cz1.ngs.model.TraceableVertex) TreeBidiMap(org.apache.commons.collections4.bidimap.TreeBidiMap) HashSet(java.util.HashSet)

Example 3 with DefaultWeightedEdge

use of org.jgrapht.graph.DefaultWeightedEdge in project lobcder by skoulouzis.

the class SDNControllerClient method pushFlow.

// private double getCost(String v1, String v2) throws InterruptedException, IOException {
// //        String[] agentPort = getsFlowPort(v1, v2);
// //        double tpp = getTimePerPacket(agentPort[0], Integer.valueOf(agentPort[1]));
// String dpi;
// if (v1.contains(":")) {
// dpi = v1;
// } else {
// dpi = v2;
// }
// 
// //        SDNSweep.FloodlightStats[] stats = getFloodlightPortStats(dpi, getPort());
// Double rpps = SDNSweep.getReceivePacketsMap().get(dpi);
// Double tpps = SDNSweep.getTransmitPacketsMap().get(dpi);
// if (rpps == null) {
// rpps = Double.valueOf(1);
// }
// if (tpps == null) {
// tpps = Double.valueOf(1);
// }
// //        double rrrt = (interval / rpps);
// //        double trrt = (interval / tpps);
// 
// double tpp = (rpps > tpps) ? rpps : tpps;
// if (tpp <= 0) {
// tpp = 1;
// }
// Double rbytes = SDNSweep.getReceiveBytesMap().get(dpi);
// if (rbytes == null) {
// rbytes = Double.valueOf(1);
// }
// Double tbytes = SDNSweep.getTransmitBytesMap().get(dpi);
// if (tbytes == null) {
// tbytes = Double.valueOf(1);
// }
// double rbps = rbytes / SDNSweep.interval;
// double tbps = tbytes / SDNSweep.interval;
// double cost = 1.0 / ((rbps + tbps) / 2.0);
// 
// 
// if (rbytes <= 0) {
// rbytes = Double.valueOf(1);
// }
// if (tbytes <= 0) {
// tbytes = Double.valueOf(1);
// }
// 
// double rMTU = rbytes / rpps * 1.0;
// double tMTU = tbytes / tpps * 1.0;
// double mtu = (rMTU > tMTU) ? rMTU : tMTU;
// //        if (mtu <= 500) {
// //            mtu = 1500;
// //        }
// 
// //TT=TpP * NoP
// //NoP = {MTU}/{FS}
// //TpP =[({MTU} / {bps}) + RTT] // is the time it takes to transmit one packet or time per packet
// //TT = [({MTU} / {bps}) + RTT] * [ {MTU}/{FS}]
// double nop = mtu / 1024.0;
// double mtt = tpp * nop;
// 
// //        SDNSweep.OFlow f = SDNSweep.getOFlowsMap().get(dpi);
// //        double bps = -1;
// //        if (f != null) {
// //            bps = f.byteCount / f.durationSeconds * 1.0;
// //            double tmp = f.packetCount / f.durationSeconds * 1.0;
// //            if (tpp <= 1 && tmp > tpp) {
// //                mtt = tmp * nop;
// //            }
// //        }
// //        Double averageLinkUsage = SDNSweep.getAverageLinkUsageMap().get(dpi);
// //        if (averageLinkUsage != null) {
// //
// //            if (factor > -1) {
// //                mtt += averageLinkUsage * factor;
// //            }
// ////        For each sec of usage how much extra time we get ?
// ////        We asume a liner ralationship
// ////        The longer the usage it means either more transfers per flow or larger files or both
// //
// ////            Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "dpi: " + dpi + " averageLinkUsage: " + averageLinkUsage);
// //        } else {
// //            mtt-=factor;
// //        }
// 
// //        Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "From: {0} to: {1} tt: {2}", new Object[]{v1, v2, mtt});
// return (mtt > 0) ? mtt : 1.2;
// }
public void pushFlow(final List<DefaultWeightedEdge> shortestPath) throws IOException {
    if (shortestPath != null && !shortestPath.isEmpty()) {
        Thread thread = new Thread() {

            public void run() {
                try {
                    DefaultWeightedEdge e = shortestPath.get(0);
                    String pair = e.toString().substring(1, e.toString().length() - 1);
                    String[] workerSwitch = pair.toString().split(" : ");
                    String srcIP = workerSwitch[0];
                    String srcMac = SDNSweep.getNetworkEntity(srcIP).getMac().get(0);
                    String srcSwitchAndPort = workerSwitch[1];
                    String srcSwitch = srcSwitchAndPort.split("-")[0];
                    String srcIngressPort = String.valueOf(SDNSweep.getNetworkEntity(srcIP).getAttachmentPoint().get(0).getPort());
                    String srcOutput;
                    e = shortestPath.get(1);
                    pair = e.toString().substring(1, e.toString().length() - 1);
                    workerSwitch = pair.split(" : ");
                    if (workerSwitch[0].equals(srcSwitch + "-" + srcIngressPort)) {
                        srcOutput = workerSwitch[1].split("-")[1];
                    } else {
                        srcOutput = workerSwitch[0].split("-")[1];
                    }
                    e = shortestPath.get(shortestPath.size() - 1);
                    pair = e.toString().substring(1, e.toString().length() - 1);
                    workerSwitch = pair.toString().split(" : ");
                    String dstIP = workerSwitch[0];
                    String dstMac = SDNSweep.getNetworkEntity(dstIP).getMac().get(0);
                    String dstSwitchAndPort = workerSwitch[1];
                    String dstSwitch = dstSwitchAndPort.split("-")[0];
                    String dstOutput = String.valueOf(SDNSweep.getNetworkEntity(dstIP).getAttachmentPoint().get(0).getPort());
                    e = shortestPath.get(shortestPath.size() - 2);
                    pair = e.toString().substring(1, e.toString().length() - 1);
                    workerSwitch = pair.toString().split(" : ");
                    String node1 = workerSwitch[0];
                    String node2 = workerSwitch[1];
                    String dstIngressPort = "";
                    if (node1.equals(dstSwitch + "-" + dstOutput)) {
                        dstIngressPort = node2.split("-")[1];
                    } else {
                        dstIngressPort = node1.split("-")[1];
                    }
                    // String rulesrcToSw = "{\"switch\": \"" + srcSwitch + "\", \"name\":\"tmp\", \"cookie\":\"0\", \"priority\":\"5\", "
                    // + "\"src-ip\":\"" + srcIP + "\", \"ingress-getPort()\":\"" + srcIngressPort + "\", "
                    // + "\"dst-ip\": \"" + dstIP + "\", \"active\":\"true\",\"ether-type\":\"0x0800\", "
                    // + "\"actions\":\"output=" + srcOutput + "\"}";
                    // 
                    // 
                    // String ruleSwTodst = "{\"switch\": \"" + dstSwitch + "\", \"name\":\"tmp\", \"cookie\":\"0\", \"priority\":\"5\", "
                    // + "\"src-ip\":\"" + srcIP + "\", \"ingress-getPort()\":\"" + dstIngressPort + "\", "
                    // + "\"dst-ip\": \"" + dstIP + "\", \"active\":\"true\",\"ether-type\":\"0x0800\", "
                    // + "\"actions\":\"output=" + dstOutput + "\"}";
                    String rule11 = "{\"switch\": \"" + srcSwitch + "\", \"name\":\"tmp1-1\", \"cookie\":\"0\", \"priority\":\"5\", " + "\"src-mac\":\"" + srcMac + "\", \"ingress-getPort()\":\"" + srcIngressPort + "\", " + "\"dst-mac\": \"" + dstMac + "\", \"active\":\"true\",\"vlan-id\":\"-1\", " + "\"actions\":\"output=" + srcOutput + "\"}";
                    String rule12 = "{\"switch\": \"" + srcSwitch + "\", \"name\":\"tmp1-2\", \"cookie\":\"0\", \"priority\":\"5\", " + "\"src-mac\":\"" + dstMac + "\", \"ingress-getPort()\":\"" + srcOutput + "\", " + "\"dst-mac\": \"" + srcMac + "\", \"active\":\"true\",\"vlan-id\":\"-1\", " + "\"actions\":\"output=" + srcIngressPort + "\"}";
                    String rule21 = "{\"switch\": \"" + dstSwitch + "\", \"name\":\"tmp2-1\", \"cookie\":\"0\", \"priority\":\"5\", " + "\"src-mac\":\"" + srcMac + "\", \"ingress-getPort()\":\"" + dstIngressPort + "\", " + "\"dst-mac\": \"" + dstMac + "\", \"active\":\"true\",\"vlan-id\":\"-1\", " + "\"actions\":\"output=" + dstOutput + "\"}";
                    String rule22 = "{\"switch\": \"" + dstSwitch + "\", \"name\":\"tmp2-2\", \"cookie\":\"0\", \"priority\":\"5\", " + "\"src-mac\":\"" + dstMac + "\", \"ingress-getPort()\":\"" + dstOutput + "\", " + "\"dst-mac\": \"" + srcMac + "\", \"active\":\"true\",\"vlan-id\":\"-1\", " + "\"actions\":\"output=" + dstIngressPort + "\"}";
                    List<String> rules = new ArrayList<>();
                    rules.add(rule11);
                    rules.add(rule12);
                    rules.add(rule21);
                    rules.add(rule22);
                    try {
                        new SDNSweep(null).pushFlows(rules);
                    } catch (IOException ex) {
                        Logger.getLogger(SDNControllerClient.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ParserConfigurationException ex) {
                        Logger.getLogger(SDNControllerClient.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (SAXException ex) {
                        Logger.getLogger(SDNControllerClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(SDNControllerClient.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        thread.start();
    }
}
Also used : DefaultWeightedEdge(org.jgrapht.graph.DefaultWeightedEdge) SDNSweep(nl.uva.cs.lobcder.catalogue.SDNSweep) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 4 with DefaultWeightedEdge

use of org.jgrapht.graph.DefaultWeightedEdge in project lobcder by skoulouzis.

the class SDNControllerClient method getShortestPath.

public List<DefaultWeightedEdge> getShortestPath(String dest, Set<String> sources) throws InterruptedException, IOException {
    if (graph == null) {
        graph = new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
    }
    Collection<SDNSweep.Switch> switchesColl = SDNSweep.getSwitches().values();
    List<SDNSweep.Switch> switches;
    if (switchesColl instanceof List) {
        switches = (List) switchesColl;
    } else {
        switches = new ArrayList(switchesColl);
    }
    for (int i = 0; i < switches.size(); i++) {
        List<Port> ports = switches.get(i).ports;
        if (ports != null) {
            for (int j = 0; j < ports.size(); j++) {
                for (int k = 0; k < ports.size(); k++) {
                    if (ports.get(j).state == 0 && ports.get(k).state == 0 && j != k) {
                        String vertex1 = switches.get(i).dpid + "-" + ports.get(j).portNumber;
                        String vertex2 = switches.get(i).dpid + "-" + ports.get(k).portNumber;
                        // Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "From: {0} to: {1}", new Object[]{vertex1, vertex2});
                        if (!graph.containsVertex(vertex1)) {
                            graph.addVertex(vertex1);
                        }
                        if (!graph.containsVertex(vertex2)) {
                            graph.addVertex(vertex2);
                        }
                        DefaultWeightedEdge e1;
                        if (!graph.containsEdge(vertex1, vertex2)) {
                            e1 = graph.addEdge(vertex1, vertex2);
                        } else {
                            e1 = graph.getEdge(vertex1, vertex2);
                        }
                        graph.setEdgeWeight(e1, 1);
                    }
                }
            }
        }
    }
    // Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "Destination: {0}", new Object[]{dest});
    if (!graph.containsVertex(dest)) {
        graph.addVertex(dest);
    }
    NetworkEntity destinationEntityArray = SDNSweep.getNetworkEntity(dest);
    // for (SDNSweep.NetworkEntity ne : destinationEntityArray) {
    if (destinationEntityArray != null) {
        for (AttachmentPoint ap : destinationEntityArray.getAttachmentPoint()) {
            String vertex = ap.getSwitchDPID() + "-" + ap.getPort();
            // Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "vertex: {0}", new Object[]{vertex});
            if (!graph.containsVertex(vertex)) {
                graph.addVertex(vertex);
            }
            DefaultWeightedEdge e1;
            if (!graph.containsEdge(dest, vertex)) {
                e1 = graph.addEdge(dest, vertex);
            } else {
                e1 = graph.getEdge(dest, vertex);
            }
            // Don't calculate the cost from the destination to the switch.
            // There is nothing we can do about it so why waste cycles ?
            // graph.setEdgeWeight(e1, 2);
            graph.setEdgeWeight(e1, getCost(dest, vertex));
        }
    }
    // List<NetworkEntity> sourceEntityArray = getNetworkEntity(sources);
    for (String s : sources) {
        NetworkEntity ne = SDNSweep.getNetworkEntity(s);
        if (ne != null) {
            for (String ip : ne.getIpv4()) {
                if (!graph.containsVertex(ip)) {
                    graph.addVertex(ip);
                }
                for (AttachmentPoint ap : ne.getAttachmentPoint()) {
                    String vertex = ap.getSwitchDPID() + "-" + ap.getPort();
                    if (!graph.containsVertex(vertex)) {
                        graph.addVertex(vertex);
                    }
                    DefaultWeightedEdge e2;
                    if (!graph.containsEdge(ip, vertex)) {
                        e2 = graph.addEdge(ip, vertex);
                    } else {
                        e2 = graph.getEdge(ip, vertex);
                    }
                    graph.setEdgeWeight(e2, getCost(ip, vertex));
                }
            }
        }
    }
    List<Link> links = SDNSweep.getSwitchLinks();
    for (Link l : links) {
        String srcVertex = l.srcSwitch + "-" + l.srcPort;
        if (!graph.containsVertex(srcVertex)) {
            graph.addVertex(srcVertex);
        }
        String dstVertex = l.dstSwitch + "-" + l.dstPort;
        if (!graph.containsVertex(dstVertex)) {
            graph.addVertex(dstVertex);
        }
        DefaultWeightedEdge e3;
        if (!graph.containsEdge(srcVertex, dstVertex)) {
            e3 = graph.addEdge(srcVertex, dstVertex);
        } else {
            e3 = graph.getEdge(srcVertex, dstVertex);
        }
        // Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "dstVertex: {0}", new Object[]{dstVertex});
        graph.setEdgeWeight(e3, getCost(srcVertex, dstVertex));
    }
    double cost = Double.MAX_VALUE;
    List<DefaultWeightedEdge> shortestPath = null;
    // exportGraph();
    StringBuilder msg = new StringBuilder();
    msg.append("\n");
    for (String s : sources) {
        if (graph.containsVertex(dest) && graph.containsVertex(s)) {
            List<DefaultWeightedEdge> shorPath = null;
            shorPath = DijkstraShortestPath.findPathBetween(graph, s, dest);
            double w = 0;
            if (shorPath != null) {
                for (DefaultWeightedEdge e : shorPath) {
                    w += graph.getEdgeWeight(e);
                }
                DefaultWeightedEdge p = shorPath.get(0);
                String e = graph.getEdgeSource(p);
                msg.append("source: ").append(e).append(" cost: ").append(w).append("\n");
                if (w <= cost) {
                    cost = w;
                    shortestPath = shorPath;
                    if (cost <= sources.size() + 1) {
                        break;
                    }
                }
            }
        }
    }
    Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, msg.toString());
    return shortestPath;
}
Also used : Port(nl.uva.cs.lobcder.catalogue.SDNSweep.Port) NetworkEntity(nl.uva.cs.lobcder.rest.wrappers.NetworkEntity) ArrayList(java.util.ArrayList) AttachmentPoint(nl.uva.cs.lobcder.rest.wrappers.AttachmentPoint) AttachmentPoint(nl.uva.cs.lobcder.rest.wrappers.AttachmentPoint) DefaultWeightedEdge(org.jgrapht.graph.DefaultWeightedEdge) ArrayList(java.util.ArrayList) List(java.util.List) Link(nl.uva.cs.lobcder.rest.wrappers.Link)

Example 5 with DefaultWeightedEdge

use of org.jgrapht.graph.DefaultWeightedEdge in project cogcomp-nlp by CogComp.

the class ShortestPathInference method addEdge.

public void addEdge(Node s, Node t, double score) {
    DefaultWeightedEdge e = graph.addEdge(s, t);
    double sc = getNodeScore(t) + score;
    graph.setEdgeWeight(e, sc);
    if (sc < 0)
        negativeWeights = true;
}
Also used : DefaultWeightedEdge(org.jgrapht.graph.DefaultWeightedEdge)

Aggregations

DefaultWeightedEdge (org.jgrapht.graph.DefaultWeightedEdge)18 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 IOException (java.io.IOException)4 List (java.util.List)3 TreeRangeSet (com.google.common.collect.TreeRangeSet)2 AlignmentSegment (cz1.ngs.model.AlignmentSegment)2 Blast6Segment (cz1.ngs.model.Blast6Segment)2 Sequence (cz1.ngs.model.Sequence)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SDNControllerClient (nl.uva.cs.lobcder.optimization.SDNControllerClient)2 BidiMap (org.apache.commons.collections4.BidiMap)2 DualHashBidiMap (org.apache.commons.collections4.bidimap.DualHashBidiMap)2 BFSShortestPath (cz1.ngs.model.BFSShortestPath)1 TraceableVertex (cz1.ngs.model.TraceableVertex)1 CigarElement (htsjdk.samtools.CigarElement)1