/** * Sets the covariance for the a<->b edge to the given covariance, if within range. Otherwise does * nothing. * * @param a a <-> b * @param b a <-> b * @param covar The covariance of a <-> b. * @return true if the coefficent was set (i.e. was within range), false if not. */ public boolean setErrorCovariance(Node a, Node b, final double covar) { Edge edge = Edges.bidirectedEdge(semGraph.getExogenous(a), semGraph.getExogenous(b)); if (edgeParameters.get(edge) == null) { throw new IllegalArgumentException("Not a covariance parameter in this model: " + edge); } if (editingEdge == null || !edge.equals(editingEdge)) { range = getParameterRange(edge); editingEdge = edge; } if (covar > range.getLow() && covar < range.getHigh()) { edgeParameters.put(edge, covar); return true; } else { return false; } // if (!paramInBounds(edge, coef)) { // edgeParameters.put(edge, d); // return false; // } // // edgeParameters.put(edge, coef); // return true; // if (!paramInBounds(edge, covar)) { // edgeParameters.put(edge, d); // return false; // } // // edgeParameters.put(edge, covar); // return true; }
/** * Sets the coefficient for the a->b edge to the given coefficient, if within range. Otherwise * does nothing. * * @param a a -> b * @param b a -> b * @param coef The coefficient of a -> b. * @return true if the coefficent was set (i.e. was within range), false if not. */ public boolean setEdgeCoefficient(Node a, Node b, final double coef) { Edge edge = Edges.directedEdge(a, b); if (edgeParameters.get(edge) == null) { throw new NullPointerException("Not a coefficient parameter in this model: " + edge); } if (editingEdge == null || !edge.equals(editingEdge)) { range = getParameterRange(edge); editingEdge = edge; } if (coef > range.getLow() && coef < range.getHigh()) { edgeParameters.put(edge, coef); return true; } return false; // if (!paramInBounds(edge, coef)) { // edgeParameters.put(edge, d); // return false; // } // // edgeParameters.put(edge, coef); // return true; }
/** * If the edge e is already in the list, return its index. * * @return index, if e is already in the list -1 otherwise */ public int findEdgeIndex(final Edge e) { int i = 0; for (final Edge edge : this.edges) { if (edge.equals(e)) { return i; } i++; } return -1; }
private static boolean isCandidateCloserToAnotherEdge( final double distanceToBeat, final Edge selectedEdgeToBeat, final Collection<Edge> edges, final Coordinate selectedCandidate) { for (final Edge edge : edges) { if (selectedEdgeToBeat.equals(edge)) { continue; } final double dist = calcDistance(edge.start, edge.end, selectedCandidate); if ((dist >= 0.0) && (dist < distanceToBeat)) { return true; } } return false; }
/** Construct depth-first spanning tree. */ void dfs(Node n, Set mark) { assert !mark.contains(n); n.dfsnum = mark.size(); mark.add(n); all_nodes.add(n); for (Iterator i = n.adj(); i.hasNext(); ) { Edge e = (Edge) i.next(); Node m = e.otherEnd(n); /* edge from n to m */ if (!mark.contains(m)) { /* a tree edge (child) */ n.children = new EdgeSList(e, n.children); m.parent = e; /* recurse */ dfs(m, mark); } else if (!e.equals(n.parent)) { /* a backedge */ n.backedges = new EdgeSList(e, n.backedges); } } }
@Test public void testEquals() { Node n1 = new Node(0, 0, 0); Node n2 = new Node(-2, 2, 1); Node n3 = new Node(5, 5, 2); Edge e1 = new Edge(n1, n2); Edge e2 = new Edge(n2, n1); Edge e3 = new Edge(n1, n3); Assert.assertTrue(e1.equals(e2)); Assert.assertTrue(e2.equals(e1)); Assert.assertFalse(e3.equals(e2)); Assert.assertFalse(e2.equals(e3)); Assert.assertFalse(e1.equals(e3)); Assert.assertFalse(e3.equals(e1)); }
/** * Method computes the amount of edge-crossing in a graph and displays the result on command-line */ public static String minEdgeCrossing_Test(OperatorGraph graph) { HashMap<GraphWrapper, GraphBox> boxes = graph.getBoxes(); LinkedList<Edge> edges = new LinkedList<Edge>(); int crossCounter = 0; // get all edges of the graph for (GraphWrapper gw : boxes.keySet()) { LinkedList<GraphWrapperIDTuple> children = gw.getSucceedingElements(); for (GraphWrapperIDTuple child : children) { Edge edge = new Edge(gw, child.getOperator(), 0); edges.add(edge); } } int edgecount = 0; LinkedList<Edge> visited = new LinkedList<Edge>(); for (int i = 0; i < edges.size(); i++ /** Edge edge1 : edges* */ ) { Edge edge1 = edges.get(i); edgecount++; GraphBox sourceE1 = boxes.get(edge1.getSource()); GraphBox targetE1 = boxes.get(edge1.getTarget()); // edge 1 is vertical if ((targetE1.getX() == sourceE1.getX())) { crossCounter = infinitSlope(edge1, edges, graph); } else { double m1 = ((double) targetE1.getY() - (double) sourceE1.getY()) / ((double) targetE1.getX() - (double) sourceE1.getX()); // slope of line 1 double b1 = (targetE1.getY()) - (m1 * targetE1.getX()); // slope of line 1 for (int j = 0; j < edges.size(); j++ /** Edge edge2 : edges* */ ) { Edge edge2 = edges.get(j); if ((!edge2.equals(edge1)) && (!visited.contains(edge2)) && (m1 != 0.0)) { GraphBox sourceE2 = boxes.get(edge2.getSource()); GraphBox targetE2 = boxes.get(edge2.getTarget()); // edge 2 is vertical if (targetE2.getX() == sourceE2.getX()) { crossCounter += infinitSlope(edge2, edge1, graph); } else { double m2 = ((double) targetE2.getY() - (double) sourceE2.getY()) / ((double) targetE2.getX() - (double) sourceE2.getX()); // slope of line 2 double b2 = (targetE2.getY()) - (m2 * targetE2.getX()); // crossing with y-axis of line 2 double x = ((b2 - b1) / (m1 - m2)); double y = ((m1 * x) + b1); // cross-point-coordinates of the 2 lines double maxXe1 = Math.max(sourceE1.getX(), targetE1.getX()); double maxXe2 = Math.max(sourceE2.getX(), targetE2.getX()); double minXe1 = Math.min(sourceE1.getX(), targetE1.getX()); double minXe2 = Math.min(sourceE2.getX(), targetE2.getX()); double maxYe1 = Math.max(sourceE1.getY(), targetE1.getY()); double maxYe2 = Math.max(sourceE2.getY(), targetE2.getY()); double minYe1 = Math.min(sourceE1.getY(), targetE1.getY()); double minYe2 = Math.min(sourceE2.getY(), targetE2.getY()); // test if cross-point is part of edge 1 if ((x < maxXe1) && (x > minXe1) && (y < maxYe1) && (y > minYe1)) { if ((x < maxXe2) && (x > minXe2) && (y < maxYe2) && (y > minYe2)) { crossCounter++; } } } // visited.add(edge2); } } } visited.add(edge1); } return "Test: Crossing Edges:\nThe number of edges crossing is " + crossCounter + " of total " + edgecount + " edges.\n"; }