[英]Error “illegalArgumentException: no such vertex in graph” but vertexSet() returns the vertex
我使用泛洪填充算法查看矩陣的1和2是否構成循環。
例如:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2 2 0 0 0
0 0 0 0 0 2 2 1 0 0
0 0 0 1 1 1 0 1 0 0
0 0 0 1 0 0 1 1 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
會變成:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2 2 0 0 0
0 0 0 0 0 2 2 3 0 0
0 0 0 3 3 3 0 3 0 0
0 0 0 3 0 0 3 3 0 0
0 0 0 3 3 3 3 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
我在給定的點(2,7)處初始化算法,並且該算法在鏈接到起點的每1中變為3s。 我還想實現一個有向圖(使用jgrapht),每個1是一個頂點,並且在兩個彼此相鄰的1之間創建一個邊。 因此,算法每次將1變成3時,都會使3成為頂點,並創建一個填充了最后一個點的邊。
注意:我使用reactivision,因此每個檢測到的標記都以1的形式添加到矩陣中。由於我現在無法使用10個標記進行測試,因此我在程序中添加了一個矩陣。
這是我的代碼執行此操作的部分:[編輯]:由於找不到錯誤,我現在粘貼了整個代碼
import TUIO.*;
import java.util.*;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
import org.joda.time.DateTime;
import org.jgrapht.alg.*;
import org.jgrapht.demo.*;
import java.net.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.alg.*;
TuioProcessing tuioClient;
// --------------------------------------------------------------
// Create matrix
static int[][] matrix = new int[10][10];
// Create Grid
int cols = 10, rows = 10;
int rectangleWidth = 100;
int rectangleHeight = 60;
// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15;
float object_size = 60;
float table_size = 760;
float scale_factor = 1;
boolean verbose = false; // print console debug messages
boolean callback = true; // updates only after callbacks
// Variables to mesure time
long startTime = 0;
long time ;
// Variables to caracterise each ficudial
int x, y, k, l, iD;
String myType;
// Types of components: V: Vertical / H: Horizontal /ER: Elbow Right / EL: Elbow Left / LI: Left Intersection / VI: Vertical Intersection
// Their position in the list matches their iD
String [] fiducialsList = {
"R300", "R3OO", "R600", "R600", "V", "V", "H", "ER", "H", "ER", "EL", "EL", "R100", "R100", "R900", "R900", "EL", "V", "V", "ER", "H", "H", "Box", "LI", "VI"
};
//Create the directed graph
public static DirectedGraph<Point, DefaultEdge> directedGraph = new DefaultDirectedGraph<Point, DefaultEdge>(DefaultEdge.class);
public static Point firstPoint = new Point(2,7);
// --------------------------------------------------------------
ArrayList<Fiducial> activList = new ArrayList<Fiducial>();
public class Fiducial {
public float x;
public float y;
public int iD;
public String myType;
public Fiducial(float x, float y, int iD, String myType)
{
this.myType = myType;
this.iD = iD;
this.x = x;
this.y = y;
}
@Override
public String toString() {
return ("[iD="+iD+" x="+x+" y="+y +" type="+myType+"]");
}
}
// --------------------------------------------------------------
//Time measurement: every lapse of time spent by the user on a circuit is stored here
ArrayList <Time> timeSpent = new ArrayList <Time> ();
public class Time {
public long time ;
public Time (long time)
{
this.time = time;
}
@Override
public String toString () {
return ("time spent on this circuit:" + time );
}
}
// --------------------------------------------------------------
void setup() {
size(1000, 600);
noCursor();
noStroke();
fill(0);
directedGraph.addVertex(new Point (2,7));
// periodic updates
if (!callback) {
frameRate(60); //<>//
loop();
} else noLoop(); // or callback updates
//int[][] array = new int[10][10];
//System.out.println(Arrays.deepToString(array));
tuioClient = new TuioProcessing(this);
System.out.println(Arrays.deepToString(matrix));
startTime = System.nanoTime();
System.out.println("#vertex: "+ directedGraph.vertexSet());
}
// --------------------------------------------------------------
void draw() {
// Begin loop for columns
for ( k = 0; k < cols; k++) {
// Begin loop for rows
for ( l = 0; l < rows; l++) {
fill(255);
stroke(0);
rect(k*rectangleWidth, l*rectangleHeight, rectangleWidth, rectangleHeight);
}
}
matrix [1][5]= 2;
matrix [1][6]= 2;
matrix [2][5]= 2;
matrix [2][6]= 2;
matrix [3][5]=1;
matrix [2][7]=1;
matrix [4][6]=1;
matrix [3][5]=1;
matrix [4][6]=1;
matrix [4][7]=0;
matrix [3][4]=1;
matrix [3][3]=1;
matrix [3][7]=1;
matrix [3][7]=1;
matrix [3][7]=1;
matrix [3][7]=1;
matrix [4][3]=1;
matrix [5][3]=1;
matrix [5][4]=1;
matrix [5][5]=1;
matrix [5][6]=1;
matrix [6][6]=1;
matrix [7][6]=1;
matrix [3][2]=1;
matrix [3][1]=1;
matrix [3][0]=1;
// Print Matrix
for (int i=0; i<matrix.length; i++) {
for (int j=0; j<matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
// This part detects the fiducial markers
float obj_size = object_size*scale_factor;
float cur_size = cursor_size*scale_factor;
ArrayList<TuioObject> tuioObjectList = tuioClient.getTuioObjectList();
for (int i=0; i<tuioObjectList.size (); i++) {
//System.out.println("#vertex: "+ directedGraph.vertexSet());
TuioObject tobj= tuioObjectList.get(i);
stroke(0);
fill(0, 0, 0);
pushMatrix();
translate(tobj.getScreenX(width), tobj.getScreenY(height));
rotate(tobj.getAngle());
rect(-80, -40, 80, 40);
popMatrix();
fill(255);
x = round(10*tobj.getX ());
y = round(10*tobj.getY ());
iD = tobj.getSymbolID();
// directedGraph.addVertex(new Point(x,y));
int taille = fiducialsList.length;
for (int o = 0; o<taille; o++) {
if (iD == o) {
myType = fiducialsList [o];
}
}
activList.add(new Fiducial (x, y, iD, myType));
matrix [x][y] = 1 ;
circuitState ();
for (int p = 0; p < 10; p++) {
for (int r = 0; r < 10; r++) {
System.out.print(matrix[p][r] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
}
System.out.println("#vertex: "+ directedGraph.vertexSet());
//Re-initialize matrix
for (int[] row : matrix)
Arrays.fill(row, 0);
}
// --------------------------------------------------------------
void circuitState () {
if ( matrix [2][7]==1 ) {
FloodFill.resolution(args);
if (matrix [3][5]== 3) {
System.out.println("Fermé");
} else {
long estimatedTime = System.nanoTime() - startTime;
timeSpent.add(new Time (time));
System.out.println(" Ouvert " + "took" + estimatedTime);
}
}
}
// --------------------------------------------------------------
// Implementation of the Flood Fill Algorithm to detect if circuit is closed or not
public static class FloodFill {
public static void resolution(String[] args) {
System.out.println("Found loop: "+checkIfPositionIsInLoop(matrix, 2, 7, 3));
//result
System.out.println("-------------------");
for (int i=0; i<matrix.length; i++) {
for (int j=0; j<matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
}
private static Direction direction;
public static boolean checkIfPositionIsInLoop(int[][] matrix, int x, int y, int fillValue) {
int targetX = x;
int targetY = y;
return fillReachesTargetPosition(matrix, x, y, targetX, targetY, fillValue, Direction.LEFT );
}
private static boolean fillReachesTargetPosition(int[][] matrix, int x, int y, int targetX, int targetY, int fillValue, Direction forbiddenDirection) {
if (x>=matrix.length)
return false;
if (y>=matrix[x].length)
return false;
int originValue=matrix[x][y];
matrix[x][y]=fillValue;
int xToFillNext;
int yToFillNext;
boolean fillingReachedTargetPosition = false;
// Up
xToFillNext = x-1;
yToFillNext = y;
if (xToFillNext==targetX && yToFillNext==targetY && !forbiddenDirection.equals(Direction.UP)) {
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
return true;
} else if (xToFillNext>=0 && originValue==matrix[xToFillNext][yToFillNext] && !forbiddenDirection.equals(Direction.UP)) {
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
fillingReachedTargetPosition =
fillReachesTargetPosition(matrix, xToFillNext, yToFillNext, targetX, targetY, fillValue, Direction.DOWN );
if (fillingReachedTargetPosition) {
return true;
}
}
// Right
xToFillNext = x;
yToFillNext = y+1;
if (xToFillNext==targetX && yToFillNext==targetY && !forbiddenDirection.equals(Direction.RIGHT)) {
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
return true;
} else if (yToFillNext<matrix[xToFillNext].length && originValue==matrix[xToFillNext][yToFillNext] && !forbiddenDirection.equals(Direction.RIGHT)) {
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
fillingReachedTargetPosition =
fillReachesTargetPosition(matrix, xToFillNext, yToFillNext, targetX, targetY, fillValue, Direction.LEFT );
if (fillingReachedTargetPosition) {
return true;
}
}
// Down
xToFillNext = x+1;
yToFillNext = y;
if (xToFillNext==targetX && yToFillNext==targetY && !forbiddenDirection.equals(Direction.DOWN)) {
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
return true;
} else if (xToFillNext<matrix.length && originValue==matrix[xToFillNext][yToFillNext] && !forbiddenDirection.equals(Direction.DOWN)) {
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
fillingReachedTargetPosition =
fillReachesTargetPosition(matrix, xToFillNext, yToFillNext, targetX, targetY, fillValue, Direction.UP );
if (fillingReachedTargetPosition) {
return true;
}
}
// Left
xToFillNext = x;
yToFillNext = y-1;
if (xToFillNext==targetX && yToFillNext==targetY && forbiddenDirection.equals(Direction.RIGHT)) {
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
return true;
} else if (yToFillNext>=0 && originValue==matrix[xToFillNext][yToFillNext] && !forbiddenDirection.equals(Direction.LEFT)) {
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
fillingReachedTargetPosition =
fillReachesTargetPosition(matrix, xToFillNext, yToFillNext, targetX, targetY, fillValue, Direction.RIGHT );
if (fillingReachedTargetPosition) {
return true;
}
}
return false;
}
}
// --------------------------------------------------------------
public static class DirectedGraphDemo {
public static void graph(String args[]) {
// constructs a directed graph with the specified vertices and edges
directedGraph.addVertex(firstPoint);
// prints the strongly connected components
System.out.println("Strongly connected components:");
for (int i = 0; i < stronglyConnectedSubgraphs.size(); i++) {
System.out.println(stronglyConnectedSubgraphs.get(i));
}
System.out.println();
}
}
// --------------------------------------------------------------
ArrayList<Point> pointList = new ArrayList<Point>();
public static class Point {
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public String toString() {
return ("[x="+x+" y="+y+"]");
}
}
// --------------------------------------------------------------
// called at the end of each TUIO frame
void refresh(TuioTime frameTime) {
if (verbose) println("frame #"+frameTime.getFrameID()+" ("+frameTime.getTotalMilliseconds()+")");
if (callback) redraw();
}
但是,當我運行它時,出現一個錯誤“ illegalArgumentException:圖形[x = 2 y = 7]中沒有這樣的頂點”,這是起點。 因此,我嘗試通過執行以下操作將此點定義為頂點:
public static Point firstPoint = new Point(2,7);
接着:
directedGraph.addVertex(new Point (2,7));
但它仍然無法正常工作,總是在這一行出現相同的錯誤: directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
但是在Flood Fill算法中的不同位置,具體取決於Reactivision在哪里檢測到標記(因此它位於算法的UP,DOWN,LEFT或RIGHT部分中)。
我完全被困住了。 我添加了一條打印語句,因此每次執行draw方法時,它都會返回圖形的頂點,並且[2,7]在那里...我無法弄清楚,有人可以幫助我嗎?
函數之外唯一可以擁有的就是聲明 。
這些是聲明:
int x, y, k, l, iD;
這是這樣的:
public static DirectedGraph<Point, DefaultEdge> directedGraph = new DefaultDirectedGraph<Point, DefaultEdge>(DefaultEdge.class);
但這不是:
directedGraph.addVertex(new Point (2,7));
您會收到“意外的令牌”編譯錯誤,因為在函數外不能有這樣的語句。
而是將此語句移到setup()
函數中。
您還可以使用初始化程序塊將代碼片段自動移到類構造函數中。 只需將其用大括號括起來即可:
{
directedGraph.addVertex(new Point (2,7));
}
發現我的錯誤:我必須添加:
Point myPoint = new Point(x, y);
Point myNextPoint = new Point(xToFillNext, yToFillNext);
directedGraph.addVertex(myPoint);
directedGraph.addVertex(myNextPoint);
directedGraph.addEdge(myPoint, myNextPoint);
在我的Flood Fill算法的每個部分中,而不是:
directedGraph.addVertex(new Point (x,y));
directedGraph.addVertex(new Point(xToFillNext,yToFillNext));
directedGraph.addEdge(new Point (x,y),new Point(xToFillNext,yToFillNext));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.