From e006ad06d5b1c65f0f89249dc9b6e7ab7b85ccb6 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:07:49 -0200 Subject: [PATCH 001/118] =?UTF-8?q?Adicionando=20artifato=20rcaller,=20par?= =?UTF-8?q?a=20manipular=20as=20fun=C3=A7=C3=B5es=20do=20R.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/pom.xml | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index fd99642..9f3d3dc 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -14,6 +14,20 @@ maven-repo http://arida.github.io/maven-repo/ + @@ -77,10 +91,21 @@ rtree 0.6.9 + + + + postgresql + postgresql + 9.1-901-1.jdbc4 + + - - From 4d3822cb9925b87b38d195f90c1c40ba1c69b9a5 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:09:42 -0200 Subject: [PATCH 002/118] =?UTF-8?q?Criando=20estutura=20de=20classes=20par?= =?UTF-8?q?a=20manipular=20dos=20entradas=20para=20a=20gera=C3=A7=C3=A3o?= =?UTF-8?q?=20da=20fun=C3=A7=C3=A3o=20das=20arestas.=20Concluido=20Entrada?= =?UTF-8?q?=20Database.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GeneratorFunctionPiecewiseCsv.java | 14 +++++ .../GeneratorFunctionPiecewiseDatabase.java | 59 +++++++++++++++++++ .../IGeneratorFunctionPiecewise.java | 7 +++ .../piecewise/PiecewiseException.java | 10 ++++ 4 files changed, 90 insertions(+) create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java create mode 100644 core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java create mode 100644 core/src/main/java/org/graphast/piecewise/PiecewiseException.java diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java new file mode 100644 index 0000000..b1d9fb0 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java @@ -0,0 +1,14 @@ +package org.graphast.piecewise; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +public class GeneratorFunctionPiecewiseCsv implements IGeneratorFunctionPiecewise { + + @Override + public double[][] getData() { + + return null; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java new file mode 100644 index 0000000..a74e1b2 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java @@ -0,0 +1,59 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.graphast.query.postgis.QueryPostgis; +import org.graphast.util.ConnectionJDBC; + +public class GeneratorFunctionPiecewiseDatabase implements IGeneratorFunctionPiecewise { + + private int FIELD_DATE_TIME = 1; + private int FIELD_DURACAO = 2; + + @Override + public double[][] getData() throws PiecewiseException { + + Connection connectionJDBC = null; + try { + connectionJDBC = ConnectionJDBC.getConnection(); + } catch (ClassNotFoundException | SQLException | IOException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); + } + + ResultSet resultSet = null; + double[][] matrixResult; + try { + Statement statement = connectionJDBC.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); + resultSet = statement.executeQuery(QueryPostgis.QUERY_ALL_DATE_TIME_WITH_DURATE); + + resultSet.last(); + int size = resultSet.getRow(); + resultSet.beforeFirst(); + + matrixResult = new double[size][2]; + + int i = 0; + while (resultSet.next()) { + + Double dateTime = resultSet.getDouble(FIELD_DATE_TIME); + Double duracao = resultSet.getDouble(FIELD_DURACAO); + double[] result = new double[2]; + result[0] = duracao; + result[1] = dateTime; + + matrixResult[i] = result; + i++; + } + + } catch (SQLException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu ao pegar as informações no DB."); + } + + + return matrixResult; + } +} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java new file mode 100644 index 0000000..14dcad7 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java @@ -0,0 +1,7 @@ +package org.graphast.piecewise; + + +public interface IGeneratorFunctionPiecewise { + + public double[][] getData() throws PiecewiseException; +} diff --git a/core/src/main/java/org/graphast/piecewise/PiecewiseException.java b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java new file mode 100644 index 0000000..28b8cb3 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + +public class PiecewiseException extends Exception { + + private static final long serialVersionUID = 1L; + + public PiecewiseException(String error) { + super(error); + } +} From 928b0c25eafdd75dc92f4cfe3dbb32fc59134717 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:10:26 -0200 Subject: [PATCH 003/118] =?UTF-8?q?Manipula=C3=A7=C3=A3o=20como=20Banco=20?= =?UTF-8?q?de=20Dados.=20Consulta,=20aquivo=20de=20configura=C3=A7=C3=A3o?= =?UTF-8?q?=20e=20classe=20de=20conex=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/db.properties | 11 ++++++ .../graphast/query/postgis/QueryPostgis.java | 7 ++++ .../org/graphast/util/ConnectionJDBC.java | 38 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 core/db.properties create mode 100644 core/src/main/java/org/graphast/query/postgis/QueryPostgis.java create mode 100644 core/src/main/java/org/graphast/util/ConnectionJDBC.java diff --git a/core/db.properties b/core/db.properties new file mode 100644 index 0000000..49e2234 --- /dev/null +++ b/core/db.properties @@ -0,0 +1,11 @@ +# remoto - nuvem arida +#driver=org.postgresql.Driver +#host=jdbc:postgresql://arida1.mooo.com:8080/fortaleza +#user=postgres +#password=aridapostgres12 + +#local +driver=org.postgresql.Driver +host=jdbc:postgresql://localhost:5432/piecewise +user=postgres +password=postgres \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java new file mode 100644 index 0000000..9271efa --- /dev/null +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -0,0 +1,7 @@ +package org.graphast.query.postgis; + +public interface QueryPostgis { + + String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; + +} diff --git a/core/src/main/java/org/graphast/util/ConnectionJDBC.java b/core/src/main/java/org/graphast/util/ConnectionJDBC.java new file mode 100644 index 0000000..86bb0ce --- /dev/null +++ b/core/src/main/java/org/graphast/util/ConnectionJDBC.java @@ -0,0 +1,38 @@ +package org.graphast.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +public class ConnectionJDBC { + + private static Connection connection = null; + private static final String FILE_NAME_PROPERTIES = "db.properties"; + private static final String STR_DRIVER = "driver"; + private static final String STR_HOST = "host"; + private static final String STR_USER = "user"; + private static final String STR_PASS = "password"; + + + public static Connection getConnection() throws ClassNotFoundException, + SQLException, IOException { + + if (connection == null) { + + Properties properties = new Properties(); + properties.load(new FileInputStream(new File(FILE_NAME_PROPERTIES))); + Class.forName(properties.getProperty(STR_DRIVER)); + String host = properties.getProperty(STR_HOST); + String user = properties.getProperty(STR_USER); + String password = properties.getProperty(STR_PASS); + + connection = DriverManager.getConnection(host, user, password); + } + + return connection; + } +} From beb8b0c483a6768b665c24d2d7834d39f5c3db51 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:10:46 -0200 Subject: [PATCH 004/118] =?UTF-8?q?Gerado=20da=20fun=C3=A7=C3=A3o=20para?= =?UTF-8?q?=20cada=20aresta=20do=20grafo.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/GeneratorFunctionPiecewise.java | 57 +++++++++++++++++++ ...eneratorFunctionPiecewiseDatabaseTest.java | 29 ++++++++++ 2 files changed, 86 insertions(+) create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java new file mode 100644 index 0000000..cb274bb --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java @@ -0,0 +1,57 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.util.Date; +import java.util.List; + +import rcaller.RCaller; +import rcaller.RCode; + +public class GeneratorFunctionPiecewise { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + + public double getValue(Date timeDay) { + + IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + + double[][] data = null; + try { + data = generatorFunctionPiecewise.getData(); + } catch (PiecewiseException e) { + e.printStackTrace(); + } + +// List executeLossFuntion = executeLossFuntion(data); + + return 0; + } + + private List executeLossFuntion(double[][] data ) throws PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addDoubleMatrix("dados.frame", data); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + try { + System.out.println(caller.getParser().getXMLFileAsString()); + } catch (IOException e) { + throw new PiecewiseException("[ERRO] Um erro quando estava sendo executada a função LOESS do R."); + } + + return null; + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java new file mode 100644 index 0000000..3d5722d --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -0,0 +1,29 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; + +import org.graphast.util.ConnectionJDBC; +import org.junit.Assert; +import org.junit.Test; + +public class GeneratorFunctionPiecewiseDatabaseTest { + + @Test + public void getConnectionTest() throws ClassNotFoundException, SQLException, IOException { + + Connection connection = ConnectionJDBC.getConnection(); + Assert.assertNotNull(connection); + } + + @Test + public void getDataTest() throws PiecewiseException { + + IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + double[][] data = generatorFunctionPiecewise.getData(); + int length = data.length; + + Assert.assertEquals(28, length); + } +} From d1d70385330cd4d3a5df174b962d4f65b7a6e466 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:11:21 -0200 Subject: [PATCH 005/118] =?UTF-8?q?Classe=20com=20testes=20unit=C3=A1rios?= =?UTF-8?q?=20para=20testes=20da=20biblioteca=20RCaller.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/RCallerEnvironmentsTest.java | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java new file mode 100644 index 0000000..71b28ad --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -0,0 +1,146 @@ +package org.graphast.piecewise; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +import rcaller.RCaller; +import rcaller.RCode; + +public class RCallerEnvironmentsTest { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + + @Test + public void createRscriptTest() { + + RCaller rcaller = new RCaller(); + rcaller.setRscriptExecutable(RSCRIPT); + Assert.assertNotNull(rcaller.getRCode()); + } + + @Test + public void baseLinearOutXMLTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("ols<-lm(y~x)"); + + caller.setRCode(code); + caller.runAndReturnResult("ols"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + + @Test + public void loessTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + + caller.setRCode(code); + caller.runAndReturnResult("dados.loess"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void loessWithRandomDataTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void loessWithSynteticDataTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void rcallerSetParameterTest() throws IOException, PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + double[][] d = generatorFunctionPiecewise.getData(); + + code.addDoubleMatrix("d", d); + code.addRCode("dados.frame <- data.frame(d)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("dados.loess"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + + } + + + + +} From c3e35392878aa2dcccd8c2876582b75372e8dc1c Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 14:36:53 -0200 Subject: [PATCH 006/118] =?UTF-8?q?Criada=20primeira=20fun=C3=A7=C3=A3o=20?= =?UTF-8?q?ap=C3=B3s=20o=20uso=20do=20loess?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/GeneratorFunctionPiecewise.java | 86 ++++++++++++------- ...eneratorFunctionPiecewiseDatabaseTest.java | 54 ++++++++++++ 2 files changed, 110 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java index cb274bb..829c5cb 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java @@ -1,9 +1,5 @@ package org.graphast.piecewise; -import java.io.IOException; -import java.util.Date; -import java.util.List; - import rcaller.RCaller; import rcaller.RCode; @@ -11,47 +7,77 @@ public class GeneratorFunctionPiecewise { private static final String RSCRIPT = "/usr/bin/Rscript"; - public double getValue(Date timeDay) { + public double getValue(double timestamp) throws PiecewiseException { IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - - double[][] data = null; - try { - data = generatorFunctionPiecewise.getData(); - } catch (PiecewiseException e) { - e.printStackTrace(); - } - -// List executeLossFuntion = executeLossFuntion(data); - - return 0; + double[][] coleta = generatorFunctionPiecewise.getData(); + return getFuntionEdge(coleta, timestamp); } - private List executeLossFuntion(double[][] data ) throws PiecewiseException { + private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { RCaller caller = new RCaller(); RCode code = caller.getRCode(); caller.setRscriptExecutable(RSCRIPT); - code.addDoubleMatrix("dados.frame", data); - code.addRCode("dados.loess <- loess(y ~ x)"); - code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); code.addRCode("y.predict <- predict(dados.loess, xl)"); code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); - code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); - + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + caller.setRCode(code); caller.runAndReturnResult("allObjects"); - try { - System.out.println(caller.getParser().getXMLFileAsString()); - } catch (IOException e) { - throw new PiecewiseException("[ERRO] Um erro quando estava sendo executada a função LOESS do R."); + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + + PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); + PontoGeometrico pontoFinalGeo = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + + double coeficienteAngular = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo); + double coeficienteLinear = getCoeficienteLinear(pontoInicialGeo, coeficienteAngular); + double y = coeficienteAngular * timestamp + coeficienteLinear; + + return y; + } + + private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { + double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); + return gama; + } + + private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { + double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); + return alfa; + } + + class PontoGeometrico { + + double x; + double y; + + public PontoGeometrico(double x, double y) { + this.x = x; + this.y= y; } - return null; + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } } } diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 3d5722d..c05c83f 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -8,7 +8,12 @@ import org.junit.Assert; import org.junit.Test; +import rcaller.RCaller; +import rcaller.RCode; + public class GeneratorFunctionPiecewiseDatabaseTest { + + private static final String RSCRIPT = "/usr/bin/Rscript"; @Test public void getConnectionTest() throws ClassNotFoundException, SQLException, IOException { @@ -26,4 +31,53 @@ public void getDataTest() throws PiecewiseException { Assert.assertEquals(28, length); } + + @Test + public void verifyPointsTest() throws IOException, PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + double[][] data = generatorFunctionPiecewise.getData(); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + System.out.println(caller.getParser().getXMLFileAsString().toString()); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + Assert.assertEquals(Double.valueOf("1201977368000"), Double.valueOf(pontoInicial[0])); + Assert.assertEquals(Double.valueOf("295522763826171"), Double.valueOf(pontoInicial[1])); + + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + int length = pontosInflexao.length; + Assert.assertEquals(6, length); + + Assert.assertEquals(Double.valueOf("1202063763696"), Double.valueOf(pontosInflexao[0])); + Assert.assertEquals(Double.valueOf("55328186.6718641"), Double.valueOf(pontosInflexao[length/2])); + } + + @Test + public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { + + GeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewise(); + double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); + + System.out.println(value); + Assert.assertNotNull(value); + } } From 3cea3f23cf587616ae8c61f1400384fd3d49be78 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 14:37:17 -0200 Subject: [PATCH 007/118] =?UTF-8?q?Removido=20teste=20unit=C3=A1rio=20para?= =?UTF-8?q?=20outra=20classe.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/RCallerEnvironmentsTest.java | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java index 71b28ad..e458b6a 100644 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -110,37 +110,4 @@ public void loessWithSynteticDataTest() throws IOException { System.out.println(caller.getParser().getXMLFileAsString()); Assert.assertNotNull(caller.getParser().getXMLFileAsString()); } - - @Test - public void rcallerSetParameterTest() throws IOException, PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - double[][] d = generatorFunctionPiecewise.getData(); - - code.addDoubleMatrix("d", d); - code.addRCode("dados.frame <- data.frame(d)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); - code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); - - caller.setRCode(code); - caller.runAndReturnResult("dados.loess"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - - } - - - - } From 43fd1928823d44a6b8a5b93e749a734e227c5e46 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 15:27:18 -0200 Subject: [PATCH 008/118] =?UTF-8?q?Finalizada=20fun=C3=A7=C3=A3o=20para=20?= =?UTF-8?q?o=20c=C3=A1lculo=20do=20tempo=20gasto=20para=20percorrer=20cada?= =?UTF-8?q?=20aresta=20de=20um=20grafo=20dependente=20do=20tempo.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/GeneratorFunctionPiecewise.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java index 829c5cb..4ba69b8 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java @@ -38,13 +38,38 @@ private double getFuntionEdge(double[][] data, double timestamp) throws Piecewis double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); - PontoGeometrico pontoFinalGeo = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); - double coeficienteAngular = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo); - double coeficienteLinear = getCoeficienteLinear(pontoInicialGeo, coeficienteAngular); - double y = coeficienteAngular * timestamp + coeficienteLinear; + double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); + double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); + - return y; + double yFinal = 0; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { + + PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); + double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); + double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); + + yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + + coeficienteAngularAnterior = coeficienteAngularCurrent; + coeficienteLinearAnterior = coeficienteLinearCurrent; + } + + double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); + + PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], + pontosInflexao[pontosInflexao.length - 1]); + PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); + + double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); + double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); + + yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); + + return yFinal; } private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { From a5272fdbc86725df1720940725220a986547459f Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 27 Nov 2015 09:12:38 -0200 Subject: [PATCH 009/118] =?UTF-8?q?Adicionado=20bin=C3=A1rio=20do=20R.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 32a4bd9..2c4b80d 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ null *.aux *.synctex* bin +core/.RData From 1f66faf3b099c2e071224d9bad05e055e89659bc Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:01:27 -0200 Subject: [PATCH 010/118] Add configure of the DB, with file properties, DAO and class java for connection. --- core/db.properties | 2 +- .../query/dao/postgis/GraphastDAO.java | 58 +++++++++++++++++++ .../query/dao/postgis/QueryPostgis.java | 12 ++++ .../org/graphast/util/ConnectionJDBC.java | 2 +- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/org/graphast/query/dao/postgis/GraphastDAO.java create mode 100644 core/src/main/java/org/graphast/query/dao/postgis/QueryPostgis.java diff --git a/core/db.properties b/core/db.properties index 49e2234..201ec27 100644 --- a/core/db.properties +++ b/core/db.properties @@ -6,6 +6,6 @@ #local driver=org.postgresql.Driver -host=jdbc:postgresql://localhost:5432/piecewise +host=jdbc:postgresql://localhost:5432/fortaleza_novo user=postgres password=postgres \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/dao/postgis/GraphastDAO.java b/core/src/main/java/org/graphast/query/dao/postgis/GraphastDAO.java new file mode 100644 index 0000000..5e2bf79 --- /dev/null +++ b/core/src/main/java/org/graphast/query/dao/postgis/GraphastDAO.java @@ -0,0 +1,58 @@ +package org.graphast.query.dao.postgis; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.graphast.model.Node; +import org.graphast.model.NodeImpl; +import org.graphast.util.ConnectionJDBC; +import org.postgis.Point; + +public class GraphastDAO { + + private static final int FIELD_ID_TAXI = 1; + private final static int FIELD_POINT_TAXI = 2; + private final static int FIELD_ID_ROAD = 3; + + public ResultSet getPoints(String table) throws ClassNotFoundException, SQLException, IOException { + + String finalQuery = String.format(QueryPostgis.QUERY_POINT_ROAD.replace("TABLE_NAME", "%s"), table); + Statement statement = ConnectionJDBC.getConnection().createStatement(); + + return statement.executeQuery(finalQuery); + } + private void addNodeInMap( Map> map, int idRoad, Node node) { + if (!map.containsKey(idRoad)) { + map.put(idRoad, new ArrayList()); + } + map.get(idRoad).add(node); + } + public Map> getPoiTaxiFortaleza() throws ClassNotFoundException, SQLException, IOException { + + Map> mapIdRoads = new HashMap>(); + + String finalQuery = String.format(QueryPostgis.QUERY_POINT_TAXI); + Statement statement = ConnectionJDBC.getConnection().createStatement(); + ResultSet result = statement.executeQuery(finalQuery); + + while (result.next()) { + + String strPointTaxi = result.getString(FIELD_POINT_TAXI); + Point point = new Point(strPointTaxi); + int idTaxi = result.getInt(FIELD_ID_TAXI); + int idRoad = result.getInt(FIELD_ID_ROAD); + Node node = new NodeImpl(idTaxi, point.getY(), point.getX()); + node.setCategory(idTaxi); + node.setLabel(Integer.valueOf(idRoad).toString()); + addNodeInMap(mapIdRoads, idRoad, node); + } + + return mapIdRoads; + } +} diff --git a/core/src/main/java/org/graphast/query/dao/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/dao/postgis/QueryPostgis.java new file mode 100644 index 0000000..9b5936d --- /dev/null +++ b/core/src/main/java/org/graphast/query/dao/postgis/QueryPostgis.java @@ -0,0 +1,12 @@ +package org.graphast.query.dao.postgis; + +public interface QueryPostgis { + + String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; + + String QUERY_POINT_ROAD = "SELECT ro.gid, ST_AsText(st_makeline(st_linemerge(ro.geom))) " + + "FROM TABLE_NAME ro GROUP BY ro.gid ORDER BY ro.gid;"; + + String QUERY_POINT_TAXI = "SELECT t.id, ST_AsText(t.point_geo), t.gid FROM view_taxi_close_linestring t"; + +} diff --git a/core/src/main/java/org/graphast/util/ConnectionJDBC.java b/core/src/main/java/org/graphast/util/ConnectionJDBC.java index 86bb0ce..b176224 100644 --- a/core/src/main/java/org/graphast/util/ConnectionJDBC.java +++ b/core/src/main/java/org/graphast/util/ConnectionJDBC.java @@ -21,7 +21,7 @@ public class ConnectionJDBC { public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException { - if (connection == null) { + if (connection == null || connection.isClosed()) { Properties properties = new Properties(); properties.load(new FileInputStream(new File(FILE_NAME_PROPERTIES))); From da10bc50df07e468be24bbb1d810edec2df7940f Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:04:17 -0200 Subject: [PATCH 011/118] Create importer of the OSM Fortaleza/Ce-Brazil and ploted taxi into graph. Data taxi is of Taxi Simples. --- .../org/graphast/importer/OSMDBImporter.java | 146 ++++++++++++++++++ .../importer/PoiTaxiFortalezaImporter.java | 100 ++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 core/src/main/java/org/graphast/importer/OSMDBImporter.java create mode 100644 core/src/main/java/org/graphast/importer/PoiTaxiFortalezaImporter.java diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java new file mode 100644 index 0000000..00f271c --- /dev/null +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -0,0 +1,146 @@ +package org.graphast.importer; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.graphast.model.Edge; +import org.graphast.model.EdgeImpl; +import org.graphast.model.GraphBounds; +import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.Node; +import org.graphast.model.NodeImpl; +import org.graphast.query.dao.postgis.GraphastDAO; +import org.graphast.util.ConnectionJDBC; +import org.graphast.util.DistanceUtils; +import org.graphast.util.GeoUtils; +import org.postgis.LineString; +import org.postgis.Point; + +public class OSMDBImporter implements Importer { + + private GraphastDAO dao; + private GraphBounds graph; + private String table; + private final int FIELD_ID_LINESTRING = 1; + private final int FIELD_LINESTRING = 2; + private final int SIZE_INTERVAL = 96; + + protected static final Logger LOGGER = Logger.getGlobal(); + + public OSMDBImporter(String table, String directory) { + this.table = table; + dao = new GraphastDAO(); + graph = new GraphBoundsImpl(directory); + } + + @Override + public GraphBounds execute() { + + try { + plotNodes(); + graph.createBounds(); + + } catch (SQLException | ClassNotFoundException | IOException e) { + System.err.println("[ERRO] Ocorreu um erro na construção do grafo."); + } + return graph; + } + + private void plotNodes() throws SQLException, ClassNotFoundException, IOException { + Map> mapTaxi = dao.getPoiTaxiFortaleza(); + + ResultSet result = dao.getPoints(table); + int pointCount = 0; + while (result.next()) { + LineString lineString = new LineString(result.getString(FIELD_LINESTRING)); + Point[] arrayPoints = lineString.getPoints(); + LOGGER.log(Level.INFO, String.format("registro: %s", result.getString(FIELD_LINESTRING))); + + + int idRoad = result.getInt(FIELD_ID_LINESTRING); + Node previousNode = null; + + for (Point point : arrayPoints) { + pointCount++; + LOGGER.log(Level.INFO, String.format("Point [x,y]: %s,%s", point.getX(), point.getY())); + Node node = new NodeImpl(point.getY(), point.getX()); + node.setLabel(Long.valueOf(idRoad).toString()); + Long nodeId = graph.getNodeId(GeoUtils.latLongToInt(node.getLatitude()), GeoUtils.latLongToInt(node.getLongitude())); + + if (nodeId != null) { + LOGGER.log(Level.INFO, String.format("point already exist in graph")); + node = graph.getNode(nodeId); + } else { + graph.addNode(node); + LOGGER.log(Level.INFO, String.format("point inserted in graph with ID: %s", node.getId())); + } + + + + if (previousNode != null && !previousNode.getId().equals(node.getId())) { + LOGGER.log(Level.INFO, String.format("Add edge from previous: %s to current: %s node", previousNode.getId(), node.getId())); + Edge edge = new EdgeImpl(idRoad, previousNode.getId() .longValue(), node.getId().longValue(), 0); + addCost(edge); + graph.addEdge(edge); + if(mapTaxi.containsKey(idRoad)) { + for(int i=0; i taxiNodes = getCloseTaxi(externalId); + Node nodeTo = graph.getNode(edge.getToNode()); + + for (Node node: taxiNodes) { + + if (!graph.getCategories().contains(node.getCategory())) { + graph.addNode(node); + Edge edgeToNeighbor = new EdgeImpl(node.getId(), nodeTo.getId(), 1); + addCostZero(edgeToNeighbor); + graph.addEdge(edgeToNeighbor); + } + } + } + + private void addCostZero(Edge edge) { + + int[] costs = new int[SIZE_INTERVAL]; + for (int i : costs) { + costs[i] = 2; + } + edge.setCosts(costs); + } + + private List getCloseTaxi(long idLineString) throws ClassNotFoundException, IOException { + + List taxiNodes = new ArrayList(); + try { + PreparedStatement taxiStatement = ConnectionJDBC.getConnection().prepareStatement(QueryPostgis.QUERY_POINT_TAXI); + taxiStatement.setLong(FIELD_PARAMETER_ID_LINESTRING, idLineString); + ResultSet resultSet = taxiStatement.executeQuery(); + + while (resultSet.next()) { + + String string = resultSet.getString(FIELD_POINT_TAXI); + Point point = new Point(string); + Node node = new NodeImpl(resultSet.getInt(FIELD_ID_TAXI), point.getY(), point.getX()); + node.setCategory(resultSet.getInt(FIELD_ID_TAXI)); + node.setExternalId(resultSet.getInt(FIELD_ID_TAXI)); + taxiNodes.add(node); + } + + ConnectionJDBC.getConnection().close(); + + } catch (SQLException e) { + e.printStackTrace(); + } + + return taxiNodes; + } + +} From 5d83a61ce4cd8afe8dd98deab04195bb60c9a192 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:06:38 -0200 Subject: [PATCH 012/118] add postgis dependency and add configuration main class for service run. Compare solution of the RNN. --- core/pom.xml | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 9f3d3dc..403c59d 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -14,20 +14,7 @@ maven-repo http://arida.github.io/maven-repo/ - + @@ -91,18 +78,18 @@ rtree 0.6.9 - - + postgresql postgresql 9.1-901-1.jdbc4 + + + org.postgis + postgis-jdbc + 1.3.3 + @@ -166,6 +153,26 @@ --> + + org.apache.maven.plugins + maven-shade-plugin + 2.4.2 + + + package + + shade + + + + + org.graphast.query.rnn.CompareRNNSearchsMethodsAnalysis + + + + + + From 2906f3e5cb9b6752c8dc0a17042374eac8b9ca64 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:09:55 -0200 Subject: [PATCH 013/118] Removed classes referente of the solution piecewise. --- .../piecewise/GeneratorFunctionPiecewise.java | 108 ----------------- .../GeneratorFunctionPiecewiseCsv.java | 14 --- .../GeneratorFunctionPiecewiseDatabase.java | 59 --------- .../IGeneratorFunctionPiecewise.java | 7 -- .../piecewise/PiecewiseException.java | 10 -- .../graphast/query/postgis/QueryPostgis.java | 7 -- ...eneratorFunctionPiecewiseDatabaseTest.java | 83 ------------- .../piecewise/RCallerEnvironmentsTest.java | 113 ------------------ 8 files changed, 401 deletions(-) delete mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java delete mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java delete mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java delete mode 100644 core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java delete mode 100644 core/src/main/java/org/graphast/piecewise/PiecewiseException.java delete mode 100644 core/src/main/java/org/graphast/query/postgis/QueryPostgis.java delete mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java delete mode 100644 core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java deleted file mode 100644 index 4ba69b8..0000000 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.graphast.piecewise; - -import rcaller.RCaller; -import rcaller.RCode; - -public class GeneratorFunctionPiecewise { - - private static final String RSCRIPT = "/usr/bin/Rscript"; - - public double getValue(double timestamp) throws PiecewiseException { - - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - double[][] coleta = generatorFunctionPiecewise.getData(); - return getFuntionEdge(coleta, timestamp); - } - - private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addDoubleMatrix("matrix", data); - code.addRCode("dados.frame <- data.frame(matrix)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); - code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); - double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); - - PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); - PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); - - double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); - double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); - - - double yFinal = 0; - for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { - - PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); - PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); - double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); - double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); - - yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); - - coeficienteAngularAnterior = coeficienteAngularCurrent; - coeficienteLinearAnterior = coeficienteLinearCurrent; - } - - double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); - - PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], - pontosInflexao[pontosInflexao.length - 1]); - PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); - - double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); - double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - - yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); - - return yFinal; - } - - private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { - double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); - return gama; - } - - private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { - double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); - return alfa; - } - - class PontoGeometrico { - - double x; - double y; - - public PontoGeometrico(double x, double y) { - this.x = x; - this.y= y; - } - - public double getX() { - return x; - } - public void setX(double x) { - this.x = x; - } - public double getY() { - return y; - } - public void setY(double y) { - this.y = y; - } - } -} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java deleted file mode 100644 index b1d9fb0..0000000 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.graphast.piecewise; - -import java.util.Date; -import java.util.List; -import java.util.Map; - -public class GeneratorFunctionPiecewiseCsv implements IGeneratorFunctionPiecewise { - - @Override - public double[][] getData() { - - return null; - } -} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java deleted file mode 100644 index a74e1b2..0000000 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.graphast.piecewise; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; - -import org.graphast.query.postgis.QueryPostgis; -import org.graphast.util.ConnectionJDBC; - -public class GeneratorFunctionPiecewiseDatabase implements IGeneratorFunctionPiecewise { - - private int FIELD_DATE_TIME = 1; - private int FIELD_DURACAO = 2; - - @Override - public double[][] getData() throws PiecewiseException { - - Connection connectionJDBC = null; - try { - connectionJDBC = ConnectionJDBC.getConnection(); - } catch (ClassNotFoundException | SQLException | IOException e) { - throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); - } - - ResultSet resultSet = null; - double[][] matrixResult; - try { - Statement statement = connectionJDBC.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - resultSet = statement.executeQuery(QueryPostgis.QUERY_ALL_DATE_TIME_WITH_DURATE); - - resultSet.last(); - int size = resultSet.getRow(); - resultSet.beforeFirst(); - - matrixResult = new double[size][2]; - - int i = 0; - while (resultSet.next()) { - - Double dateTime = resultSet.getDouble(FIELD_DATE_TIME); - Double duracao = resultSet.getDouble(FIELD_DURACAO); - double[] result = new double[2]; - result[0] = duracao; - result[1] = dateTime; - - matrixResult[i] = result; - i++; - } - - } catch (SQLException e) { - throw new PiecewiseException("[ERRO] Um erro ocorreu ao pegar as informações no DB."); - } - - - return matrixResult; - } -} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java deleted file mode 100644 index 14dcad7..0000000 --- a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.graphast.piecewise; - - -public interface IGeneratorFunctionPiecewise { - - public double[][] getData() throws PiecewiseException; -} diff --git a/core/src/main/java/org/graphast/piecewise/PiecewiseException.java b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java deleted file mode 100644 index 28b8cb3..0000000 --- a/core/src/main/java/org/graphast/piecewise/PiecewiseException.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.graphast.piecewise; - -public class PiecewiseException extends Exception { - - private static final long serialVersionUID = 1L; - - public PiecewiseException(String error) { - super(error); - } -} diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java deleted file mode 100644 index 9271efa..0000000 --- a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.graphast.query.postgis; - -public interface QueryPostgis { - - String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; - -} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java deleted file mode 100644 index c05c83f..0000000 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.graphast.piecewise; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.SQLException; - -import org.graphast.util.ConnectionJDBC; -import org.junit.Assert; -import org.junit.Test; - -import rcaller.RCaller; -import rcaller.RCode; - -public class GeneratorFunctionPiecewiseDatabaseTest { - - private static final String RSCRIPT = "/usr/bin/Rscript"; - - @Test - public void getConnectionTest() throws ClassNotFoundException, SQLException, IOException { - - Connection connection = ConnectionJDBC.getConnection(); - Assert.assertNotNull(connection); - } - - @Test - public void getDataTest() throws PiecewiseException { - - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - double[][] data = generatorFunctionPiecewise.getData(); - int length = data.length; - - Assert.assertEquals(28, length); - } - - @Test - public void verifyPointsTest() throws IOException, PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - double[][] data = generatorFunctionPiecewise.getData(); - - code.addDoubleMatrix("matrix", data); - code.addRCode("dados.frame <- data.frame(matrix)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); - code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - System.out.println(caller.getParser().getXMLFileAsString().toString()); - - double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); - Assert.assertEquals(Double.valueOf("1201977368000"), Double.valueOf(pontoInicial[0])); - Assert.assertEquals(Double.valueOf("295522763826171"), Double.valueOf(pontoInicial[1])); - - double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); - int length = pontosInflexao.length; - Assert.assertEquals(6, length); - - Assert.assertEquals(Double.valueOf("1202063763696"), Double.valueOf(pontosInflexao[0])); - Assert.assertEquals(Double.valueOf("55328186.6718641"), Double.valueOf(pontosInflexao[length/2])); - } - - @Test - public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { - - GeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewise(); - double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); - - System.out.println(value); - Assert.assertNotNull(value); - } -} diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java deleted file mode 100644 index e458b6a..0000000 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.graphast.piecewise; - -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Test; - -import rcaller.RCaller; -import rcaller.RCode; - -public class RCallerEnvironmentsTest { - - private static final String RSCRIPT = "/usr/bin/Rscript"; - - @Test - public void createRscriptTest() { - - RCaller rcaller = new RCaller(); - rcaller.setRscriptExecutable(RSCRIPT); - Assert.assertNotNull(rcaller.getRCode()); - } - - @Test - public void baseLinearOutXMLTest() throws IOException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - - code.addRCode("set.seed(123)"); - code.addRCode("x<-rnorm(10)"); - code.addRCode("y<-rnorm(10)"); - code.addRCode("ols<-lm(y~x)"); - - caller.setRCode(code); - caller.runAndReturnResult("ols"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - } - - - @Test - public void loessTest() throws IOException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addRCode("set.seed(123)"); - code.addRCode("x<-rnorm(10)"); - code.addRCode("y<-rnorm(10)"); - code.addRCode("dados.loess <- loess(y ~ x)"); - - caller.setRCode(code); - caller.runAndReturnResult("dados.loess"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - } - - @Test - public void loessWithRandomDataTest() throws IOException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - - caller.setRscriptExecutable(RSCRIPT); - - code.addRCode("set.seed(123)"); - code.addRCode("x<-rnorm(10)"); - code.addRCode("y<-rnorm(10)"); - code.addRCode("dados.loess <- loess(y ~ x)"); - code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); - code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - } - - @Test - public void loessWithSynteticDataTest() throws IOException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addRCode("set.seed(123)"); - code.addRCode("x<-rnorm(10)"); - code.addRCode("y<-rnorm(10)"); - code.addRCode("dados.loess <- loess(y ~ x)"); - code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); - code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - } -} From 7c083825876fe0ffbe69cb6fdcbbef1458888514 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:16:32 -0200 Subject: [PATCH 014/118] Create classes with naive (Dijkstra) and proposed solution to the problem of the nearest taxi. Solutions includes unit testing and comparison between class solutions (with synthetic and real graph). --- .../query/knn/RouteQueueRNNEntry.java | 25 + .../rnn/CompareRNNSearchsMethodsAnalysis.java | 121 +++++ .../graphast/query/rnn/IRNNTimeDependent.java | 13 + .../query/rnn/RNNBreadthFirstSearch.java | 130 +++++ .../graphast/query/rnn/RNNComparatorTest.java | 188 +++++++ .../query/rnn/RNNDepthFirstSearch.java | 83 +++ .../query/rnn/RouteQueueRNNEntry.java | 25 + .../graphgenerator/GraphGenerator.java | 510 ++++++++++++++++++ .../graphgenerator/GraphGeneratorGrid.java | 129 +++++ .../GraphGeneratorGridTest.java | 213 ++++++++ .../graphast/importer/OSMDBImporterTest.java | 52 ++ .../PoiTaxiFortalezaImporterTest.java | 26 + .../knn/RNNBreadthFirstSearchTest.java | 329 +++++++++++ .../graphast/knn/RNNDepthFirstSearchTest.java | 260 +++++++++ 14 files changed, 2104 insertions(+) create mode 100644 core/src/main/java/org/graphast/query/knn/RouteQueueRNNEntry.java create mode 100644 core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java create mode 100644 core/src/main/java/org/graphast/query/rnn/IRNNTimeDependent.java create mode 100644 core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java create mode 100644 core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java create mode 100644 core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java create mode 100644 core/src/main/java/org/graphast/query/rnn/RouteQueueRNNEntry.java create mode 100644 core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java create mode 100644 core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java create mode 100644 core/src/test/java/org/graphast/importer/OSMDBImporterTest.java create mode 100644 core/src/test/java/org/graphast/importer/PoiTaxiFortalezaImporterTest.java create mode 100644 core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java create mode 100644 core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java diff --git a/core/src/main/java/org/graphast/query/knn/RouteQueueRNNEntry.java b/core/src/main/java/org/graphast/query/knn/RouteQueueRNNEntry.java new file mode 100644 index 0000000..5d50d65 --- /dev/null +++ b/core/src/main/java/org/graphast/query/knn/RouteQueueRNNEntry.java @@ -0,0 +1,25 @@ +package org.graphast.query.knn; + +import java.util.List; + +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.route.shortestpath.model.TimeEntry; + +/** + * Representa um taxista na malha com a rota do cliente a este. + * + */ +public class RouteQueueRNNEntry extends TimeEntry { + + private List routes; + + public RouteQueueRNNEntry(long id, int travelTime, int arrivalTime, long parentId, List routes) { + super(id, travelTime, arrivalTime, parentId); + this.routes = routes; + } + + public List getRoutes() { + return routes; + } + +} diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java new file mode 100644 index 0000000..32d9393 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -0,0 +1,121 @@ +package org.graphast.query.rnn; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.logging.Logger; + +import org.graphast.config.Configuration; +import org.graphast.exception.PathNotFoundException; +import org.graphast.importer.OSMDBImporter; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.DateUtils; +import org.graphast.util.NumberUtils; + +public class CompareRNNSearchsMethodsAnalysis { + private static final String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + + protected static final Logger LOGGER = Logger.getGlobal(); + + public static void main(String[] args) throws IOException { + runAnalysis("view_exp_1k", Integer.parseInt(args[0])); + runAnalysis("view_exp_10k", Integer.parseInt(args[0])); + runAnalysis("view_exp_100k", Integer.parseInt(args[0])); + runAnalysis("view_exp_300mil", Integer.parseInt(args[0])); + } + + public static void runAnalysis(String tableName, int testTimes) throws IOException { + + OSMDBImporter importer = new OSMDBImporter(tableName, PATH_GRAPH+tableName); + GraphBounds graph = importer.execute(); + + OSMDBImporter importerReverse = new OSMDBImporter(tableName, PATH_GRAPH+tableName+"_reverse"); + GraphBounds graphReverse = importerReverse.execute(); + + + RNNDepthFirstSearch rnnDFS = new RNNDepthFirstSearch(graph); + RNNBreadthFirstSearch rnnBFS = new RNNBreadthFirstSearch(graphReverse); + + Date timeout = DateUtils.parseDate(00, 20, 00); + Date timestamp = DateUtils.parseDate(00, 00, 00); + + FileWriter rnnDFSFileCsv = new FileWriter(tableName+"_rnn_dfs.csv"); + FileWriter rnnBFSFileCsv = new FileWriter(tableName+"_rnn_bfs.csv"); + + for (int i = 0; i < testTimes; i++) { + Node customer = getRandomCustomerInGraph(graph); + runSearchAndWrite(graph, rnnDFS, customer, timeout, timestamp, rnnDFSFileCsv); + runSearchAndWrite(graph, rnnBFS, customer, timeout, timestamp, rnnBFSFileCsv); + } + + rnnBFSFileCsv.close(); + rnnDFSFileCsv.close(); + } + + private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, + Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { + try { + long startTime = System.currentTimeMillis(); + NearestNeighbor solution = rnn.search(customer, timeout, timestamp); + long endTime = System.currentTimeMillis(); + + long time = endTime - startTime; + + Long solutionId = null; + Integer distance = null; + Integer nodesSize = null; + ArrayList path = null; + Long externalId = null; + if(solution != null && solution.getPath()!=null) { + solutionId = solution.getId(); + distance = solution.getDistance(); + nodesSize = solution.getPath().size(); + path = solution.getPath(); + externalId = graph.getNode(solution.getId()).getExternalId(); + + String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); + String gidCustomer = customer.getLabel(); + + Node nodePoi = graph.getNode(solutionId); + String poiCoordinate = nodePoi.getLongitude() + "," + nodePoi.getLatitude(); + String gidPoi = nodePoi.getLabel(); + + String coordinateNodeVisited = ""; + String gidVisited = ""; + for (Long visited : path) { + Node nodeVisited = graph.getNode(visited); + coordinateNodeVisited = coordinateNodeVisited + "(" + nodeVisited.getLongitude() + "," + nodeVisited.getLatitude() + ")"; + + gidVisited = gidVisited + "-" + nodeVisited.getLabel(); + } + + + + String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, + poiCoordinate, time, solutionId, externalId, distance, + nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited) + "\n"; + + System.out.println(currentLine); + + fileCsv.write(currentLine); + } + } catch(PathNotFoundException e) { + System.err.println(String.format("Customer %s (%s, %s) has no POI in subgraph", customer.getId(), customer.getLatitude(), customer.getLongitude())); + } + } + + + private static Node getRandomCustomerInGraph(GraphBounds graph) { + Node node; + double[] bounds = new double[]{-3.710467, -38.591078, -3.802376, -38.465530}; + do { + long id = Double.valueOf(NumberUtils.generatePdseurandom(0, Long.valueOf(graph.getNumberOfNodes()-1).intValue())).longValue(); + node = graph.getNode(id); + } while(node.getCategory()!=-1 || node.getLatitude()>bounds[0] || node.getLatitude()bounds[3]); + return node; + } + +} diff --git a/core/src/main/java/org/graphast/query/rnn/IRNNTimeDependent.java b/core/src/main/java/org/graphast/query/rnn/IRNNTimeDependent.java new file mode 100644 index 0000000..2d2955e --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/IRNNTimeDependent.java @@ -0,0 +1,13 @@ +package org.graphast.query.rnn; + +import java.util.Date; + +import org.graphast.exception.PathNotFoundException; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; + +public interface IRNNTimeDependent { + + public NearestNeighbor search(Node root, Date timeout, Date timestamp) throws PathNotFoundException; + +} diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java new file mode 100644 index 0000000..1765b99 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -0,0 +1,130 @@ +package org.graphast.query.rnn; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Set; + +import org.graphast.exception.PathNotFoundException; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.DateUtils; + +public class RNNBreadthFirstSearch implements IRNNTimeDependent{ + + private GraphBounds graph; + + public RNNBreadthFirstSearch(GraphBounds graph) { + this.graph = graph; + this.graph.reverseGraph(); + } + + public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServiceTime) { + + if (graph.getPoi(customer.getId()) != null) { + ArrayList path = new ArrayList(); + path.add(customer.getId()); + return new NearestNeighbor(customer.getId(), 0, path); + } + + PriorityQueue queue = new PriorityQueue(); + Map> parents = new HashMap>(); + + long maxTravelTimeMilliseconds = DateUtils.dateToMilli(maxTravelTime); + long hourServiceTimeMilliseconds = DateUtils.dateToMilli(startServiceTime); + long startServiceTimeMilliseconds = hourServiceTimeMilliseconds + maxTravelTimeMilliseconds; + + init(customer, queue, parents, hourServiceTimeMilliseconds, startServiceTimeMilliseconds); + RouteQueueRNNEntry current = null; + Set visited = new HashSet<>(); + + while(!queue.isEmpty()) { + + current = queue.poll(); + if (visited.contains(current.getId())) { + continue; + } else { + visited.add(current.getId()); + } + + if(current.getTravelTime() > maxTravelTimeMilliseconds) { + throw new PathNotFoundException(String.format("not found path in reverse graph for parameter time %s milliseconds.", + maxTravelTimeMilliseconds)); + } + + if (graph.getPoi(current.getId()) != null) { + return new NearestNeighbor(current.getId(), current.getTravelTime(), pathToTaxi(current.getId(), customer.getId(), parents)); + } + + // Acessa os vizinhos do primeiro vértice da pilha, no caso os vizinho do vértice que representa o cliente. + HashMap neighbors = graph.accessNeighborhood(graph.getNode(current.getId()), current.getArrivalTime()); + + for (Node neighbor : neighbors.keySet()) { + if (visited.contains(neighbor.getId())) { + continue; + } + int travelTime = current.getTravelTime() + neighbors.get(neighbor); + if (travelTime > maxTravelTimeMilliseconds) { + continue; + } + + List parents_list = new ArrayList(); + parents_list.add(current.getId()); + parents.put(neighbor.getId(), parents_list); + + int arrivalTime = current.getArrivalTime() - neighbors.get(neighbor); + + RouteQueueRNNEntry newRouteQueueTaxiEntry = new RouteQueueRNNEntry(neighbor.getId(), travelTime, + arrivalTime, current.getId(), current.getRoutes()); + queue.offer(newRouteQueueTaxiEntry); + } + + + } + + throw new PathNotFoundException("not found path in reverse graph"); + } + + private ArrayList pathToTaxi(Long idTaxista, Long idCustomer, Map> parents) { + + ArrayList idsPath = new ArrayList(); + idsPath.add(idTaxista); + + Long idAnterior = idTaxista; + Set keySet = parents.keySet(); + for (Iterator iter = keySet.iterator(); iter.hasNext(); ) { + Long next = iter.next(); + if(next.equals(idAnterior) && !next.equals(idCustomer)) { + List list = parents.get(next); + for (Long long1 : list) { + idsPath.add(long1); + idAnterior = long1; + iter = keySet.iterator(); + } + } + } + if(!idsPath.contains(idCustomer)) { + idsPath.add(idCustomer); + } + + return idsPath; + } + + private void init(Node customer, PriorityQueue queue, Map> parents, long startServiceTime, long arrivedTime) { + + int travelTime = Long.valueOf(startServiceTime).intValue(); + int arrivalTime = Long.valueOf(arrivedTime).intValue(); + + List parents_list = new ArrayList(); + parents_list.add(Long.valueOf(-1)); + parents.put(customer.getId(), parents_list); + + queue.offer(new RouteQueueRNNEntry(customer.getId(), travelTime, arrivalTime, -1, new ArrayList())); + } +} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java b/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java new file mode 100644 index 0000000..a279b47 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java @@ -0,0 +1,188 @@ +package org.graphast.query.rnn; + +import java.io.IOException; +import java.text.ParseException; +import java.util.Date; + +import org.graphast.config.Configuration; +import org.graphast.graphgenerator.GraphGeneratorGrid; +import org.graphast.model.GraphBounds; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.DateUtils; +import org.graphast.util.FileUtils; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; + +public class RNNComparatorTest { + + private Integer idCustomer = null; + private Date endServiceTime = null; + private Date startServiceTime = null; + private Double percentagemPoi = null; + + @Before + public void setUp() throws ParseException, IOException { + + //Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(0); + + //Tempo para atendiemento - 23h 59m 59s + endServiceTime = DateUtils.parseDate(0, 20, 00); + + //Hora que ele realiza a chamada do serviço + startServiceTime = DateUtils.parseDate(00, 00, 00); + + percentagemPoi = Double.valueOf(1); + } + + // 1k (1024 pontos) + @Test + public void taxiSearchSytenticGraph1k() throws IOException, ParseException { + + for(int i=0; i<100; i++) { + + int comprimento = 32; + int altura = 32; + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + //==== SOLUÇÃO I ==== + long startSolution1 = System.currentTimeMillis(); + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution1 = System.currentTimeMillis(); + long timeSolution1 = endSolution1-startSolution1; + + graph.reverseGraph(); + + //==== SOLUÇÃO III ==== + long startSolution3 = System.currentTimeMillis(); + RNNBreadthFirstSearch taxiSearchRestritionsOSR = new RNNBreadthFirstSearch(graph); + NearestNeighbor solution3 = taxiSearchRestritionsOSR.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution3 = System.currentTimeMillis(); + long timeSolution3 = endSolution3-startSolution3; + + System.out.println(String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", timeSolution1, timeSolution3, solution1.getId(), solution3.getId(), solution1.getDistance(), + solution3.getDistance(), solution1.getPath().size(), solution3.getPath().size(), solution1.getPath(), solution3.getPath())); + } + } + + // 10k (10000 pontos) + @Test + public void taxiSearchSytenticGraph10k() throws IOException, ParseException { + + for(int i=0; i<100; i++) { + + int comprimento = 100; + int altura = 100; + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + //==== SOLUÇÃO I ==== + long startSolution1 = System.currentTimeMillis(); + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution1 = System.currentTimeMillis(); + long timeSolution1 = endSolution1-startSolution1; + + graph.reverseGraph(); + + //==== SOLUÇÃO III ==== + long startSolution3 = System.currentTimeMillis(); + RNNBreadthFirstSearch taxiSearchRestritionsOSR = new RNNBreadthFirstSearch(graph); + NearestNeighbor solution3 = taxiSearchRestritionsOSR.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution3 = System.currentTimeMillis(); + long timeSolution3 = endSolution3-startSolution3; + + System.out.println(String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", timeSolution1, timeSolution3, solution1.getId(), solution3.getId(), solution1.getDistance(), + solution3.getDistance(), solution1.getPath().size(), solution3.getPath().size(), solution1.getPath(), solution3.getPath())); + } + } + + // 100k (99856 pontos) + @Test + public void taxiSearchSytenticGraph100k() throws IOException, ParseException { + + for(int i=0; i<100; i++) { + + int comprimento = 316; + int altura = 316; + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + GraphBounds reverseGraph = graphSynthetic.getGraph(); + graph = reverseGraph; + reverseGraph.reverseGraph(); + + //==== SOLUÇÃO I ==== + long startSolution1 = System.currentTimeMillis(); + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution1 = System.currentTimeMillis(); + long timeSolution1 = endSolution1-startSolution1; + + + //==== SOLUÇÃO III ==== + long startSolution3 = System.currentTimeMillis(); + RNNBreadthFirstSearch taxiSearchRestritionsOSR = new RNNBreadthFirstSearch(reverseGraph); + NearestNeighbor solution3 = taxiSearchRestritionsOSR.search(reverseGraph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution3 = System.currentTimeMillis(); + long timeSolution3 = endSolution3-startSolution3; + + System.out.println(String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", timeSolution1, timeSolution3, solution1.getId(), solution3.getId(), solution1.getDistance(), + solution3.getDistance(), solution1.getPath().size(), solution3.getPath().size(), solution1.getPath(), solution3.getPath())); + } + } + + // 1G (1000000 pontos) + @Test + public void taxiSearchSytenticGraph1000k() throws IOException, ParseException { + + for(int i=0; i<10; i++) { + + int comprimento = 1000; + int altura = 1000; + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + GraphBounds reverseGraph = graphSynthetic.getGraph(); + graph = reverseGraph; + reverseGraph.reverseGraph(); + + //==== SOLUÇÃO I ==== + long startSolution1 = System.currentTimeMillis(); + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution1 = System.currentTimeMillis(); + long timeSolution1 = endSolution1-startSolution1; + + + //==== SOLUÇÃO III ==== + long startSolution3 = System.currentTimeMillis(); + RNNBreadthFirstSearch taxiSearchRestritionsOSR = new RNNBreadthFirstSearch(reverseGraph); + NearestNeighbor solution3 = taxiSearchRestritionsOSR.search(reverseGraph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution3 = System.currentTimeMillis(); + long timeSolution3 = endSolution3-startSolution3; + + System.out.println(String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", timeSolution1, timeSolution3, solution1.getId(), solution3.getId(), solution1.getDistance(), + solution3.getDistance(), solution1.getPath().size(), solution3.getPath().size(), solution1.getPath(), solution3.getPath())); + } + } + + + @AfterClass + public static void tearDown() { + + FileUtils.deleteDir(Configuration.USER_HOME + "/graphhopper/test"); + FileUtils.deleteDir(Configuration.USER_HOME + "/graphast/test"); + } +} diff --git a/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java new file mode 100644 index 0000000..be0106a --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java @@ -0,0 +1,83 @@ +package org.graphast.query.rnn; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.graphast.exception.PathNotFoundException; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.route.shortestpath.dijkstra.Dijkstra; +import org.graphast.query.route.shortestpath.dijkstra.DijkstraLinearFunction; +import org.graphast.query.route.shortestpath.model.Path; +import org.graphast.util.DateUtils; + +public class RNNDepthFirstSearch implements IRNNTimeDependent { + + private GraphBounds graph; + + public RNNDepthFirstSearch(GraphBounds graphBounds) { + this.graph = graphBounds; + } + + public NearestNeighbor search(Node root, Date timeout, Date timestamp) throws PathNotFoundException { + + long maxTravelTimeMillisenconds = DateUtils.dateToMilli(timeout); + double bestTravelTime = maxTravelTimeMillisenconds; + long currentPoi = -1; + Path pathResult = null; + + Dijkstra dijkstraShortestPathLinearFunction = new DijkstraLinearFunction(graph); + + for (Long poi : graph.getPoiIds()) { + try { + Node target = graph.getNode(poi); + Path path = dijkstraShortestPathLinearFunction.shortestPath( + target.getId(), root.getId(), timestamp); + + if (path.getTotalCost() <= maxTravelTimeMillisenconds + && path.getTotalCost() <= bestTravelTime) { + currentPoi = target.getId(); + bestTravelTime = path.getTotalCost(); + pathResult = path; + } + } catch(PathNotFoundException e) { +// System.err.println(e.getMessage()); + } + } + + NearestNeighbor nearestNeighbor = createNN(root, currentPoi, pathResult); + + if (nearestNeighbor == null) { + throw new PathNotFoundException( + "target not found for root and set timestamp"); + } + + return nearestNeighbor; + } + + private NearestNeighbor createNN(Node root, long currentPoi, Path path) { + + NearestNeighbor nearestNeighbor = new NearestNeighbor(); + if (currentPoi > -1) { + + nearestNeighbor.setDistance((int) path.getTotalCost()); + nearestNeighbor.setId(currentPoi); + + ArrayList arrayPath = new ArrayList(); + List edges = path.getEdges(); + + if (edges != null) { + for (Long edge : edges) { + arrayPath.add(graph.getEdge(edge).getFromNode()); + } + } + + arrayPath.add(root.getId()); + nearestNeighbor.setPath(arrayPath); + } + + return nearestNeighbor; + } +} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/rnn/RouteQueueRNNEntry.java b/core/src/main/java/org/graphast/query/rnn/RouteQueueRNNEntry.java new file mode 100644 index 0000000..4a384c7 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/RouteQueueRNNEntry.java @@ -0,0 +1,25 @@ +package org.graphast.query.rnn; + +import java.util.List; + +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.route.shortestpath.model.TimeEntry; + +/** + * Representa um taxista na malha com a rota do cliente a este. + * + */ +public class RouteQueueRNNEntry extends TimeEntry { + + private List routes; + + public RouteQueueRNNEntry(long id, int travelTime, int arrivalTime, long parentId, List routes) { + super(id, travelTime, arrivalTime, parentId); + this.routes = routes; + } + + public List getRoutes() { + return routes; + } + +} diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java b/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java index eb1c5fc..65f450b 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java @@ -1,5 +1,6 @@ package org.graphast.graphgenerator; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -537,5 +538,514 @@ public Graph generateAndorra() { return graph; } + +public GraphBounds generateExampleTAXI() { + + GraphBounds graph = new GraphBoundsImpl(Configuration.USER_HOME + "/graphast/test/examplePoI"); + + Node node; + Edge edge; + + node = new NodeImpl(0l, 0.0d, 1.0d); + graph.addNode(node); + + node = new NodeImpl(1l, 0.0d, 10.0d); + int[] costs = new int[]{0}; + node.setCategory(1); + node.setLabel("TAXI I"); + node.setCosts(costs); + graph.addNode(node); + + node = new NodeImpl(2l, 0.0d, 20.0d); + graph.addNode(node); + + node = new NodeImpl(3l, 0.0d, 30.0d); + graph.addNode(node); + + node = new NodeImpl(4l, 0.0d, 40.0d); + costs = new int[]{0}; + node.setCategory(2); + node.setLabel("TAXI II"); + node.setCosts(costs); + graph.addNode(node); + + node = new NodeImpl(5l, 10.0d, 0.0d); + graph.addNode(node); + + node = new NodeImpl(6l, 10.0d, 10.0d); + graph.addNode(node); + + node = new NodeImpl(7l, 10.0d, 20.0d); + graph.addNode(node); + + node = new NodeImpl(8l, 10.0d, 30.0d); + graph.addNode(node); + + node = new NodeImpl(9l, 10.0d, 40.0d); + costs = new int[]{0}; + node.setCategory(3); + node.setLabel("TAXI III"); + node.setCosts(costs); + graph.addNode(node); + + //EDGES + + edge = new EdgeImpl(1l, 0l, 1l, 15); + int[] costsEdge1 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000}; + edge.setCosts(costsEdge1); + graph.addEdge(edge); + + edge = new EdgeImpl(2l, 1l, 2l, 15); + int[] costsEdge2 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge2); + graph.addEdge(edge); + + edge = new EdgeImpl(3l, 1l, 7l, 12); + int[] costsEdge3 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge3); + graph.addEdge(edge); + + edge = new EdgeImpl(4l, 2l, 3l, 10); + int[] costsEdge4 = new int[]{600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge4); + graph.addEdge(edge); + + edge = new EdgeImpl(5l, 3l, 4l, 12); + int[] costsEdge5 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000}; + edge.setCosts(costsEdge5); + graph.addEdge(edge); + + edge = new EdgeImpl(6l, 4l, 8l, 12); + int[] costsEdge6 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge6); + graph.addEdge(edge); + + edge = new EdgeImpl(7l, 4l, 9l, 12); + int[] costsEdge7 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge7); + graph.addEdge(edge); + + edge = new EdgeImpl(8l, 5l, 0l, 12); + int[] costsEdge8 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000}; + edge.setCosts(costsEdge8); + graph.addEdge(edge); + + edge = new EdgeImpl(9l, 6l, 5l, 15); + int[] costsEdge9 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge9); + graph.addEdge(edge); + + edge = new EdgeImpl(10l, 7l, 2l, 15); + int[] costsEdge10 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge10); + graph.addEdge(edge); + + edge = new EdgeImpl(11l, 7l, 6l, 12); + int[] costsEdge11 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge11); + graph.addEdge(edge); + + edge = new EdgeImpl(12l, 8l, 7l, 12); + int[] costsEdge12 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge12); + graph.addEdge(edge); + + edge = new EdgeImpl(13l, 9l, 8l, 15); + int[] costsEdge13 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge13); + graph.addEdge(edge); + + graph.createBounds(); + + return graph; + } + + public GraphBounds generateExampleTaxi15to15minutes() { + + String separator = File.separator; + final String directory = Configuration.USER_HOME + separator + + "graphast" + separator + "test" +separator + "exampleTaxi"; + + GraphBounds graph = new GraphBoundsImpl(directory); + + graph.addNode(new NodeImpl(0l, 0.0d, 1.0d)); + + Node nodeCategory1 = new NodeImpl(1l, 0.0d, 10.0d); + int[] costs = new int[]{0}; + nodeCategory1.setCategory(1); + nodeCategory1.setLabel("TAXI I"); + nodeCategory1.setCosts(costs); + graph.addNode(nodeCategory1); + + graph.addNode(new NodeImpl(2l, 0.0d, 20.0d)); + graph.addNode(new NodeImpl(3l, 0.0d, 30.0d)); + + Node nodeCategory2 = new NodeImpl(4l, 0.0d, 40.0d); + costs = new int[]{0}; + nodeCategory2.setCategory(2); + nodeCategory2.setLabel("TAXI II"); + nodeCategory2.setCosts(costs); + graph.addNode(nodeCategory2); + + graph.addNode(new NodeImpl(5l, 10.0d, 0.0d)); + graph.addNode(new NodeImpl(6l, 10.0d, 10.0d)); + graph.addNode(new NodeImpl(7l, 10.0d, 20.0d)); + graph.addNode(new NodeImpl(8l, 10.0d, 30.0d)); + + Node nodeCategory3 = new NodeImpl(9l, 10.0d, 40.0d); + costs = new int[]{0}; + nodeCategory3.setCategory(3); + nodeCategory3.setLabel("TAXI III"); + nodeCategory3.setCosts(costs); + graph.addNode(nodeCategory3); + + //Edges com custo de 15 em 15 minutos + + Edge edge = new EdgeImpl(1l, 0l, 1l, 15); + int[] costsEdge1 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000,900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000,900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000}; + edge.setCosts(costsEdge1); + graph.addEdge(edge); + + edge = new EdgeImpl(2l, 1l, 2l, 15); + int[] costsEdge2 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge2); + graph.addEdge(edge); + + Edge edge1to7 = new EdgeImpl(3l, 1l, 7l, 12); + int[] costsEdge1To7 = new int[]{960000, 960000, 300000, 300000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 300000, 300000, 1800000, 720000, 720000, + 720000, 720000, 720000}; + edge1to7.setCosts(costsEdge1To7); + graph.addEdge(edge1to7); + + Edge edge7to1 = new EdgeImpl(3l, 7l, 1l, 12); + int[] costsEdge7to1 = new int[]{600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000}; + edge7to1.setCosts(costsEdge7to1); + graph.addEdge(edge7to1); + + edge = new EdgeImpl(4l, 2l, 3l, 10); + int[] costsEdge4 = new int[]{600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge4); + graph.addEdge(edge); + + edge = new EdgeImpl(5l, 3l, 4l, 12); + int[] costsEdge5 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000}; + edge.setCosts(costsEdge5); + graph.addEdge(edge); + + edge = new EdgeImpl(6l, 4l, 8l, 12); + int[] costsEdge6 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge6); + graph.addEdge(edge); + + edge = new EdgeImpl(7l, 4l, 9l, 12); + int[] costsEdge7 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge7); + graph.addEdge(edge); + + edge = new EdgeImpl(8l, 5l, 0l, 12); + int[] costsEdge8 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000}; + edge.setCosts(costsEdge8); + graph.addEdge(edge); + + Edge edge6to5 = new EdgeImpl(9l, 6l, 5l, 15); + int[] costsEdge6to5 = new int[]{900000, 900000, 960000, 960000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000}; + edge6to5.setCosts(costsEdge6to5); + graph.addEdge(edge6to5); + + Edge edge5to6 = new EdgeImpl(9l, 5l, 6l, 15); + int[] costsEdge5to6 = new int[]{600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000}; + edge5to6.setCosts(costsEdge5to6); + graph.addEdge(edge5to6); + + edge = new EdgeImpl(10l, 7l, 2l, 15); + int[] costsEdge10 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge10); + graph.addEdge(edge); + + edge = new EdgeImpl(11l, 7l, 6l, 12); + int[] costsEdge11 = new int[]{720000, 720000, 480000, 480000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 780000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 780000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 780000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 120000, 120000, 1080000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge11); + graph.addEdge(edge); + + edge = new EdgeImpl(12l, 8l, 7l, 12); + int[] costsEdge12 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge12); + graph.addEdge(edge); + + edge = new EdgeImpl(13l, 9l, 8l, 15); + int[] costsEdge13 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge13); + graph.addEdge(edge); + + graph.createBounds(); + + return graph; + } + } \ No newline at end of file diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java new file mode 100644 index 0000000..a38b9b4 --- /dev/null +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java @@ -0,0 +1,129 @@ +package org.graphast.graphgenerator; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +import org.graphast.config.Configuration; +import org.graphast.model.Edge; +import org.graphast.model.EdgeImpl; +import org.graphast.model.GraphBounds; +import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.Node; +import org.graphast.model.NodeImpl; + +public class GraphGeneratorGrid { + + private int width; + private int length; + private GraphBounds graph; + private double percentagemPoi; + private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + + public GraphGeneratorGrid(int width, int length, double percentualPoi) { + this.width = width; + this.length = length; + this.percentagemPoi = percentualPoi; + this.graph = new GraphBoundsImpl(PATH_GRAPH); + } + + public void generateGraph() { + plotNodes(); + plotEdges(); + graph.createBounds(); + } + + private void plotNodes() { + + BigDecimal interador_largura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(width), 8, RoundingMode.HALF_UP); + BigDecimal interador_altura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(length), 8, RoundingMode.HALF_UP); + + Set listaIdsPoi = getListIdsPois(); + + Integer category = 0; + for (int i = 0; i < width; i++) { + BigDecimal latitude = interador_altura.multiply(BigDecimal.valueOf(i)).add(BigDecimal.valueOf(-90)); + for (int j = 0; j < length; j++) { + BigDecimal longitude = interador_largura.multiply(BigDecimal.valueOf(j)).add(BigDecimal.valueOf(-90));; + Node node = new NodeImpl(Long.valueOf(category), latitude.doubleValue(), longitude.doubleValue()); + if(listaIdsPoi.contains(category)) { + int[] costs = new int[]{0}; + node.setCategory(category); + node.setLabel("CATEGORY "+category); + node.setCosts(costs); + listaIdsPoi.remove(category); + } + graph.addNode(node); + category++; + } + } + } + + private Set getListIdsPois() { + + int quantidadeVerticesPoi = BigDecimal.valueOf(width).multiply(BigDecimal.valueOf(length)).multiply(BigDecimal.valueOf(percentagemPoi)).divide(BigDecimal.valueOf(100.0f), 8, RoundingMode.UP).intValue(); + + Set listIdPoi = new HashSet<>(); + do { + int rangeMax = width*length - 1; + Double idRandom = generatePdseurandom(0, rangeMax); + listIdPoi.add(idRandom.intValue()); + } while(listIdPoi.size() path = new ArrayList<>(); + path.add(4l); + assertEquals("Deve retornar o caminho esperado.", path.get(0), nearestNeighbor.getPath().get(0)); + } + + // TESTE 1.1: (EXCEPTION) Nenhum taxista é encontrado na malha, para a quantidade de tempo + // superior necessario para atendimento (11 minutos e 59 segundos) + @Test(expected = PathNotFoundException.class) + public void taxiSearchExpected_II() throws ParseException, IOException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(8); + + // Tempo para atendiemento + maxTravelTime = DateUtils.parseDate(0, 11, 59); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + } + + // TESTE 2: O cliente está em um vértice vizinho ao taxista. CLIENTE -> TAXISTA (NÃO REVERSO) + @Test + public void taxiSearchNeighbor() throws IOException, ParseException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(7); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + ArrayList path_result = new ArrayList<>(); + path_result.add(4l); + path_result.add(3l); + path_result.add(2l); + path_result.add(7l); + + assertEquals("Deve retornar o vid esperado.", 4l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 37, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // TESTE 2.1: O cliente está em um vértice vizinho ao taxista. CLIENTE -> TAXISTA (REVERSO) + @Test + public void taxiSearchNeighborReverso() throws IOException, ParseException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(7); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 12, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, custumer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolution() throws IOException, ParseException { + + //Cliente que realiza a chamada do serviço + Integer idCustomer = Integer.valueOf(5); + + //Tempo para atendiemento - 39 minutos + Date serviceTime = DateUtils.parseDate(0, 39, 0); + + //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundos + Date hourServiceTime = DateUtils.parseDate(00, 00, 00); + + graphBounds = new GraphGenerator().generateExampleTAXI(); + graphBoundsReverse = new GraphGenerator().generateExampleTAXI(); + graphBoundsReverse.reverseGraph(); + + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), serviceTime, hourServiceTime); + + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 39, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + + // TESTE 3: O cliente está em um vértice vizinho a dois taxistas. Mas com + // pesos das aresta para chegar ao cliente diferente. Cliente:8; Taxista 1: 4, 12 minutos; Taxista 1: 9, 15 minutos. + @Test + public void taxiSearchNeighborWithDifferentWeight() throws IOException, ParseException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(8); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + ArrayList path_result = new ArrayList<>(); + path_result.add(4l); + path_result.add(8l); + + assertEquals("Deve retornar o vid esperado.", 4l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 12, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // TESTE 4: O cliente está em um vértice vizinho a dois taxistas. Mas com + // pesos das arestas para chegar ao cliente iguais + @Test + public void taxiSearchNeighborWithEqualsWeight() throws IOException, ParseException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(8); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + ArrayList path_result = new ArrayList<>(); + path_result.add(4l); + path_result.add(8l); + + //Retorna o que possui o menor tempo para a travessia, a Queue realiza o compare para a ordenação. + assertEquals("Deve retornar o vid esperado.", 4l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 12, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolutionTimeForTheCallMidnight() throws IOException, ParseException { + + GraphBounds graphBoundsReverse = new GraphGenerator().generateExampleTaxi15to15minutes(); + graphBoundsReverse.reverseGraph(); + + //Tempo para atendiemento - 40 minutos + maxTravelTime = DateUtils.parseDate(00, 38, 00); + + //Hora que ele realiza a chamada do serviço - meia-noite + hourServiceTime = DateUtils.parseDate(00, 00, 00); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(5); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 28, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolutionTimeForTheCallHourServiceInit() throws IOException, ParseException { + + GraphBounds graphBoundsReverse = new GraphGenerator().generateExampleTaxi15to15minutes(); + graphBoundsReverse.reverseGraph(); + + //Tempo para atendiemento - 40 minutos + maxTravelTime = DateUtils.parseDate(2, 52, 00); + + //Hora que ele realiza a chamada do serviço - meia-noite e quinze minutos + hourServiceTime = DateUtils.parseDate(00, 15, 00); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(5); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 52, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + @Test + public void taxiBetterWaytRandomGraphOneByOne() throws ParseException { + + setUpRandomGraph(1,1,1); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(0); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + assertEquals("Deve retornar o vid esperado.", 0l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 0, nearestNeighbor.getDistance()/1000/60); + + ArrayList path = new ArrayList<>(); + path.add(0l); + assertEquals("Deve retornar o caminho esperado.", path , nearestNeighbor.getPath()); + } + + @Test + public void taxiBetterWaytRandomGraphTwoByTwo() throws ParseException { + + setUpRandomGraph(2, 2, 1); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(0); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + Assert.assertNotNull(nearestNeighbor); + } + + @AfterClass + public static void tearDown() { + + FileUtils.deleteDir(Configuration.USER_HOME + "/graphhopper/test"); + FileUtils.deleteDir(Configuration.USER_HOME + "/graphast/test"); + } +} diff --git a/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java new file mode 100644 index 0000000..1bf9564 --- /dev/null +++ b/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java @@ -0,0 +1,260 @@ +package org.graphast.knn; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.graphast.config.Configuration; +import org.graphast.exception.PathNotFoundException; +import org.graphast.graphgenerator.GraphGenerator; +import org.graphast.importer.OSMDBImporter; +import org.graphast.model.Edge; +import org.graphast.model.GraphBounds; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.rnn.RNNDepthFirstSearch; +import org.graphast.query.route.shortestpath.dijkstra.Dijkstra; +import org.graphast.query.route.shortestpath.dijkstra.DijkstraLinearFunction; +import org.graphast.query.route.shortestpath.model.Path; +import org.graphast.util.DateUtils; +import org.graphast.util.FileUtils; +import org.graphast.util.NumberUtils; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class RNNDepthFirstSearchTest { + private static final String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + + private Integer idCustomer; + private Date maxTravelTime; + private Date hourServiceTime; + + @Before + public void setUp() throws ParseException, IOException { + + //Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(5); + + //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundo + hourServiceTime = DateUtils.parseDate(00, 00, 00); + } + + @Test + public void reverteGraphTest() throws IOException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTAXI(); + GraphBounds graphBoundsReverse = new GraphGenerator().generateExampleTAXI(); + graphBoundsReverse.reverseGraph(); + + //checar quantidade de vértices que foram invertidos + assertEquals("A quantidade de Vértices esperado não corresponde com a quantidade retornada.", + 10, graphBounds.getNumberOfNodes()); + + //checar quantidade de edge que foram invertidos + assertEquals("A quantidade de Edges esperados não corresponde com a quantidade retornada.", + 13, graphBounds.getNumberOfEdges()); + + List latitudesTo = new ArrayList(); + List longitudeTo = new ArrayList(); + List idTo = new ArrayList(); + + for (int i = 0; i < graphBounds.getNumberOfEdges(); i++) { + Edge edge = graphBounds.getEdge(i); + double latitude = graphBounds.getNode(edge.getToNode()).getLatitude(); + double longitude = graphBounds.getNode(edge.getToNode()).getLongitude(); + double id = graphBounds.getNode(edge.getToNode()).getId(); + + latitudesTo.add(latitude); + longitudeTo.add(longitude); + idTo.add(id); + } + + //Reverse Graph + graphBounds.reverseGraph(); + + List latitudesFromReverse = new ArrayList(); + List longitudeFromReverse = new ArrayList(); + List idFromReverse = new ArrayList(); + + for (int i = 0; i < graphBounds.getNumberOfEdges(); i++) { + Edge edge = graphBounds.getEdge(i); + double latitude = graphBounds.getNode(edge.getFromNode()).getLatitude(); + double longitude = graphBounds.getNode(edge.getFromNode()).getLongitude(); + double id = graphBounds.getNode(edge.getFromNode()).getId(); + + latitudesFromReverse.add(latitude); + longitudeFromReverse.add(longitude); + idFromReverse.add(id); + } + + //checar quantidade de vértices que foram invertidos + assertEquals("A quantidade de Vértices esperado não corresponde com a quantidade retornada após o reverso do grafo.", + 10, graphBounds.getNumberOfNodes()); + + //checar quantidade de edge que foram invertidos + assertEquals("A quantidade de Edges esperados não corresponde com a quantidade retornada após o reverso do grafo.", + 13, graphBounds.getNumberOfEdges()); + + assertEquals(latitudesTo, latitudesFromReverse); + assertEquals(longitudeTo, longitudeFromReverse); + assertEquals(idTo, idFromReverse); + + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolution() { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTAXI(); + + //Tempo para atendiemento - 39 minutos + maxTravelTime = DateUtils.parseDate(0, 39, 00); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 39, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, custumer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + public void returnNullSolution() throws PathNotFoundException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTAXI(); + + //Tempo para atendiemento - 38 minutos + maxTravelTime = DateUtils.parseDate(0, 38, 00); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + Assert.assertNull(nearestNeighbor); + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolutionTimeForTheCallMidnight() throws PathNotFoundException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTaxi15to15minutes(); + + //Tempo para atendiemento - 40 minutos + maxTravelTime = DateUtils.parseDate(0, 40, 00); + + //Hora que ele realiza a chamada do serviço - meia-noite + hourServiceTime = DateUtils.parseDate(00, 00, 00); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 40, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolutionTimeForTheCallHourServiceInit() throws PathNotFoundException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTaxi15to15minutes(); + + //Tempo para atendiemento - 39 minutos + maxTravelTime = DateUtils.parseDate(0, 40, 00); + + //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundo + hourServiceTime = DateUtils.parseDate(00, 15, 00); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 29, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + @Test + public void baseTest() throws IOException, ParseException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTAXI(); + + Dijkstra dijkstraShortestPathLinearFunction = new DijkstraLinearFunction(graphBounds); + Path shortestPath = dijkstraShortestPathLinearFunction.shortestPath(1l, 5l); + Assert.assertEquals(39, shortestPath.getTotalCost()/1000/60, 0); + + } + + @Test + public void dbTest() { + + OSMDBImporter importer = new OSMDBImporter("view_exp_1k", PATH_GRAPH); + GraphBounds graphBounds = importer.execute(); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); +// NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + //Tempo para atendiemento - 39 minutos + maxTravelTime = DateUtils.parseDate(00, 59, 00); + //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundo + hourServiceTime = DateUtils.parseDate(00, 00, 00); + + for (int i = 0; i < 100; i++) { + + long numberOfNodes = graphBounds.getNumberOfNodes(); + long customer = Double.valueOf(NumberUtils.generatePdseurandom(0, Long.valueOf(numberOfNodes).intValue())).longValue(); + NearestNeighbor solution2 = taxiSearch.search(graphBounds.getNode(customer), maxTravelTime, hourServiceTime); + + Long idSolution2 = null; + Integer distance2 = null; + Integer size2 = null; + ArrayList path2 = null; + Long externalId2 = null; + if(solution2 != null) { + idSolution2 = solution2.getId(); + distance2 = solution2.getDistance(); + size2 = solution2.getPath().size(); + path2 = solution2.getPath(); + externalId2 = graphBounds.getNode(solution2.getId()).getExternalId(); + + } + + String.format("%s;%s;%s;%s;%s", idSolution2, externalId2, distance2, size2, path2); + } + } + + @AfterClass + public static void tearDown() { + + FileUtils.deleteDir(Configuration.USER_HOME + "/graphhopper/test"); + FileUtils.deleteDir(Configuration.USER_HOME + "/graphast/test"); + } +} From 6f2840fdeb5dc41a442cf4a28adf508f6e7e2edf Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:20:32 -0200 Subject: [PATCH 015/118] chance member level of the method Long getNodeId(int latitude, int longitude). chance of the proteced to public. And add method public Set getPoiIds() with IDs Pois. --- .../main/java/org/graphast/model/Graph.java | 21 +++++++-- .../java/org/graphast/model/GraphImpl.java | 47 +++++++++++++------ 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/org/graphast/model/Graph.java b/core/src/main/java/org/graphast/model/Graph.java index 4f55d01..1345a4f 100644 --- a/core/src/main/java/org/graphast/model/Graph.java +++ b/core/src/main/java/org/graphast/model/Graph.java @@ -1,7 +1,13 @@ package org.graphast.model; +import it.unimi.dsi.fastutil.ints.IntBigArrayBigList; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.longs.Long2IntMap; +import it.unimi.dsi.fastutil.longs.LongList; + import java.util.HashMap; import java.util.List; +import java.util.Set; import org.graphast.enums.CompressionType; import org.graphast.enums.TimeType; @@ -10,11 +16,6 @@ import org.graphast.geometry.Point; import org.graphast.util.FileUtils; -import it.unimi.dsi.fastutil.ints.IntBigArrayBigList; -import it.unimi.dsi.fastutil.ints.IntSet; -import it.unimi.dsi.fastutil.longs.Long2IntMap; -import it.unimi.dsi.fastutil.longs.LongList; - public interface Graph { @@ -186,6 +187,14 @@ public interface Graph { * @return Id of a node */ public Long getNodeId(double latitude, double longitude); + + /** + * This method return a nodeId based on a given absolute latitude and longitude. + * @param latitude latitude that is given + * @param longitude longitude that is given + * @return Id of a node + */ + public Long getNodeId(int latitude, int longitude); /** * This method returns a label of a given node. @@ -321,4 +330,6 @@ public interface Graph { public String getAbsoluteDirectory(); public void setDirectory(String directory); + + public Set getPoiIds(); } \ No newline at end of file diff --git a/core/src/main/java/org/graphast/model/GraphImpl.java b/core/src/main/java/org/graphast/model/GraphImpl.java index 2076ddd..6bd8099 100644 --- a/core/src/main/java/org/graphast/model/GraphImpl.java +++ b/core/src/main/java/org/graphast/model/GraphImpl.java @@ -2,11 +2,25 @@ import static org.graphast.util.GeoUtils.latLongToDouble; import static org.graphast.util.GeoUtils.latLongToInt; +import it.unimi.dsi.fastutil.BigArrays; +import it.unimi.dsi.fastutil.ints.IntBigArrayBigList; +import it.unimi.dsi.fastutil.ints.IntOpenHashSet; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.longs.Long2IntMap; +import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; +import it.unimi.dsi.fastutil.longs.Long2LongMap; +import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; +import it.unimi.dsi.fastutil.longs.LongArrayList; +import it.unimi.dsi.fastutil.longs.LongList; +import it.unimi.dsi.fastutil.objects.ObjectBigArrayBigList; +import it.unimi.dsi.fastutil.objects.ObjectBigList; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.graphast.enums.CompressionType; import org.graphast.enums.TimeType; @@ -21,19 +35,6 @@ import com.graphhopper.util.StopWatch; -import it.unimi.dsi.fastutil.BigArrays; -import it.unimi.dsi.fastutil.ints.IntBigArrayBigList; -import it.unimi.dsi.fastutil.ints.IntOpenHashSet; -import it.unimi.dsi.fastutil.ints.IntSet; -import it.unimi.dsi.fastutil.longs.Long2IntMap; -import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; -import it.unimi.dsi.fastutil.longs.Long2LongMap; -import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArrayList; -import it.unimi.dsi.fastutil.longs.LongList; -import it.unimi.dsi.fastutil.objects.ObjectBigArrayBigList; -import it.unimi.dsi.fastutil.objects.ObjectBigList; - public class GraphImpl implements Graph { private Logger log = LoggerFactory.getLogger(this.getClass()); @@ -825,7 +826,8 @@ public List getGeometryByGeometryIndex(long geometryIndex) { return listPoints; } - Long getNodeId(int latitude, int longitude) { + @Override + public Long getNodeId(int latitude, int longitude) { Long result = nodeIndex.get(BigArrays.index(latitude, longitude)); @@ -846,7 +848,9 @@ Long getNodeId(int latitude, int longitude) { */ @Override public Long getNodeId(double latitude, double longitude) { - + if(this.getNumberOfNodes()==0) { + return null; + } int lat, lon; lat = latLongToInt(latitude); @@ -1467,4 +1471,17 @@ public void setDirectory(String directory) { this.directory = directory; } + @Override + public Set getPoiIds() { + Set ids = new HashSet<>(); + long max = nodes.size64()/Node.NODE_BLOCKSIZE; + for (long id = 0; id= 0) { + ids.add(id); + } + } + return ids; + } } From 468a3633e2d42e55993185c844fedeb0ecde282f Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:21:56 -0200 Subject: [PATCH 016/118] add util method double generatePdseurandom(int rangeMin, int rangeMax). Returns random values within a range. --- core/src/main/java/org/graphast/util/NumberUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/main/java/org/graphast/util/NumberUtils.java b/core/src/main/java/org/graphast/util/NumberUtils.java index f19eea8..2c1b426 100755 --- a/core/src/main/java/org/graphast/util/NumberUtils.java +++ b/core/src/main/java/org/graphast/util/NumberUtils.java @@ -2,6 +2,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.Random; import org.graphast.exception.GraphastException; @@ -113,4 +114,8 @@ public static int index( final short segment, final short displacement ) { return (short)segment << 16 | displacement & 0xFFFF; } + public static double generatePdseurandom(int rangeMin, int rangeMax) { + return rangeMin + (rangeMax - rangeMin) * new Random().nextDouble(); + } + } From 760435f6aed1f49518f3b7db5896d62416ef8c18 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:23:37 -0200 Subject: [PATCH 017/118] add util method boolean isPointInEdgeLine(GraphBounds graph, Node point, Edge edge). checks if a point is in a line. --- core/src/main/java/org/graphast/util/GeoUtils.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/util/GeoUtils.java b/core/src/main/java/org/graphast/util/GeoUtils.java index da91e13..58b36d7 100644 --- a/core/src/main/java/org/graphast/util/GeoUtils.java +++ b/core/src/main/java/org/graphast/util/GeoUtils.java @@ -1,5 +1,9 @@ package org.graphast.util; +import org.graphast.model.Edge; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; + public class GeoUtils { final private static double R_MAJOR = 6378137.0; @@ -88,6 +92,12 @@ public static double lat2YElliptical(double lat) { double ts = Math.tan(0.5 * ((Math.PI*0.5) - phi))/con; double y = 0 - R_MAJOR * Math.log(ts); return y; - } + } + + public static boolean isPointInEdgeLine(GraphBounds graph, Node point, Edge edge) { + Node start = graph.getNode(edge.getFromNode()); + Node end = graph.getNode(edge.getToNode()); + return DistanceUtils.distanceLatLong(start, point)+DistanceUtils.distanceLatLong(point, end)-DistanceUtils.distanceLatLong(start, end)<=0.1; + } } From 4cf936a61e64607cce00f9d563ee382bc41b26c6 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:24:11 -0200 Subject: [PATCH 018/118] set default value of category. --- core/src/main/java/org/graphast/model/NodeImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/model/NodeImpl.java b/core/src/main/java/org/graphast/model/NodeImpl.java index dd4df9f..f13104d 100644 --- a/core/src/main/java/org/graphast/model/NodeImpl.java +++ b/core/src/main/java/org/graphast/model/NodeImpl.java @@ -12,7 +12,7 @@ public class NodeImpl implements Node { private long externalId; - private int category; + private int category = -1; private int latitude; From 02d7d3693fe78d96d30183d0e2618613ecf277fa Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 4 Dec 2015 17:40:07 -0300 Subject: [PATCH 019/118] Added data collection in nanoseconds --- .../query/rnn/CompareRNNSearchsMethodsAnalysis.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 32d9393..601750b 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -39,7 +39,7 @@ public static void runAnalysis(String tableName, int testTimes) throws IOExcepti RNNDepthFirstSearch rnnDFS = new RNNDepthFirstSearch(graph); RNNBreadthFirstSearch rnnBFS = new RNNBreadthFirstSearch(graphReverse); - Date timeout = DateUtils.parseDate(00, 20, 00); + Date timeout = DateUtils.parseDate(00, 50, 00); Date timestamp = DateUtils.parseDate(00, 00, 00); FileWriter rnnDFSFileCsv = new FileWriter(tableName+"_rnn_dfs.csv"); @@ -58,9 +58,9 @@ public static void runAnalysis(String tableName, int testTimes) throws IOExcepti private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { try { - long startTime = System.currentTimeMillis(); + long startTime = System.nanoTime(); NearestNeighbor solution = rnn.search(customer, timeout, timestamp); - long endTime = System.currentTimeMillis(); + long endTime = System.nanoTime(); long time = endTime - startTime; @@ -74,7 +74,8 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, distance = solution.getDistance(); nodesSize = solution.getPath().size(); path = solution.getPath(); - externalId = graph.getNode(solution.getId()).getExternalId(); + //externalId = graph.getNode(solution.getId()).getExternalId(); + externalId = Integer.valueOf(graph.getNode(solution.getId()).getCategory()).longValue(); String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); String gidCustomer = customer.getLabel(); @@ -92,12 +93,11 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, gidVisited = gidVisited + "-" + nodeVisited.getLabel(); } - - String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, poiCoordinate, time, solutionId, externalId, distance, nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited) + "\n"; + System.out.println(currentLine); fileCsv.write(currentLine); From 6b464e8193915f2273dd64d37be165661a045b0f Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 4 Dec 2015 17:40:50 -0300 Subject: [PATCH 020/118] Add test for verify cost edge --- .../graphast/importer/OSMDBImporterTest.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java b/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java index 1199cf0..413a53c 100644 --- a/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java +++ b/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java @@ -1,6 +1,7 @@ package org.graphast.importer; import org.graphast.config.Configuration; +import org.graphast.model.Edge; import org.graphast.model.GraphBounds; import org.junit.Assert; import org.junit.Test; @@ -14,9 +15,24 @@ public void createGraphTest1k() { OSMDBImporter importer = new OSMDBImporter("view_exp_1k", PATH_GRAPH); GraphBounds graph = importer.execute(); Assert.assertNotNull(graph); - Assert.assertEquals(15, graph.getCategories().size()); - Assert.assertEquals(809+15, graph.getNumberOfNodes()); - Assert.assertEquals(844+15, graph.getNumberOfEdges()); + //Assert.assertEquals(15, graph.getCategories().size()); + //Assert.assertEquals(809+15, graph.getNumberOfNodes()); + //Assert.assertEquals(844+15, graph.getNumberOfEdges()); + + Assert.assertEquals(14, graph.getCategories().size()); + Assert.assertEquals(809, graph.getNumberOfNodes()); + Assert.assertEquals(844, graph.getNumberOfEdges()); + } + + @Test + public void costEdgeTest() { + OSMDBImporter importer = new OSMDBImporter("view_exp_1k", PATH_GRAPH); + GraphBounds graphBounds = importer.execute(); + + for (int i = 0; i < graphBounds.getNumberOfEdges() - 1; i++) { + Edge edge = graphBounds.getEdge(i); + Assert.assertEquals(96, edge.getCosts().length); + } } @Test From 8f54628c03e81deb46d39dbd765b6a5710ee08c9 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 4 Dec 2015 17:41:14 -0300 Subject: [PATCH 021/118] NearestNeighbor nearestNeighbor = null; --- .../main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java index be0106a..53bf8b8 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java @@ -59,9 +59,10 @@ public NearestNeighbor search(Node root, Date timeout, Date timestamp) throws Pa private NearestNeighbor createNN(Node root, long currentPoi, Path path) { - NearestNeighbor nearestNeighbor = new NearestNeighbor(); + NearestNeighbor nearestNeighbor = null; if (currentPoi > -1) { + nearestNeighbor = new NearestNeighbor(); nearestNeighbor.setDistance((int) path.getTotalCost()); nearestNeighbor.setId(currentPoi); From d701d351d8226a5dd400cff2ed81003802b1c550 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 4 Dec 2015 17:42:12 -0300 Subject: [PATCH 022/118] define node with taxi and fixed erro cost edge --- .../java/org/graphast/importer/OSMDBImporter.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java index 00f271c..7fdb261 100644 --- a/core/src/main/java/org/graphast/importer/OSMDBImporter.java +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -84,14 +84,14 @@ private void plotNodes() throws SQLException, ClassNotFoundException, IOExceptio if (previousNode != null && !previousNode.getId().equals(node.getId())) { LOGGER.log(Level.INFO, String.format("Add edge from previous: %s to current: %s node", previousNode.getId(), node.getId())); - Edge edge = new EdgeImpl(idRoad, previousNode.getId() .longValue(), node.getId().longValue(), 0); + Edge edge = new EdgeImpl(idRoad, previousNode.getId().longValue(), node.getId().longValue(), 0, String.valueOf(idRoad)); addCost(edge); graph.addEdge(edge); if(mapTaxi.containsKey(idRoad)) { for(int i=0; i Date: Tue, 8 Dec 2015 13:30:37 -0300 Subject: [PATCH 023/118] alterado biblioteca que coloca log. --- .../org/graphast/importer/OSMDBImporter.java | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java index 7fdb261..b986bf9 100644 --- a/core/src/main/java/org/graphast/importer/OSMDBImporter.java +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -5,8 +5,6 @@ import java.sql.SQLException; import java.util.List; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import org.graphast.model.Edge; import org.graphast.model.EdgeImpl; @@ -20,6 +18,9 @@ import org.graphast.util.GeoUtils; import org.postgis.LineString; import org.postgis.Point; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class OSMDBImporter implements Importer { @@ -29,8 +30,7 @@ public class OSMDBImporter implements Importer { private final int FIELD_ID_LINESTRING = 1; private final int FIELD_LINESTRING = 2; private final int SIZE_INTERVAL = 96; - - protected static final Logger LOGGER = Logger.getGlobal(); + protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); public OSMDBImporter(String table, String directory) { this.table = table; @@ -59,7 +59,7 @@ private void plotNodes() throws SQLException, ClassNotFoundException, IOExceptio while (result.next()) { LineString lineString = new LineString(result.getString(FIELD_LINESTRING)); Point[] arrayPoints = lineString.getPoints(); - LOGGER.log(Level.INFO, String.format("registro: %s", result.getString(FIELD_LINESTRING))); + LOGGER.info(String.format("registro: %s", result.getString(FIELD_LINESTRING))); int idRoad = result.getInt(FIELD_ID_LINESTRING); @@ -67,23 +67,23 @@ private void plotNodes() throws SQLException, ClassNotFoundException, IOExceptio for (Point point : arrayPoints) { pointCount++; - LOGGER.log(Level.INFO, String.format("Point [x,y]: %s,%s", point.getX(), point.getY())); + LOGGER.info( String.format("Point [x,y]: %s,%s", point.getX(), point.getY())); Node node = new NodeImpl(point.getY(), point.getX()); node.setLabel(Long.valueOf(idRoad).toString()); Long nodeId = graph.getNodeId(GeoUtils.latLongToInt(node.getLatitude()), GeoUtils.latLongToInt(node.getLongitude())); if (nodeId != null) { - LOGGER.log(Level.INFO, String.format("point already exist in graph")); + LOGGER.info(String.format("point already exist in graph")); node = graph.getNode(nodeId); } else { graph.addNode(node); - LOGGER.log(Level.INFO, String.format("point inserted in graph with ID: %s", node.getId())); + LOGGER.info(String.format("point inserted in graph with ID: %s", node.getId())); } if (previousNode != null && !previousNode.getId().equals(node.getId())) { - LOGGER.log(Level.INFO, String.format("Add edge from previous: %s to current: %s node", previousNode.getId(), node.getId())); + LOGGER.info( String.format("Add edge from previous: %s to current: %s node", previousNode.getId(), node.getId())); Edge edge = new EdgeImpl(idRoad, previousNode.getId().longValue(), node.getId().longValue(), 0, String.valueOf(idRoad)); addCost(edge); graph.addEdge(edge); @@ -98,14 +98,14 @@ private void plotNodes() throws SQLException, ClassNotFoundException, IOExceptio } } } - LOGGER.log(Level.INFO, String.format("Graph now has %s nodes", graph.getNumberOfNodes())); - LOGGER.log(Level.INFO, String.format("Graph now has %s edges",graph.getNumberOfEdges())); + LOGGER.info(String.format("Graph now has %s nodes", graph.getNumberOfNodes())); + LOGGER.info(String.format("Graph now has %s edges",graph.getNumberOfEdges())); previousNode = node; } } - LOGGER.log(Level.INFO, String.format("Total points parsed are: %s", pointCount)); + LOGGER.info( String.format("Total points parsed are: %s", pointCount)); ConnectionJDBC.getConnection().close(); } @@ -126,8 +126,8 @@ private void defineNodeWithTaxi(Node taxi, Node node) { @Deprecated private void connectTaxiToEdge(Node taxi, Node end) { graph.addNode(taxi); - LOGGER.log(Level.INFO, String.format("Taxi %s lies in edge and now taxi is node: %s", taxi.getCategory(), taxi.getId())); - LOGGER.log(Level.INFO, String.format("Connect Taxi %s with node: %s", taxi.getCategory(), end.getId())); + LOGGER.info(String.format("Taxi %s lies in edge and now taxi is node: %s", taxi.getCategory(), taxi.getId())); + LOGGER.info(String.format("Connect Taxi %s with node: %s", taxi.getCategory(), end.getId())); Edge edge = new EdgeImpl(taxi.getId().longValue(), end.getId().longValue(), 0); addCostZero(edge); graph.addEdge(edge); @@ -135,19 +135,14 @@ private void connectTaxiToEdge(Node taxi, Node end) { private void addCost(Edge edge) { - int[] costs = new int[SIZE_INTERVAL]; - Node nodeFrom = graph.getNode(edge.getFromNode()); Node nodeTo = graph.getNode(edge.getToNode()); - double distanceBetweenLatLongHaversine = DistanceUtils.distanceLatLong(nodeFrom.getLatitude(), nodeFrom.getLongitude(), + double distanceBetweenLatLongHaversine = + DistanceUtils.distanceLatLong(nodeFrom.getLatitude(), nodeFrom.getLongitude(), nodeTo.getLatitude(), nodeTo.getLongitude()); - - for (int i = 0; i < costs.length; i++) { - costs[i] = Double.valueOf(distanceBetweenLatLongHaversine) - .intValue(); - } - edge.setCosts(costs); + int[] edgesCosts = CostGenerator.generateSyntheticEdgesCosts(Double.valueOf(distanceBetweenLatLongHaversine).intValue()); + edge.setCosts(edgesCosts); } } From 24ec194b6037ae3eca179a9da7b98f20852abdcb Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:31:12 -0300 Subject: [PATCH 024/118] add NumberVisitedNodes --- .../GraphGeneratorGridTest.java | 49 ++++++++++--------- .../knn/RNNBreadthFirstSearchTest.java | 5 +- .../graphast/knn/RNNDepthFirstSearchTest.java | 17 ++++--- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java index 7dbe5a3..43a2057 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java @@ -1,38 +1,43 @@ package org.graphast.graphgenerator; +import org.graphast.config.Configuration; import org.graphast.model.GraphBounds; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; public class GraphGeneratorGridTest { - + + private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + @Test public void gererateGraphSynthetic2x2() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(2,2,0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 2,2,0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); - Assert.assertEquals(graph.getNumberOfNodes(), 4); - Assert.assertEquals(graph.getNumberOfEdges(), 8); + Assert.assertEquals(4, graph.getNumberOfNodes()); + Assert.assertEquals(8, graph.getNumberOfEdges()); + Assert.assertNotNull(graph.getEdgeCost(graph.getEdge(0), 0)); } @Test public void gererateGraphSynthetic4x4() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(4,4, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 4,4, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); - Assert.assertEquals(graph.getNumberOfNodes(), 16); - Assert.assertEquals(graph.getNumberOfEdges(), 48); + Assert.assertEquals(16, graph.getNumberOfNodes()); + Assert.assertEquals(48, graph.getNumberOfEdges()); + Assert.assertNotNull(graph.getEdgeCost(graph.getEdge(0), 0)); } @Test public void gererateGraphSynthetic5x5() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(5,5, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 5,5, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -43,7 +48,7 @@ public void gererateGraphSynthetic5x5() { @Test public void gererateGraphSynthetic100x100() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(100,100, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 100,100, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -51,13 +56,14 @@ public void gererateGraphSynthetic100x100() { Assert.assertEquals(graph.getNumberOfEdges(), 39600); } + @Ignore @Test public void gererateGraphSyntheticLimit() { int comprimento = 992; int altura = 992; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -65,13 +71,14 @@ public void gererateGraphSyntheticLimit() { Assert.assertEquals(graph.getNumberOfEdges(), 2*altura*(2*(comprimento-1))); } + @Ignore @Test public void gererateGraphSyntheticLimitWithPoi() { int comprimento = 992; int altura = 992; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -87,7 +94,7 @@ public void gererateGraphSyntheticDifferentSize() { int comprimento = 3; int altura = 2; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -101,7 +108,7 @@ public void gererateGraphSyntheticWithPoiShort() { int comprimento = 4; int altura = 4; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento, altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -114,7 +121,7 @@ public void gererateGraphSyntheticWithPoi() { int comprimento = 10; int altura = 10; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento, altura, 2); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, 2); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -128,7 +135,7 @@ public void gererateGraphSyntheticMin() { int altura = 1; int percentagemPoi = 1; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento, altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -139,14 +146,13 @@ public void gererateGraphSyntheticMin() { // 1k (1024 pontos) @Test - @Ignore public void gererateGraphSyntheticLimitWithPoi1k() { int comprimento = 32; int altura = 32; int qtdPoi = 10; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -157,7 +163,6 @@ public void gererateGraphSyntheticLimitWithPoi1k() { // 10k (10000 pontos) @Test - @Ignore public void gererateGraphSyntheticLimitWithPoi10k() { int comprimento = 100; @@ -165,7 +170,7 @@ public void gererateGraphSyntheticLimitWithPoi10k() { int qtdPoi = 100; int percentualPoi = 1; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentualPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentualPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -176,14 +181,13 @@ public void gererateGraphSyntheticLimitWithPoi10k() { // 100k (99856 pontos) @Test - @Ignore public void gererateGraphSyntheticLimitWithPoi100k() { int comprimento = 316; int altura = 316; int qtdPoi = 998; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -194,14 +198,13 @@ public void gererateGraphSyntheticLimitWithPoi100k() { // 1G (1000000 pontos) @Test - @Ignore public void gererateGraphSyntheticLimitWithPoi1000k() { int comprimento = 1000; int altura = 1000; int qtdPoi = 10000; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); diff --git a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java index a938b86..cb69f67 100644 --- a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java +++ b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java @@ -28,7 +28,8 @@ public class RNNBreadthFirstSearchTest { private Integer idCustomer; private Date maxTravelTime; private Date hourServiceTime; - + private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + public void setUpNoRandomGraph() throws ParseException, IOException { // Tempo para atendiemento @@ -50,7 +51,7 @@ public void setUpRandomGraph(int qtdX, int qtdY, int percentPois) throws ParseEx // Hora que ele realiza a chamada do serviço meia-noite e vinte minutos hourServiceTime = DateUtils.parseDate(00, 00, 00); - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(qtdX, qtdY, percentPois); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, qtdX, qtdY, percentPois); graphSynthetic.generateGraph(); graphBoundsReverse = graphSynthetic.getGraph(); diff --git a/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java index 1bf9564..93c999f 100644 --- a/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java +++ b/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java @@ -15,7 +15,7 @@ import org.graphast.model.Edge; import org.graphast.model.GraphBounds; import org.graphast.query.knn.NearestNeighbor; -import org.graphast.query.rnn.RNNDepthFirstSearch; +import org.graphast.query.rnn.RNNBacktrackingSearch; import org.graphast.query.route.shortestpath.dijkstra.Dijkstra; import org.graphast.query.route.shortestpath.dijkstra.DijkstraLinearFunction; import org.graphast.query.route.shortestpath.model.Path; @@ -25,6 +25,7 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; public class RNNDepthFirstSearchTest { @@ -115,7 +116,7 @@ public void returnBetterSolution() { //Tempo para atendiemento - 39 minutos maxTravelTime = DateUtils.parseDate(0, 39, 00); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); System.out.println(nearestNeighbor); @@ -130,6 +131,7 @@ public void returnBetterSolution() { assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); assertEquals("Deve retornar o custo esperado.", 39, nearestNeighbor.getDistance()/1000/60); assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + Assert.assertNotNull(nearestNeighbor.getNumberVisitedNodes()); } // Tem três taxista na malha, custumer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 @@ -140,7 +142,7 @@ public void returnNullSolution() throws PathNotFoundException { //Tempo para atendiemento - 38 minutos maxTravelTime = DateUtils.parseDate(0, 38, 00); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); Assert.assertNull(nearestNeighbor); } @@ -157,7 +159,7 @@ public void returnBetterSolutionTimeForTheCallMidnight() throws PathNotFoundExce //Hora que ele realiza a chamada do serviço - meia-noite hourServiceTime = DateUtils.parseDate(00, 00, 00); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); System.out.println(nearestNeighbor); @@ -172,6 +174,7 @@ public void returnBetterSolutionTimeForTheCallMidnight() throws PathNotFoundExce assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); assertEquals("Deve retornar o custo esperado.", 40, nearestNeighbor.getDistance()/1000/60); assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + Assert.assertNotNull(nearestNeighbor.getNumberVisitedNodes()); } // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 @@ -186,7 +189,7 @@ public void returnBetterSolutionTimeForTheCallHourServiceInit() throws PathNotFo //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundo hourServiceTime = DateUtils.parseDate(00, 15, 00); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); System.out.println(nearestNeighbor); @@ -201,6 +204,7 @@ public void returnBetterSolutionTimeForTheCallHourServiceInit() throws PathNotFo assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); assertEquals("Deve retornar o custo esperado.", 29, nearestNeighbor.getDistance()/1000/60); assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + Assert.assertNotNull(nearestNeighbor.getNumberVisitedNodes()); } @Test @@ -214,13 +218,14 @@ public void baseTest() throws IOException, ParseException { } + @Ignore @Test public void dbTest() { OSMDBImporter importer = new OSMDBImporter("view_exp_1k", PATH_GRAPH); GraphBounds graphBounds = importer.execute(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); // NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); //Tempo para atendiemento - 39 minutos maxTravelTime = DateUtils.parseDate(00, 59, 00); From 9d44e9284430d6390c52087a9648484df815ebf8 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:31:56 -0300 Subject: [PATCH 025/118] add parameter of the number visited nodes --- .../route/shortestpath/dijkstra/Dijkstra.java | 18 +++++++---- .../dijkstra/DijkstraConstantWeight.java | 3 +- .../dijkstra/DijkstraLinearFunction.java | 31 ++++++++++++------- .../query/route/shortestpath/model/Path.java | 10 ++++++ 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java index 29558f0..5c9e76c 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java @@ -54,25 +54,31 @@ protected List reconstructPath(long id, HashMap pa public Path shortestPath(Node source, Node target, Date time) { PriorityQueue queue = new PriorityQueue(); HashMap wasTraversed = new HashMap(); + List allWasViseted = new ArrayList(); HashMap parents = new HashMap(); TimeEntry removed = null; int targetId = convertToInt(target.getId()); - int t = DateUtils.dateToMilli(time); + int timeInMilli = DateUtils.dateToMilli(time); - init(source, target, queue, parents, t); + init(source, target, queue, parents, timeInMilli); - while(!queue.isEmpty()){ + while(!queue.isEmpty()) { removed = queue.poll(); - wasTraversed.put(removed.getId(), wasRemoved); + long idRemoved = removed.getId(); + wasTraversed.put(idRemoved, wasRemoved); + allWasViseted.add(idRemoved); + //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); if(removed.getId() == targetId) { Path path = new Path(); path.constructPath(removed.getId(), parents, graph); + path.setNumberVisitedNodes(allWasViseted.size()); return path; } - expandVertex(target, removed, wasTraversed, queue, parents); + expandVertex(target, removed, wasTraversed, allWasViseted, queue, parents); } + throw new PathNotFoundException("Path not found between (" + source.getLatitude() + "," + source.getLongitude() + ") and (" + target.getLatitude() + "," + target.getLongitude() + ")"); } @@ -85,7 +91,7 @@ public void init(Node source, Node target, PriorityQueue queue, } public abstract void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, - PriorityQueue queue, HashMap parents); + List wasVisited, PriorityQueue queue, HashMap parents); @Override public Path shortestPath(Node source, Node target) { diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java index c33fc6d..9efbd83 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java @@ -3,6 +3,7 @@ import it.unimi.dsi.fastutil.longs.Long2IntMap; import java.util.HashMap; +import java.util.List; import java.util.PriorityQueue; import org.graphast.model.Edge; @@ -22,7 +23,7 @@ public DijkstraConstantWeight(GraphBounds graphBounds) { super(graphBounds); } - public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, + public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, List wasVisited, PriorityQueue queue, HashMap parents){ Long2IntMap neig = graph.accessNeighborhood(graph.getNode(removed.getId())); diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java index f419a06..8bf5eb7 100755 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java @@ -23,7 +23,7 @@ import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import it.unimi.dsi.fastutil.longs.LongSet; -public class DijkstraLinearFunction extends Dijkstra{ +public class DijkstraLinearFunction extends Dijkstra { public DijkstraLinearFunction(Graph graph) { super(graph); @@ -33,15 +33,15 @@ public DijkstraLinearFunction(GraphBounds graphBounds) { super(graphBounds); } - public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, + public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, List allWasViseted, PriorityQueue queue, HashMap parents){ - HashMap neig = graph.accessNeighborhood(graph.getNode(removed.getId()), removed.getArrivalTime()); + HashMap neighbors = graph.accessNeighborhood(graph.getNode(removed.getId()), removed.getArrivalTime()); - for (Node v : neig.keySet()) { - long vid = v.getId(); - int at = graph.getArrival(removed.getArrivalTime(), neig.get(v)); - int tt = removed.getTravelTime() + neig.get(v); + for (Node node : neighbors.keySet()) { + long vid = node.getId(); + int at = graph.getArrival(removed.getArrivalTime(), neighbors.get(node)); + int tt = removed.getTravelTime() + neighbors.get(node); TimeEntry newEntry = new TimeEntry( vid, tt, at, removed.getId()); Edge edge = null; @@ -49,9 +49,12 @@ public void expandVertex(Node target, TimeEntry removed, HashMap if(!wasTraversed.containsKey(vid)){ queue.offer(newEntry); + long idNewEntry = newEntry.getId(); wasTraversed.put(newEntry.getId(), newEntry.getTravelTime()); + allWasViseted.add(idNewEntry); + //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); - distance = neig.get(v); + distance = neighbors.get(node); edge = getEdge(removed.getId(), vid, distance); parents.put(vid, new RouteEntry(removed.getId(), distance, edge.getId(), edge.getLabel())); }else{ @@ -60,11 +63,15 @@ public void expandVertex(Node target, TimeEntry removed, HashMap if(cost>newEntry.getTravelTime()){ queue.remove(newEntry); queue.offer(newEntry); - wasTraversed.remove(newEntry.getId()); - wasTraversed.put(newEntry.getId(), newEntry.getTravelTime()); + long idNewEntry = newEntry.getId(); + wasTraversed.remove(idNewEntry); + wasTraversed.put(idNewEntry, newEntry.getTravelTime()); + allWasViseted.add(idNewEntry); + //System.out.println("vertex already visited"); + //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); - parents.remove(v); - distance = neig.get(v); + parents.remove(node); + distance = neighbors.get(node); edge = getEdge(removed.getId(), vid, distance); parents.put(vid, new RouteEntry(removed.getId(), distance, edge.getId(), edge.getLabel())); } diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/model/Path.java b/core/src/main/java/org/graphast/query/route/shortestpath/model/Path.java index 396a765..2388705 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/model/Path.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/model/Path.java @@ -24,6 +24,7 @@ public class Path { private List listOfPoIs; private long totalDistance; private double totalCost; + private int numberVisitedNodes; public Path() { @@ -328,4 +329,13 @@ public double getTotalCost() { return totalCost; } + + public int getNumberVisitedNodes() { + return numberVisitedNodes; + } + + + public void setNumberVisitedNodes(int numberVisitedNodes) { + this.numberVisitedNodes = numberVisitedNodes; + } } \ No newline at end of file From a134c348eed01d637d8e1968f5d9459c3227d1eb Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:32:09 -0300 Subject: [PATCH 026/118] add cost in the graph --- .../graphgenerator/GraphGeneratorGrid.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java index a38b9b4..5beb2a3 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java @@ -6,7 +6,7 @@ import java.util.Random; import java.util.Set; -import org.graphast.config.Configuration; +import org.graphast.importer.CostGenerator; import org.graphast.model.Edge; import org.graphast.model.EdgeImpl; import org.graphast.model.GraphBounds; @@ -20,13 +20,18 @@ public class GraphGeneratorGrid { private int length; private GraphBounds graph; private double percentagemPoi; - private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + private BigDecimal distance_largura; + private BigDecimal distance_altura; - public GraphGeneratorGrid(int width, int length, double percentualPoi) { + public GraphGeneratorGrid(String pathGraph, int width, int length, double percentualPoi) { this.width = width; this.length = length; this.percentagemPoi = percentualPoi; - this.graph = new GraphBoundsImpl(PATH_GRAPH); + this.graph = new GraphBoundsImpl(pathGraph); + } + + public GraphGeneratorGrid(String pathGraph, int size, double percentualPoi) { + this(pathGraph, size, size, percentualPoi); } public void generateGraph() { @@ -39,6 +44,8 @@ private void plotNodes() { BigDecimal interador_largura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(width), 8, RoundingMode.HALF_UP); BigDecimal interador_altura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(length), 8, RoundingMode.HALF_UP); + this.distance_largura = interador_largura; + this.distance_altura = interador_altura; Set listaIdsPoi = getListIdsPois(); @@ -79,24 +86,26 @@ private Set getListIdsPois() { private void plotEdges() { for (int i = 0; i < width*length - 1; i++) { + //altura if(i < (width * length - width)) { - Edge edgeOutTopDown = new EdgeImpl(Long.valueOf(i).longValue(), Long.valueOf(i+length).longValue(), 0); + Edge edgeOutTopDown = new EdgeImpl(Long.valueOf(i).longValue(), Long.valueOf(i+length).longValue(), distance_altura.intValue()); addCost(edgeOutTopDown); graph.addEdge(edgeOutTopDown); - Edge edgeInTopDown = new EdgeImpl(Long.valueOf(i+length).longValue(), Long.valueOf(i).longValue(), 0); + Edge edgeInTopDown = new EdgeImpl(Long.valueOf(i+length).longValue(), Long.valueOf(i).longValue(), distance_altura.intValue()); addCost(edgeInTopDown); graph.addEdge(edgeInTopDown); } - + + //largura if(i%width != width - 1) { - Edge edgeOutLeftRight = new EdgeImpl(Long.valueOf(i).longValue(), Long.valueOf(i+1).longValue(), 0); + Edge edgeOutLeftRight = new EdgeImpl(Long.valueOf(i).longValue(), Long.valueOf(i+1).longValue(), distance_largura.intValue()); addCost(edgeOutLeftRight); graph.addEdge(edgeOutLeftRight); - Edge edgeInLeftRight = new EdgeImpl(Long.valueOf(i+1).longValue(), Long.valueOf(i).longValue(), 0); + Edge edgeInLeftRight = new EdgeImpl(Long.valueOf(i+1).longValue(), Long.valueOf(i).longValue(), distance_largura.intValue()); addCost(edgeInLeftRight); graph.addEdge(edgeInLeftRight); @@ -111,12 +120,8 @@ private double generatePdseurandom(int rangeMin, int rangeMax) { private void addCost(Edge edge) { - int[] costs = new int[96]; - - for (int i : costs) { - costs[i] = Double.valueOf(generatePdseurandom(1, 1200)).intValue(); - } - edge.setCosts(costs); + int[] edgesCosts = CostGenerator.generateSyntheticEdgesCosts(edge.getDistance()); + edge.setCosts(edgesCosts); } public boolean isConnected() { From 4ac9faf906bc7d5c84daab0dbce6239121e3702c Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:32:27 -0300 Subject: [PATCH 027/118] add parameter of the number visited nodes --- .../org/graphast/query/knn/NearestNeighbor.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java b/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java index 642bc9a..3b1ab63 100755 --- a/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java +++ b/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java @@ -6,6 +6,7 @@ public class NearestNeighbor implements Comparable { private long id; private int distance; private ArrayList path; + private int numberVisitedNodes; public NearestNeighbor() {} @@ -20,6 +21,11 @@ public NearestNeighbor(long id, int distance, ArrayList path){ this.distance = distance; this.path = path; } + + public NearestNeighbor(long id, int distance, ArrayList path, int numberVisitedNodes){ + this(id, distance, path); + this.numberVisitedNodes = numberVisitedNodes; + } public long getId() { return id; @@ -46,7 +52,8 @@ public void setPath(ArrayList path) { } public String toString(){ - return "(NN:"+id+" TT: "+distance+ " Path: " + path + ")"; + return "(NN:"+id+" TT: "+distance+ " Path: " + path + + " Number Visited Nodes: " + numberVisitedNodes + ")"; } public int compareTo(NearestNeighbor o) { @@ -60,4 +67,12 @@ public int compareTo(NearestNeighbor o) { public boolean equals(NearestNeighbor o){ return((id == o.id) && (distance == o.distance)); } + + public int getNumberVisitedNodes() { + return numberVisitedNodes; + } + + public void setNumberVisitedNodes(int numberVisitedNodes) { + this.numberVisitedNodes = numberVisitedNodes; + } } From d881d651f088eca0c660d10049d34b4bd7888c61 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:32:41 -0300 Subject: [PATCH 028/118] add parameter of the number visited nodes --- .../graphast/importer/GraphGeneratorGrid.java | 136 ++++++++++++++++++ ...Search.java => RNNBacktrackingSearch.java} | 37 ++--- .../query/rnn/RNNBreadthFirstSearch.java | 16 ++- .../graphast/query/rnn/RNNComparatorTest.java | 17 +-- 4 files changed, 178 insertions(+), 28 deletions(-) create mode 100644 core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java rename core/src/main/java/org/graphast/query/rnn/{RNNDepthFirstSearch.java => RNNBacktrackingSearch.java} (62%) diff --git a/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java b/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java new file mode 100644 index 0000000..fb79957 --- /dev/null +++ b/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java @@ -0,0 +1,136 @@ +package org.graphast.importer; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +import org.graphast.importer.CostGenerator; +import org.graphast.model.Edge; +import org.graphast.model.EdgeImpl; +import org.graphast.model.GraphBounds; +import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.Node; +import org.graphast.model.NodeImpl; + +public class GraphGeneratorGrid { + + private int width; + private int length; + private GraphBounds graph; + private double percentagemPoi; + private BigDecimal distance_largura; + private BigDecimal distance_altura; + + public GraphGeneratorGrid(String pathGraph, int width, int length, double percentualPoi) { + this.width = width; + this.length = length; + this.percentagemPoi = percentualPoi; + this.graph = new GraphBoundsImpl(pathGraph); + } + + public GraphGeneratorGrid(String pathGraph, int size, double percentualPoi) { + this(pathGraph, size, size, percentualPoi); + } + + public void generateGraph() { + plotNodes(); + plotEdges(); + graph.createBounds(); + } + + private void plotNodes() { + + BigDecimal interador_largura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(width), 8, RoundingMode.HALF_UP); + BigDecimal interador_altura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(length), 8, RoundingMode.HALF_UP); + this.distance_largura = interador_largura; + this.distance_altura = interador_altura; + + Set listaIdsPoi = getListIdsPois(); + + Integer category = 0; + for (int i = 0; i < width; i++) { + BigDecimal latitude = interador_altura.multiply(BigDecimal.valueOf(i)).add(BigDecimal.valueOf(-90)); + for (int j = 0; j < length; j++) { + BigDecimal longitude = interador_largura.multiply(BigDecimal.valueOf(j)).add(BigDecimal.valueOf(-90));; + Node node = new NodeImpl(Long.valueOf(category), latitude.doubleValue(), longitude.doubleValue()); + if(listaIdsPoi.contains(category)) { + int[] costs = new int[]{0}; + node.setCategory(category); + node.setExternalId(category); + node.setLabel("CATEGORY "+category); + node.setCosts(costs); + listaIdsPoi.remove(category); + } + graph.addNode(node); + category++; + } + } + } + + private Set getListIdsPois() { + + int quantidadeVerticesPoi = BigDecimal.valueOf(width).multiply(BigDecimal.valueOf(length)).multiply(BigDecimal.valueOf(percentagemPoi)).divide(BigDecimal.valueOf(100.0f), 8, RoundingMode.UP).intValue(); + + Set listIdPoi = new HashSet<>(); + do { + int rangeMax = width*length - 1; + Double idRandom = generatePdseurandom(0, rangeMax); + listIdPoi.add(idRandom.intValue()); + } while(listIdPoi.size() -1) { + NearestNeighbor nearestNeighbor = createNN(root, currentPoi, numberVisitedNodes, pathResult); + return nearestNeighbor; } - return nearestNeighbor; + throw new PathNotFoundException( + "target not found for root and set timestamp"); } - private NearestNeighbor createNN(Node root, long currentPoi, Path path) { + private NearestNeighbor createNN(Node root, long currentPoi, int numberVisitedNodes, Path path) { - NearestNeighbor nearestNeighbor = null; - if (currentPoi > -1) { + NearestNeighbor nearestNeighbor = new NearestNeighbor(); - nearestNeighbor = new NearestNeighbor(); - nearestNeighbor.setDistance((int) path.getTotalCost()); + double totalCostInMilissegundo = path.getTotalCost(); + double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); + + nearestNeighbor.setDistance(Double.valueOf(totalCostInNanosegundos).intValue()); nearestNeighbor.setId(currentPoi); + nearestNeighbor.setNumberVisitedNodes(numberVisitedNodes); ArrayList arrayPath = new ArrayList(); List edges = path.getEdges(); @@ -77,7 +81,6 @@ private NearestNeighbor createNN(Node root, long currentPoi, Path path) { arrayPath.add(root.getId()); nearestNeighbor.setPath(arrayPath); - } return nearestNeighbor; } diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java index 1765b99..88bd941 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -27,10 +27,16 @@ public RNNBreadthFirstSearch(GraphBounds graph) { public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServiceTime) { + int numberVisitedNodes = 0; + if (graph.getPoi(customer.getId()) != null) { ArrayList path = new ArrayList(); path.add(customer.getId()); - return new NearestNeighbor(customer.getId(), 0, path); + numberVisitedNodes = numberVisitedNodes + 1; + + NearestNeighbor nearestNeighbor = new NearestNeighbor(customer.getId(), 0, path); + nearestNeighbor.setNumberVisitedNodes(numberVisitedNodes); + return nearestNeighbor; } PriorityQueue queue = new PriorityQueue(); @@ -47,6 +53,7 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi while(!queue.isEmpty()) { current = queue.poll(); + numberVisitedNodes = numberVisitedNodes + 1; if (visited.contains(current.getId())) { continue; } else { @@ -59,13 +66,17 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi } if (graph.getPoi(current.getId()) != null) { - return new NearestNeighbor(current.getId(), current.getTravelTime(), pathToTaxi(current.getId(), customer.getId(), parents)); + ArrayList pathToTaxi = pathToTaxi(current.getId(), customer.getId(), parents); + NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), current.getTravelTime(), + pathToTaxi, numberVisitedNodes); + return nearestNeighbor; } // Acessa os vizinhos do primeiro vértice da pilha, no caso os vizinho do vértice que representa o cliente. HashMap neighbors = graph.accessNeighborhood(graph.getNode(current.getId()), current.getArrivalTime()); for (Node neighbor : neighbors.keySet()) { + numberVisitedNodes = numberVisitedNodes + 1; if (visited.contains(neighbor.getId())) { continue; } @@ -85,7 +96,6 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi queue.offer(newRouteQueueTaxiEntry); } - } throw new PathNotFoundException("not found path in reverse graph"); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java b/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java index a279b47..263117a 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java @@ -20,6 +20,7 @@ public class RNNComparatorTest { private Date endServiceTime = null; private Date startServiceTime = null; private Double percentagemPoi = null; + private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; @Before public void setUp() throws ParseException, IOException { @@ -45,13 +46,13 @@ public void taxiSearchSytenticGraph1k() throws IOException, ParseException { int comprimento = 32; int altura = 32; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); //==== SOLUÇÃO I ==== long startSolution1 = System.currentTimeMillis(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graph); NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); long endSolution1 = System.currentTimeMillis(); long timeSolution1 = endSolution1-startSolution1; @@ -79,13 +80,13 @@ public void taxiSearchSytenticGraph10k() throws IOException, ParseException { int comprimento = 100; int altura = 100; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); //==== SOLUÇÃO I ==== long startSolution1 = System.currentTimeMillis(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graph); NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); long endSolution1 = System.currentTimeMillis(); long timeSolution1 = endSolution1-startSolution1; @@ -113,7 +114,7 @@ public void taxiSearchSytenticGraph100k() throws IOException, ParseException { int comprimento = 316; int altura = 316; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -123,7 +124,7 @@ public void taxiSearchSytenticGraph100k() throws IOException, ParseException { //==== SOLUÇÃO I ==== long startSolution1 = System.currentTimeMillis(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graph); NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); long endSolution1 = System.currentTimeMillis(); long timeSolution1 = endSolution1-startSolution1; @@ -150,7 +151,7 @@ public void taxiSearchSytenticGraph1000k() throws IOException, ParseException { int comprimento = 1000; int altura = 1000; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -160,7 +161,7 @@ public void taxiSearchSytenticGraph1000k() throws IOException, ParseException { //==== SOLUÇÃO I ==== long startSolution1 = System.currentTimeMillis(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graph); NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); long endSolution1 = System.currentTimeMillis(); long timeSolution1 = endSolution1-startSolution1; From a554c7fe59e1e7aa8d905e10c9ecf797f2e334ec Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:33:13 -0300 Subject: [PATCH 029/118] create compare method synthetic analysis. --- ...areRNNSearchsMethodsSyntheticAnalysis.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java new file mode 100644 index 0000000..e8d0796 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java @@ -0,0 +1,116 @@ +package org.graphast.query.rnn; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.logging.Logger; + +import org.graphast.config.Configuration; +import org.graphast.exception.PathNotFoundException; +import org.graphast.importer.GraphGeneratorGrid; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.DateUtils; +import org.graphast.util.NumberUtils; + +public class CompareRNNSearchsMethodsSyntheticAnalysis { + + private static final String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + protected static final Logger LOGGER = Logger.getGlobal(); + + public static void main(String[] args) throws IOException { + + runAnalysis("1k", 32, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + runAnalysis("10k", 100, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + runAnalysis("100k", 316 , Integer.parseInt(args[0]), Integer.parseInt(args[1])); + runAnalysis("1000k", 1000, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + } + + public static void runAnalysis(String experimentName, int side, int percentagemPoi, int testTimes) throws IOException { + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH+experimentName, side, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + GraphGeneratorGrid graphSyntheticReverse = new GraphGeneratorGrid(PATH_GRAPH+experimentName, side, percentagemPoi); + graphSyntheticReverse.generateGraph(); + GraphBounds graphReverse = graphSynthetic.getGraph(); + + + RNNBacktrackingSearch rnnDFS = new RNNBacktrackingSearch(graph); + RNNBreadthFirstSearch rnnBFS = new RNNBreadthFirstSearch(graphReverse); + + Date timeout = DateUtils.parseDate(00, 50, 00); + Date timestamp = DateUtils.parseDate(00, 00, 00); + + FileWriter rnnDFSFileCsv = new FileWriter(experimentName+"_"+percentagemPoi+"_rnn_dfs_baseline.csv"); + FileWriter rnnBFSFileCsv = new FileWriter(experimentName+"_"+percentagemPoi+"_rnn_bfs_solution.csv"); + + for (int i = 0; i < testTimes; i++) { + Node customer = getRandomCustomerInGraph(graph); + runSearchAndWrite(graph, rnnDFS, customer, timeout, timestamp, rnnDFSFileCsv); + runSearchAndWrite(graph, rnnBFS, customer, timeout, timestamp, rnnBFSFileCsv); + } + + rnnBFSFileCsv.close(); + rnnDFSFileCsv.close(); + } + + private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, + Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { + try { + long startTime = System.nanoTime(); + NearestNeighbor solution = rnn.search(customer, timeout, timestamp); + long endTime = System.nanoTime(); + + long time = endTime - startTime; + + Long solutionId = null; + Integer distance = null; + Integer nodesSize = null; + ArrayList path = null; + int numberVisitedNodes = 0; + if(solution != null && solution.getPath()!=null) { + solutionId = solution.getId(); + distance = solution.getDistance(); + nodesSize = solution.getPath().size(); + path = solution.getPath(); + numberVisitedNodes = solution.getNumberVisitedNodes(); + + String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); + + Node nodePoi = graph.getNode(solutionId); + String poiCoordinate = nodePoi.getLongitude() + "," + nodePoi.getLatitude(); + String gidPoi = nodePoi.getLabel(); + + String coordinateNodeVisited = ""; + for (Long visited : path) { + Node nodeVisited = graph.getNode(visited); + coordinateNodeVisited = coordinateNodeVisited + "(" + nodeVisited.getLongitude() + "," + nodeVisited.getLatitude() + ")"; + } + + String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, + poiCoordinate, time, solutionId, distance, nodesSize, path, coordinateNodeVisited, gidPoi, numberVisitedNodes) + "\n"; + + + System.out.println(currentLine); + + fileCsv.write(currentLine); + } + } catch(PathNotFoundException e) { + System.err.println(String.format("Customer %s (%s, %s) has no POI in subgraph", customer.getId(), customer.getLatitude(), customer.getLongitude())); + } + } + + + private static Node getRandomCustomerInGraph(GraphBounds graph) { + Node node; + do { + long id = Double.valueOf(NumberUtils.generatePdseurandom(0, Long.valueOf(graph.getNumberOfNodes()-1).intValue())).longValue(); + node = graph.getNode(id); + } while(node.getCategory()!=-1); + return node; + } +} From 8f4a07d567a0b44796feb478871d462626470c93 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:33:26 -0300 Subject: [PATCH 030/118] add parameter of the number visited nodes --- .../query/rnn/CompareRNNSearchsMethodsAnalysis.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 601750b..1258eff 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -36,23 +36,24 @@ public static void runAnalysis(String tableName, int testTimes) throws IOExcepti GraphBounds graphReverse = importerReverse.execute(); - RNNDepthFirstSearch rnnDFS = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch rnnDFS = new RNNBacktrackingSearch(graph); RNNBreadthFirstSearch rnnBFS = new RNNBreadthFirstSearch(graphReverse); Date timeout = DateUtils.parseDate(00, 50, 00); Date timestamp = DateUtils.parseDate(00, 00, 00); - FileWriter rnnDFSFileCsv = new FileWriter(tableName+"_rnn_dfs.csv"); - FileWriter rnnBFSFileCsv = new FileWriter(tableName+"_rnn_bfs.csv"); + FileWriter rnnBacktrackingFileCsv = new FileWriter(tableName+"_rnn_baseline.csv"); + FileWriter rnnBFSFileCsv = new FileWriter(tableName+"_rnn_bfs_proposed_solution.csv"); for (int i = 0; i < testTimes; i++) { Node customer = getRandomCustomerInGraph(graph); - runSearchAndWrite(graph, rnnDFS, customer, timeout, timestamp, rnnDFSFileCsv); + runSearchAndWrite(graph, rnnDFS, customer, timeout, timestamp, rnnBacktrackingFileCsv); runSearchAndWrite(graph, rnnBFS, customer, timeout, timestamp, rnnBFSFileCsv); } + rnnBacktrackingFileCsv.close(); rnnBFSFileCsv.close(); - rnnDFSFileCsv.close(); + } private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, @@ -74,7 +75,6 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, distance = solution.getDistance(); nodesSize = solution.getPath().size(); path = solution.getPath(); - //externalId = graph.getNode(solution.getId()).getExternalId(); externalId = Integer.valueOf(graph.getNode(solution.getId()).getCategory()).longValue(); String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); From 19989af36f5a9b8ff38a925588caca193c920cf2 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:40:52 -0300 Subject: [PATCH 031/118] Milissegundo to Nanosegundos --- .../org/graphast/query/rnn/RNNBreadthFirstSearch.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java index 88bd941..5f707a7 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -66,9 +66,13 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi } if (graph.getPoi(current.getId()) != null) { + + double totalCostInMilissegundo = current.getTravelTime(); + double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); ArrayList pathToTaxi = pathToTaxi(current.getId(), customer.getId(), parents); - NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), current.getTravelTime(), - pathToTaxi, numberVisitedNodes); + + NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), + Double.valueOf(totalCostInNanosegundos).intValue(), pathToTaxi, numberVisitedNodes); return nearestNeighbor; } From 3fb8eeb37e4530ad0ffb38f55c857cde9bbbac70 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 17:17:14 -0300 Subject: [PATCH 032/118] add travel time. --- .../org/graphast/query/knn/NearestNeighbor.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java b/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java index 3b1ab63..a4cd720 100755 --- a/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java +++ b/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java @@ -5,6 +5,7 @@ public class NearestNeighbor implements Comparable { private long id; private int distance; + private double travelTime; private ArrayList path; private int numberVisitedNodes; @@ -26,6 +27,14 @@ public NearestNeighbor(long id, int distance, ArrayList path, int numberVi this(id, distance, path); this.numberVisitedNodes = numberVisitedNodes; } + + public NearestNeighbor(long id, double travelTime, ArrayList path, int numberVisitedNodes){ + this.id = id; + this.path = path; + this.travelTime = travelTime; + this.numberVisitedNodes = numberVisitedNodes; + } + public long getId() { return id; @@ -75,4 +84,12 @@ public int getNumberVisitedNodes() { public void setNumberVisitedNodes(int numberVisitedNodes) { this.numberVisitedNodes = numberVisitedNodes; } + + public double getTravelTime() { + return travelTime; + } + + public void setTravelTime(double travelTime) { + this.travelTime = travelTime; + } } From 3241e53edd049d1c0f790317eb5cf0437525247f Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 17:18:18 -0300 Subject: [PATCH 033/118] add travel time. --- .../org/graphast/importer/CostGenerator.java | 9 +++--- .../org/graphast/importer/OSMDBImporter.java | 4 ++- .../rnn/CompareRNNSearchsMethodsAnalysis.java | 18 ++++++------ ...areRNNSearchsMethodsSyntheticAnalysis.java | 6 ++-- .../query/rnn/RNNBacktrackingSearch.java | 2 +- .../query/rnn/RNNBreadthFirstSearch.java | 2 +- .../GraphGeneratorGridTest.java | 28 +++++++++---------- ...rid.java => GraphGeneratorGridTester.java} | 6 ++-- .../knn/RNNBreadthFirstSearchTest.java | 4 +-- .../org/graphast/knn}/RNNComparatorTest.java | 14 ++++++---- 10 files changed, 50 insertions(+), 43 deletions(-) rename core/src/test/java/org/graphast/graphgenerator/{GraphGeneratorGrid.java => GraphGeneratorGridTester.java} (94%) rename core/src/{main/java/org/graphast/query/rnn => test/java/org/graphast/knn}/RNNComparatorTest.java (91%) diff --git a/core/src/main/java/org/graphast/importer/CostGenerator.java b/core/src/main/java/org/graphast/importer/CostGenerator.java index 710b5af..e580a91 100644 --- a/core/src/main/java/org/graphast/importer/CostGenerator.java +++ b/core/src/main/java/org/graphast/importer/CostGenerator.java @@ -4,18 +4,19 @@ import org.graphast.model.Graph; + public class CostGenerator { public static int[] generateSyntheticEdgesCosts(int distance) { Random random = new Random(); - int minSpeed, maxSpeed; + int minSpeed, maxSpeed; //Millimeters Per Millisecond (mm/ms) int[] syntheticCosts = new int[96]; for(int i=0; i<24; i++) { - minSpeed = 14; - maxSpeed = 17; + minSpeed = 14; // 50km/h + maxSpeed = 17; // 60km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); @@ -46,7 +47,7 @@ public static int[] generateSyntheticEdgesCosts(int distance) { } for(int i=44; i<56; i++) { - minSpeed = 1; + minSpeed = 1; //3km/h maxSpeed = 4; syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java index b986bf9..c3e23d1 100644 --- a/core/src/main/java/org/graphast/importer/OSMDBImporter.java +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -142,7 +142,9 @@ private void addCost(Edge edge) { DistanceUtils.distanceLatLong(nodeFrom.getLatitude(), nodeFrom.getLongitude(), nodeTo.getLatitude(), nodeTo.getLongitude()); - int[] edgesCosts = CostGenerator.generateSyntheticEdgesCosts(Double.valueOf(distanceBetweenLatLongHaversine).intValue()); + double distanceBetweenLatLongHaversineInMill = distanceBetweenLatLongHaversine * 1000; + + int[] edgesCosts = CostGenerator.generateSyntheticEdgesCosts(Double.valueOf(distanceBetweenLatLongHaversineInMill).intValue()); edge.setCosts(edgesCosts); } } diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 1258eff..9b92909 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -22,9 +22,9 @@ public class CompareRNNSearchsMethodsAnalysis { public static void main(String[] args) throws IOException { runAnalysis("view_exp_1k", Integer.parseInt(args[0])); - runAnalysis("view_exp_10k", Integer.parseInt(args[0])); - runAnalysis("view_exp_100k", Integer.parseInt(args[0])); - runAnalysis("view_exp_300mil", Integer.parseInt(args[0])); + /*runAnalysis("view_exp_10k", Integer.parseInt(args[0])); + runAnalysis("view_exp_50k", Integer.parseInt(args[0])); + runAnalysis("view_exp_100k", Integer.parseInt(args[0]));*/ } public static void runAnalysis(String tableName, int testTimes) throws IOException { @@ -66,16 +66,18 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, long time = endTime - startTime; Long solutionId = null; - Integer distance = null; + Double travelTime = null; Integer nodesSize = null; ArrayList path = null; Long externalId = null; + int numberVisitedNodes = 0; if(solution != null && solution.getPath()!=null) { solutionId = solution.getId(); - distance = solution.getDistance(); + travelTime = solution.getTravelTime(); nodesSize = solution.getPath().size(); path = solution.getPath(); externalId = Integer.valueOf(graph.getNode(solution.getId()).getCategory()).longValue(); + numberVisitedNodes = solution.getNumberVisitedNodes(); String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); String gidCustomer = customer.getLabel(); @@ -93,9 +95,9 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, gidVisited = gidVisited + "-" + nodeVisited.getLabel(); } - String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, - poiCoordinate, time, solutionId, externalId, distance, - nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited) + "\n"; + String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, + poiCoordinate, time, solutionId, externalId, travelTime, + nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited, numberVisitedNodes) + "\n"; System.out.println(currentLine); diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java index e8d0796..cd05aeb 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java @@ -68,13 +68,13 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, long time = endTime - startTime; Long solutionId = null; - Integer distance = null; + Double travelTime = null; Integer nodesSize = null; ArrayList path = null; int numberVisitedNodes = 0; if(solution != null && solution.getPath()!=null) { solutionId = solution.getId(); - distance = solution.getDistance(); + travelTime = solution.getTravelTime(); nodesSize = solution.getPath().size(); path = solution.getPath(); numberVisitedNodes = solution.getNumberVisitedNodes(); @@ -92,7 +92,7 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, } String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, - poiCoordinate, time, solutionId, distance, nodesSize, path, coordinateNodeVisited, gidPoi, numberVisitedNodes) + "\n"; + poiCoordinate, time, solutionId, travelTime, nodesSize, path, coordinateNodeVisited, gidPoi, numberVisitedNodes) + "\n"; System.out.println(currentLine); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java index 07cfcba..e62b0b2 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java @@ -66,7 +66,7 @@ private NearestNeighbor createNN(Node root, long currentPoi, int numberVisitedNo double totalCostInMilissegundo = path.getTotalCost(); double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); - nearestNeighbor.setDistance(Double.valueOf(totalCostInNanosegundos).intValue()); + nearestNeighbor.setTravelTime(Double.valueOf(totalCostInNanosegundos)); nearestNeighbor.setId(currentPoi); nearestNeighbor.setNumberVisitedNodes(numberVisitedNodes); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java index 5f707a7..6eccc3d 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -72,7 +72,7 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi ArrayList pathToTaxi = pathToTaxi(current.getId(), customer.getId(), parents); NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), - Double.valueOf(totalCostInNanosegundos).intValue(), pathToTaxi, numberVisitedNodes); + Double.valueOf(totalCostInNanosegundos), pathToTaxi, numberVisitedNodes); return nearestNeighbor; } diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java index 43a2057..e6405be 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java @@ -13,7 +13,7 @@ public class GraphGeneratorGridTest { @Test public void gererateGraphSynthetic2x2() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 2,2,0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, 2,2,0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -25,7 +25,7 @@ public void gererateGraphSynthetic2x2() { @Test public void gererateGraphSynthetic4x4() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 4,4, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, 4,4, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -37,7 +37,7 @@ public void gererateGraphSynthetic4x4() { @Test public void gererateGraphSynthetic5x5() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 5,5, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, 5,5, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -48,7 +48,7 @@ public void gererateGraphSynthetic5x5() { @Test public void gererateGraphSynthetic100x100() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 100,100, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, 100,100, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -63,7 +63,7 @@ public void gererateGraphSyntheticLimit() { int comprimento = 992; int altura = 992; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -78,7 +78,7 @@ public void gererateGraphSyntheticLimitWithPoi() { int comprimento = 992; int altura = 992; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -94,7 +94,7 @@ public void gererateGraphSyntheticDifferentSize() { int comprimento = 3; int altura = 2; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -108,7 +108,7 @@ public void gererateGraphSyntheticWithPoiShort() { int comprimento = 4; int altura = 4; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento, altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -121,7 +121,7 @@ public void gererateGraphSyntheticWithPoi() { int comprimento = 10; int altura = 10; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, 2); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento, altura, 2); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -135,7 +135,7 @@ public void gererateGraphSyntheticMin() { int altura = 1; int percentagemPoi = 1; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento, altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -152,7 +152,7 @@ public void gererateGraphSyntheticLimitWithPoi1k() { int altura = 32; int qtdPoi = 10; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -170,7 +170,7 @@ public void gererateGraphSyntheticLimitWithPoi10k() { int qtdPoi = 100; int percentualPoi = 1; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentualPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentualPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -187,7 +187,7 @@ public void gererateGraphSyntheticLimitWithPoi100k() { int altura = 316; int qtdPoi = 998; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -204,7 +204,7 @@ public void gererateGraphSyntheticLimitWithPoi1000k() { int altura = 1000; int qtdPoi = 10000; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java similarity index 94% rename from core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java rename to core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java index 5beb2a3..013d748 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java @@ -14,7 +14,7 @@ import org.graphast.model.Node; import org.graphast.model.NodeImpl; -public class GraphGeneratorGrid { +public class GraphGeneratorGridTester { private int width; private int length; @@ -23,14 +23,14 @@ public class GraphGeneratorGrid { private BigDecimal distance_largura; private BigDecimal distance_altura; - public GraphGeneratorGrid(String pathGraph, int width, int length, double percentualPoi) { + public GraphGeneratorGridTester(String pathGraph, int width, int length, double percentualPoi) { this.width = width; this.length = length; this.percentagemPoi = percentualPoi; this.graph = new GraphBoundsImpl(pathGraph); } - public GraphGeneratorGrid(String pathGraph, int size, double percentualPoi) { + public GraphGeneratorGridTester(String pathGraph, int size, double percentualPoi) { this(pathGraph, size, size, percentualPoi); } diff --git a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java index cb69f67..f62b972 100644 --- a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java +++ b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java @@ -10,7 +10,7 @@ import org.graphast.config.Configuration; import org.graphast.exception.PathNotFoundException; import org.graphast.graphgenerator.GraphGenerator; -import org.graphast.graphgenerator.GraphGeneratorGrid; +import org.graphast.graphgenerator.GraphGeneratorGridTester; import org.graphast.model.GraphBounds; import org.graphast.query.knn.NearestNeighbor; import org.graphast.query.rnn.RNNBreadthFirstSearch; @@ -51,7 +51,7 @@ public void setUpRandomGraph(int qtdX, int qtdY, int percentPois) throws ParseEx // Hora que ele realiza a chamada do serviço meia-noite e vinte minutos hourServiceTime = DateUtils.parseDate(00, 00, 00); - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, qtdX, qtdY, percentPois); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, qtdX, qtdY, percentPois); graphSynthetic.generateGraph(); graphBoundsReverse = graphSynthetic.getGraph(); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java b/core/src/test/java/org/graphast/knn/RNNComparatorTest.java similarity index 91% rename from core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java rename to core/src/test/java/org/graphast/knn/RNNComparatorTest.java index 263117a..29e22dd 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java +++ b/core/src/test/java/org/graphast/knn/RNNComparatorTest.java @@ -1,13 +1,15 @@ -package org.graphast.query.rnn; +package org.graphast.knn; import java.io.IOException; import java.text.ParseException; import java.util.Date; import org.graphast.config.Configuration; -import org.graphast.graphgenerator.GraphGeneratorGrid; +import org.graphast.graphgenerator.GraphGeneratorGridTester; import org.graphast.model.GraphBounds; import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.rnn.RNNBacktrackingSearch; +import org.graphast.query.rnn.RNNBreadthFirstSearch; import org.graphast.util.DateUtils; import org.graphast.util.FileUtils; import org.junit.AfterClass; @@ -46,7 +48,7 @@ public void taxiSearchSytenticGraph1k() throws IOException, ParseException { int comprimento = 32; int altura = 32; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -80,7 +82,7 @@ public void taxiSearchSytenticGraph10k() throws IOException, ParseException { int comprimento = 100; int altura = 100; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -114,7 +116,7 @@ public void taxiSearchSytenticGraph100k() throws IOException, ParseException { int comprimento = 316; int altura = 316; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -151,7 +153,7 @@ public void taxiSearchSytenticGraph1000k() throws IOException, ParseException { int comprimento = 1000; int altura = 1000; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); From 00fd97c60c714477094fab1fa7a853924be630b5 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 15 Dec 2015 07:22:22 -0300 Subject: [PATCH 034/118] logger --- .../org/graphast/importer/CostGenerator.java | 44 +++++++----- core/src/main/resources/log4j.properties | 4 +- .../graphast/importer/OSMDBImporterTest.java | 67 ++++++++++++++++--- 3 files changed, 88 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/org/graphast/importer/CostGenerator.java b/core/src/main/java/org/graphast/importer/CostGenerator.java index e580a91..6667212 100644 --- a/core/src/main/java/org/graphast/importer/CostGenerator.java +++ b/core/src/main/java/org/graphast/importer/CostGenerator.java @@ -14,6 +14,7 @@ public static int[] generateSyntheticEdgesCosts(int distance) { int minSpeed, maxSpeed; //Millimeters Per Millisecond (mm/ms) int[] syntheticCosts = new int[96]; + //1:00h to 6:00h for(int i=0; i<24; i++) { minSpeed = 14; // 50km/h maxSpeed = 17; // 60km/h @@ -22,73 +23,82 @@ public static int[] generateSyntheticEdgesCosts(int distance) { } + //6:00h to 7:00h for(int i=24; i<28; i++) { - minSpeed = 6; - maxSpeed = 9; + minSpeed = 6; //21km/h + maxSpeed = 9; //32km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //7:00h to 9:00h for(int i=28; i<36; i++) { - minSpeed = 1; - maxSpeed = 4; + minSpeed = 1; //3km/h + maxSpeed = 4; //14km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //9:00h to 11:00h for(int i=36; i<44; i++) { - minSpeed = 6; - maxSpeed = 9; + minSpeed = 6; //21km/h + maxSpeed = 9; //32km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //11:00h to 14:00h for(int i=44; i<56; i++) { minSpeed = 1; //3km/h - maxSpeed = 4; + maxSpeed = 4; //14km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //14:00h to 16:00h for(int i=56; i<64; i++) { - minSpeed = 14; - maxSpeed = 17; + minSpeed = 14; //50km/h + maxSpeed = 17; //60km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //16:00h to 17:00h for(int i=64; i<68; i++) { - minSpeed = 6; - maxSpeed = 9; + minSpeed = 6; //21km/h + maxSpeed = 9; //30/km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //17:00h to 20:00h for(int i=68; i<80; i++) { - minSpeed = 1; - maxSpeed = 4; + minSpeed = 1; //3km/h + maxSpeed = 4; //14km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //20:00h to 22:00h for(int i=80; i<88; i++) { - minSpeed = 6; - maxSpeed = 9; + minSpeed = 6; //21km/h + maxSpeed = 9; //30/km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //22:00h to 00:00h for(int i=88; i<96; i++) { - minSpeed = 14; - maxSpeed = 17; + minSpeed = 14; //50km/h + maxSpeed = 17; //60km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); diff --git a/core/src/main/resources/log4j.properties b/core/src/main/resources/log4j.properties index 31249d0..0fa3f73 100755 --- a/core/src/main/resources/log4j.properties +++ b/core/src/main/resources/log4j.properties @@ -24,4 +24,6 @@ log4j.category.org.apache=INFO log4j.category.br.com.nex2me.mobme.network.road.model.RoadGraphAdapter=INFO log4j.category.br.com.nex2me.mobme.core.model.GraphAdapter=INFO log4j.category.com.thinkaurelius.titan=ERROR -log4j.category.org.apache.cassandra=ERROR \ No newline at end of file +log4j.category.org.apache.cassandra=ERROR + +log4j.category.org.graphast.importer.OSMDBImporter=ERROR \ No newline at end of file diff --git a/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java b/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java index 413a53c..df92fca 100644 --- a/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java +++ b/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java @@ -1,8 +1,13 @@ package org.graphast.importer; +import java.util.HashMap; + +import it.unimi.dsi.fastutil.longs.Long2IntMap; + import org.graphast.config.Configuration; import org.graphast.model.Edge; import org.graphast.model.GraphBounds; +import org.graphast.model.Node; import org.junit.Assert; import org.junit.Test; @@ -19,9 +24,18 @@ public void createGraphTest1k() { //Assert.assertEquals(809+15, graph.getNumberOfNodes()); //Assert.assertEquals(844+15, graph.getNumberOfEdges()); - Assert.assertEquals(14, graph.getCategories().size()); - Assert.assertEquals(809, graph.getNumberOfNodes()); - Assert.assertEquals(844, graph.getNumberOfEdges()); + //Assert.assertEquals(14, graph.getCategories().size()); + //Assert.assertEquals(809, graph.getNumberOfNodes()); + //Assert.assertEquals(844, graph.getNumberOfEdges()); + + for (int i = 0; i < graph.getNumberOfNodes(); i++) { + Node node = graph.getNode(i); + HashMap accessNeighborhood = graph.accessNeighborhood(node, 0); + int size = accessNeighborhood.size(); + //System.out.println(String.format("the node %s contens %s neighbors", i, size)); + System.out.println(String.format("%s,%s", i, size)); + + } } @Test @@ -41,9 +55,18 @@ public void createGraphTest10k() { OSMDBImporter importer = new OSMDBImporter("view_exp_10k", PATH_GRAPH); GraphBounds graph = importer.execute(); Assert.assertNotNull(graph); - Assert.assertEquals(77, graph.getCategories().size()); - Assert.assertEquals(6947, graph.getNumberOfNodes()); - Assert.assertEquals(8411, graph.getNumberOfEdges()); + Assert.assertEquals(73, graph.getCategories().size()); //77 + Assert.assertEquals(6870, graph.getNumberOfNodes()); //6947 + Assert.assertEquals(8334, graph.getNumberOfEdges()); // + + for (int i = 0; i < graph.getNumberOfNodes(); i++) { + Node node = graph.getNode(i); + HashMap accessNeighborhood = graph.accessNeighborhood(node, 0); + int size = accessNeighborhood.size(); + //System.out.println(String.format("the node %s contens %s neighbors", i, size)); + System.out.println(String.format("%s,%s", i, size)); + + } } @Test @@ -51,9 +74,35 @@ public void createGraphTest100k() { OSMDBImporter importer = new OSMDBImporter("view_exp_100k", PATH_GRAPH); GraphBounds graph = importer.execute(); Assert.assertNotNull(graph); - Assert.assertEquals(425, graph.getCategories().size()); - Assert.assertEquals(75919, graph.getNumberOfNodes()); - Assert.assertEquals(98653, graph.getNumberOfEdges()); + Assert.assertEquals(379, graph.getCategories().size());//379 + Assert.assertEquals(75489, graph.getNumberOfNodes()); //75919 + Assert.assertEquals(98223, graph.getNumberOfEdges()); //98653 + + for (int i = 0; i < graph.getNumberOfNodes(); i++) { + Node node = graph.getNode(i); + HashMap accessNeighborhood = graph.accessNeighborhood(node, 0); + int size = accessNeighborhood.size(); + System.out.println(String.format("%s,%s", i, size)); + + } + } + + @Test + public void createGraphTest50k() { + OSMDBImporter importer = new OSMDBImporter("view_exp_50k", PATH_GRAPH); + GraphBounds graph = importer.execute(); + Assert.assertNotNull(graph); + Assert.assertEquals(328, graph.getCategories().size()); // 425 + Assert.assertEquals(33595, graph.getNumberOfNodes()); // 236216 + Assert.assertEquals(42385, graph.getNumberOfEdges()); + + for (int i = 0; i < graph.getNumberOfNodes(); i++) { + Node node = graph.getNode(i); + HashMap accessNeighborhood = graph.accessNeighborhood(node, 0); + int size = accessNeighborhood.size(); + System.out.println(String.format("%s,%s", i, size)); + + } } @Test From 8e3d433721281e75558c29bedb69c63058f508be Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 15 Jan 2016 10:51:43 -0300 Subject: [PATCH 035/118] Generalizado forma de passar o tamanho da rede. Por parametro. --- ...areRNNSearchsMethodsSyntheticAnalysis.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java index cd05aeb..ef02bd3 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java @@ -22,10 +22,25 @@ public class CompareRNNSearchsMethodsSyntheticAnalysis { public static void main(String[] args) throws IOException { - runAnalysis("1k", 32, Integer.parseInt(args[0]), Integer.parseInt(args[1])); - runAnalysis("10k", 100, Integer.parseInt(args[0]), Integer.parseInt(args[1])); - runAnalysis("100k", 316 , Integer.parseInt(args[0]), Integer.parseInt(args[1])); - runAnalysis("1000k", 1000, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + int side = 0; + switch (args[0]) { + case "1k": + side = 32; + break; + case "10k": + side = 100; + break; + case "100k": + side = 316; + break; + case "1000k": + side = 1000; + break; + default: + break; + } + + runAnalysis(args[0], side, Integer.parseInt(args[1]), Integer.parseInt(args[2])); } public static void runAnalysis(String experimentName, int side, int percentagemPoi, int testTimes) throws IOException { From e3e64fdd71bb0f86fbf5875e353d2142da54bf4d Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 15 Jan 2016 10:52:44 -0300 Subject: [PATCH 036/118] =?UTF-8?q?Alterado=20forma=20de=20capturar=20quan?= =?UTF-8?q?tidade=20de=20n=C3=B3s=20visitados.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../query/route/shortestpath/dijkstra/Dijkstra.java | 9 +++++---- .../shortestpath/dijkstra/DijkstraConstantWeight.java | 4 ++-- .../shortestpath/dijkstra/DijkstraLinearFunction.java | 9 ++------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java index 5c9e76c..baa3282 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java @@ -6,8 +6,10 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; +import java.util.Set; import org.graphast.exception.PathNotFoundException; import org.graphast.model.Graph; @@ -54,20 +56,19 @@ protected List reconstructPath(long id, HashMap pa public Path shortestPath(Node source, Node target, Date time) { PriorityQueue queue = new PriorityQueue(); HashMap wasTraversed = new HashMap(); - List allWasViseted = new ArrayList(); + Set allWasViseted = new HashSet(); HashMap parents = new HashMap(); TimeEntry removed = null; int targetId = convertToInt(target.getId()); int timeInMilli = DateUtils.dateToMilli(time); init(source, target, queue, parents, timeInMilli); + allWasViseted.add(source.getId()); while(!queue.isEmpty()) { removed = queue.poll(); long idRemoved = removed.getId(); wasTraversed.put(idRemoved, wasRemoved); - allWasViseted.add(idRemoved); - //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); if(removed.getId() == targetId) { Path path = new Path(); @@ -91,7 +92,7 @@ public void init(Node source, Node target, PriorityQueue queue, } public abstract void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, - List wasVisited, PriorityQueue queue, HashMap parents); + Set wasVisited, PriorityQueue queue, HashMap parents); @Override public Path shortestPath(Node source, Node target) { diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java index 9efbd83..fe33122 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java @@ -3,8 +3,8 @@ import it.unimi.dsi.fastutil.longs.Long2IntMap; import java.util.HashMap; -import java.util.List; import java.util.PriorityQueue; +import java.util.Set; import org.graphast.model.Edge; import org.graphast.model.Graph; @@ -23,7 +23,7 @@ public DijkstraConstantWeight(GraphBounds graphBounds) { super(graphBounds); } - public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, List wasVisited, + public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, Set wasVisited, PriorityQueue queue, HashMap parents){ Long2IntMap neig = graph.accessNeighborhood(graph.getNode(removed.getId())); diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java index 8bf5eb7..a24a1e7 100755 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java @@ -33,13 +33,14 @@ public DijkstraLinearFunction(GraphBounds graphBounds) { super(graphBounds); } - public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, List allWasViseted, + public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, Set allWasViseted, PriorityQueue queue, HashMap parents){ HashMap neighbors = graph.accessNeighborhood(graph.getNode(removed.getId()), removed.getArrivalTime()); for (Node node : neighbors.keySet()) { long vid = node.getId(); + allWasViseted.add(vid); int at = graph.getArrival(removed.getArrivalTime(), neighbors.get(node)); int tt = removed.getTravelTime() + neighbors.get(node); TimeEntry newEntry = new TimeEntry( vid, tt, at, removed.getId()); @@ -49,10 +50,7 @@ public void expandVertex(Node target, TimeEntry removed, HashMap if(!wasTraversed.containsKey(vid)){ queue.offer(newEntry); - long idNewEntry = newEntry.getId(); wasTraversed.put(newEntry.getId(), newEntry.getTravelTime()); - allWasViseted.add(idNewEntry); - //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); distance = neighbors.get(node); edge = getEdge(removed.getId(), vid, distance); @@ -66,9 +64,6 @@ public void expandVertex(Node target, TimeEntry removed, HashMap long idNewEntry = newEntry.getId(); wasTraversed.remove(idNewEntry); wasTraversed.put(idNewEntry, newEntry.getTravelTime()); - allWasViseted.add(idNewEntry); - //System.out.println("vertex already visited"); - //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); parents.remove(node); distance = neighbors.get(node); From 4cdb87ea46a373a2591a0b42a8bf6cfec26ed17c Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 15 Jan 2016 10:55:11 -0300 Subject: [PATCH 037/118] refactorado nome de variavel --- .../org/graphast/query/rnn/RNNBacktrackingSearch.java | 2 +- .../org/graphast/query/rnn/RNNBreadthFirstSearch.java | 4 ++-- .../java/org/graphast/knn/RNNBreadthFirstSearchTest.java | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java index e62b0b2..bfac129 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java @@ -66,7 +66,7 @@ private NearestNeighbor createNN(Node root, long currentPoi, int numberVisitedNo double totalCostInMilissegundo = path.getTotalCost(); double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); - nearestNeighbor.setTravelTime(Double.valueOf(totalCostInNanosegundos)); + nearestNeighbor.setTravelTime(totalCostInNanosegundos); nearestNeighbor.setId(currentPoi); nearestNeighbor.setNumberVisitedNodes(numberVisitedNodes); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java index 6eccc3d..6e07b6e 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -71,8 +71,8 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); ArrayList pathToTaxi = pathToTaxi(current.getId(), customer.getId(), parents); - NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), - Double.valueOf(totalCostInNanosegundos), pathToTaxi, numberVisitedNodes); + NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(),totalCostInNanosegundos, + pathToTaxi, numberVisitedNodes); return nearestNeighbor; } diff --git a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java index f62b972..60de5cd 100644 --- a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java +++ b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java @@ -39,8 +39,8 @@ public void setUpNoRandomGraph() throws ParseException, IOException { hourServiceTime = DateUtils.parseDate(00, 00, 00); graphBounds = new GraphGenerator().generateExampleTAXI(); - graphBoundsReverse = new GraphGenerator().generateExampleTAXI(); - graphBoundsReverse.reverseGraph(); +// graphBoundsReverse = new GraphGenerator().generateExampleTAXI(); +// graphBoundsReverse.reverseGraph(); } public void setUpRandomGraph(int qtdX, int qtdY, int percentPois) throws ParseException { @@ -94,8 +94,8 @@ public void taxiSearchExpected_II() throws ParseException, IOException { // Tempo para atendiemento maxTravelTime = DateUtils.parseDate(0, 11, 59); - RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); - taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBounds); + taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); } // TESTE 2: O cliente está em um vértice vizinho ao taxista. CLIENTE -> TAXISTA (NÃO REVERSO) From dfca468ada99052c87e7a85cf3838656fe4d2ed9 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 15 Jan 2016 10:55:42 -0300 Subject: [PATCH 038/118] adicionando parametro para captura consumo de memoria total. --- .../rnn/CompareRNNSearchsMethodsAnalysis.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 9b92909..34af678 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -21,7 +21,8 @@ public class CompareRNNSearchsMethodsAnalysis { protected static final Logger LOGGER = Logger.getGlobal(); public static void main(String[] args) throws IOException { - runAnalysis("view_exp_1k", Integer.parseInt(args[0])); + + runAnalysis("view_exp_1k", 10); /*runAnalysis("view_exp_10k", Integer.parseInt(args[0])); runAnalysis("view_exp_50k", Integer.parseInt(args[0])); runAnalysis("view_exp_100k", Integer.parseInt(args[0]));*/ @@ -59,12 +60,19 @@ public static void runAnalysis(String tableName, int testTimes) throws IOExcepti private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { try { + + Runtime.getRuntime().gc(); + long numberUseMemoryInit = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long startTime = System.nanoTime(); - NearestNeighbor solution = rnn.search(customer, timeout, timestamp); + NearestNeighbor solution = null; + solution = rnn.search(customer, timeout, timestamp); long endTime = System.nanoTime(); + long numberUseMemoryFinal = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long time = endTime - startTime; - + long numerUseMemory = (numberUseMemoryFinal - numberUseMemoryInit) / 1024; + System.out.println(" --------- >"+numerUseMemory); + Long solutionId = null; Double travelTime = null; Integer nodesSize = null; From 68a79d8582d20c53fea464cdfad3196b87ddf720 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Sat, 16 Jan 2016 22:42:45 -0300 Subject: [PATCH 039/118] Adicionada classe de Benchmark. --- .../org/graphast/util/BenchmarkMemory.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 core/src/main/java/org/graphast/util/BenchmarkMemory.java diff --git a/core/src/main/java/org/graphast/util/BenchmarkMemory.java b/core/src/main/java/org/graphast/util/BenchmarkMemory.java new file mode 100644 index 0000000..5bf80c6 --- /dev/null +++ b/core/src/main/java/org/graphast/util/BenchmarkMemory.java @@ -0,0 +1,67 @@ +package org.graphast.util; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class BenchmarkMemory { + + private static final String PATTERN_SEPARATOR = ":"; + private static final String PATTERN_NUMBER = "-?\\d+"; + + public static Map quantityMemory() throws IOException { + + final String COMMAND_MEMORY = "egrep --color 'Mem|Cache|Swap' /proc/meminfo\n"; + final String COMMAND_EXIT = "exit\n"; + + Process telnetProcess = createProcessBuilder(); + BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream())); + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream())); + runCommand(COMMAND_MEMORY, output); + runCommand(COMMAND_EXIT, output); + + return runPatter(input); + } + + private static Map runPatter(BufferedReader input) throws IOException { + + Map listMemory = new HashMap(); + String line; + + while ((line = input.readLine()) != null) { + + String[] split = line.split(PATTERN_SEPARATOR); + String keyMemory = split[0]; + String x2 = split[1]; + Matcher matcher = Pattern.compile(PATTERN_NUMBER).matcher(x2); + Long value = 0l; + + while (matcher.find()) { + value = Long.valueOf(matcher.group()); + } + + listMemory.put(keyMemory, value); + } + + return listMemory; + } + + private static Process createProcessBuilder() throws IOException { + + ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash"); + processBuilder.redirectErrorStream(true); + Process telnetProcess = processBuilder.start(); + return telnetProcess; + } + + private static void runCommand(final String command, BufferedWriter output) throws IOException { + output.write(command); + output.flush(); + } +} From caaaecf8aa02fa37111176477bfda0016544896b Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 18 Jan 2016 12:07:06 -0300 Subject: [PATCH 040/118] =?UTF-8?q?descoplando=20gerador=20de=20fun=C3=A7?= =?UTF-8?q?=C3=A3o=20do=20edge=20do=20graph=20e=20o=20engine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/graphast/model/Graph.java | 3 ++ .../piecewise/BuilderDatasourceCSV.java | 10 ++++++ ...Database.java => BuilderDatasourceDB.java} | 2 +- .../java/org/graphast/piecewise/Function.java | 5 +++ ...ewise.java => GeneratorFunctionLoess.java} | 20 +++++++++-- .../piecewise/GeneratorFunctionMatrix.java | 9 +++++ .../GeneratorFunctionPiecewiseCsv.java | 14 -------- .../piecewise/IBuilderDatasource.java | 7 ++++ .../IGeneratorFunctionPiecewise.java | 5 ++- .../piecewise/IManipulatorEngine.java | 7 ++++ .../org/graphast/piecewise/ManipulatorR.java | 10 ++++++ ...eneratorFunctionPiecewiseDatabaseTest.java | 6 ++-- .../piecewise/GeneratorFunctionRefactore.java | 34 +++++++++++++++++++ .../GeneratorGraphFunctionPiecewiseTest.java | 23 +++++++++++++ 14 files changed, 132 insertions(+), 23 deletions(-) create mode 100644 core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java rename core/src/main/java/org/graphast/piecewise/{GeneratorFunctionPiecewiseDatabase.java => BuilderDatasourceDB.java} (94%) create mode 100644 core/src/main/java/org/graphast/piecewise/Function.java rename core/src/main/java/org/graphast/piecewise/{GeneratorFunctionPiecewise.java => GeneratorFunctionLoess.java} (90%) create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java delete mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java create mode 100644 core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java create mode 100644 core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java create mode 100644 core/src/main/java/org/graphast/piecewise/ManipulatorR.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java diff --git a/core/src/main/java/org/graphast/model/Graph.java b/core/src/main/java/org/graphast/model/Graph.java index 4f55d01..9347b83 100644 --- a/core/src/main/java/org/graphast/model/Graph.java +++ b/core/src/main/java/org/graphast/model/Graph.java @@ -8,6 +8,7 @@ import org.graphast.geometry.BBox; import org.graphast.geometry.PoI; import org.graphast.geometry.Point; +import org.graphast.piecewise.Function; import org.graphast.util.FileUtils; import it.unimi.dsi.fastutil.ints.IntBigArrayBigList; @@ -321,4 +322,6 @@ public interface Graph { public String getAbsoluteDirectory(); public void setDirectory(String directory); + + public void setFuntionEdge(long edgeId, Function function); } \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java new file mode 100644 index 0000000..2876a14 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + + +public class BuilderDatasourceCSV implements IBuilderDatasource { + + @Override + public double[][] getData() { + return null; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java similarity index 94% rename from core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java rename to core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java index a74e1b2..2ca233a 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java @@ -9,7 +9,7 @@ import org.graphast.query.postgis.QueryPostgis; import org.graphast.util.ConnectionJDBC; -public class GeneratorFunctionPiecewiseDatabase implements IGeneratorFunctionPiecewise { +public class BuilderDatasourceDB implements IBuilderDatasource { private int FIELD_DATE_TIME = 1; private int FIELD_DURACAO = 2; diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java new file mode 100644 index 0000000..8b2a95d --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -0,0 +1,5 @@ +package org.graphast.piecewise; + +public class Function { + +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java similarity index 90% rename from core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java rename to core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index 4ba69b8..d16777e 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -3,16 +3,32 @@ import rcaller.RCaller; import rcaller.RCode; -public class GeneratorFunctionPiecewise { +public class GeneratorFunctionLoess implements IGeneratorFunctionPiecewise { + + private IManipulatorEngine engine; + + public GeneratorFunctionLoess() { + } + + public GeneratorFunctionLoess(IManipulatorEngine engine) { + this.engine = engine; + } private static final String RSCRIPT = "/usr/bin/Rscript"; public double getValue(double timestamp) throws PiecewiseException { - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); double[][] coleta = generatorFunctionPiecewise.getData(); return getFuntionEdge(coleta, timestamp); } + + @Override + public Function gerFuntionEdge(long idEdge, long timestamp) { + + engine.run(); + return null; + } private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java new file mode 100644 index 0000000..64702a6 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -0,0 +1,9 @@ +package org.graphast.piecewise; + +public class GeneratorFunctionMatrix implements IGeneratorFunctionPiecewise { + + @Override + public double gerFuntionEdge(long idEdge, double timestamp) { + return 0; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java deleted file mode 100644 index b1d9fb0..0000000 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.graphast.piecewise; - -import java.util.Date; -import java.util.List; -import java.util.Map; - -public class GeneratorFunctionPiecewiseCsv implements IGeneratorFunctionPiecewise { - - @Override - public double[][] getData() { - - return null; - } -} diff --git a/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java b/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java new file mode 100644 index 0000000..0cfbe64 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java @@ -0,0 +1,7 @@ +package org.graphast.piecewise; + + +public interface IBuilderDatasource { + + public double[][] getData() throws PiecewiseException; +} diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java index 14dcad7..d6e5066 100644 --- a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java @@ -1,7 +1,6 @@ package org.graphast.piecewise; - public interface IGeneratorFunctionPiecewise { - - public double[][] getData() throws PiecewiseException; + + Function gerFuntionEdge(long idEdge, long timestamp); } diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java new file mode 100644 index 0000000..6208c65 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -0,0 +1,7 @@ +package org.graphast.piecewise; + +public interface IManipulatorEngine { + + Function run(); + +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java new file mode 100644 index 0000000..e9120b2 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + +public class ManipulatorR implements IManipulatorEngine { + + @Override + public Function run() { + + return null; + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index c05c83f..45f1b0b 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -25,7 +25,7 @@ public void getConnectionTest() throws ClassNotFoundException, SQLException, IOE @Test public void getDataTest() throws PiecewiseException { - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); double[][] data = generatorFunctionPiecewise.getData(); int length = data.length; @@ -39,7 +39,7 @@ public void verifyPointsTest() throws IOException, PiecewiseException { RCode code = caller.getRCode(); caller.setRscriptExecutable(RSCRIPT); - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); double[][] data = generatorFunctionPiecewise.getData(); code.addDoubleMatrix("matrix", data); @@ -74,7 +74,7 @@ public void verifyPointsTest() throws IOException, PiecewiseException { @Test public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { - GeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewise(); + GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); System.out.println(value); diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java new file mode 100644 index 0000000..8fc3eb7 --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java @@ -0,0 +1,34 @@ +package org.graphast.piecewise; + +import java.util.Calendar; + +import org.graphast.graphgenerator.GraphGenerator; +import org.graphast.model.Graph; +import org.junit.Test; + +public class GeneratorFunctionRefactore { + + // Com um graph já existente, basta setar a função. + @Test + public void definedFuntionEdgeTest() { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IManipulatorEngine engineR = new ManipulatorR(); + GeneratorFunctionLoess generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + //Criar o graph com as funções dos edges + @Test + public void createGraphWithFuntionEdgeTest() { + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java new file mode 100644 index 0000000..a03a6c2 --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java @@ -0,0 +1,23 @@ +package org.graphast.piecewise; + +import org.graphast.config.Configuration; +import org.graphast.model.Graph; +import org.graphast.model.GraphImpl; +import org.graphast.model.NodeImpl; +import org.junit.Test; + +public class GeneratorGraphFunctionPiecewiseTest { + + + @Test + public void createGraphTest() throws PiecewiseException { + + Graph graph = new GraphImpl(Configuration.USER_HOME + "/graphast/test/examplepiecewise"); + + NodeImpl node1 = new NodeImpl(1l, 10d, 10d, "label node 1"); + graph.addNode(node1); + + NodeImpl node2 = new NodeImpl(1l, 10d, 10d, "label node 1"); + graph.addNode(node2); + } +} From 867bc19f8111fda0ce8f2864737b72c5604b8eba Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 18 Jan 2016 12:58:38 -0300 Subject: [PATCH 041/118] =?UTF-8?q?desacoplando=20implementa=C3=A7=C3=A3o?= =?UTF-8?q?=20de=20gerar=20a=20fun=C3=A7=C3=A3o=20para=20de=20uma=20aresta?= =?UTF-8?q?.=20Implementa=C3=A7=C3=A3o=20regressa=20linerar=20Loess.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/graphast/model/GraphImpl.java | 7 ++ .../piecewise/BuilderDatasourceStream.java | 10 ++ .../java/org/graphast/piecewise/Function.java | 5 +- .../piecewise/GeneratorFunctionLoess.java | 109 +----------------- .../piecewise/GeneratorFunctionMatrix.java | 13 ++- ...Piecewise.java => IGeneratorFunction.java} | 2 +- .../graphast/piecewise/ManipulatorJava.java | 9 ++ .../org/graphast/piecewise/ManipulatorR.java | 97 ++++++++++++++++ ...eneratorFunctionPiecewiseDatabaseTest.java | 2 +- .../piecewise/GeneratorFunctionRefactore.java | 52 ++++++++- 10 files changed, 192 insertions(+), 114 deletions(-) create mode 100644 core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java rename core/src/main/java/org/graphast/piecewise/{IGeneratorFunctionPiecewise.java => IGeneratorFunction.java} (66%) create mode 100644 core/src/main/java/org/graphast/piecewise/ManipulatorJava.java diff --git a/core/src/main/java/org/graphast/model/GraphImpl.java b/core/src/main/java/org/graphast/model/GraphImpl.java index 2076ddd..1d7e7ea 100644 --- a/core/src/main/java/org/graphast/model/GraphImpl.java +++ b/core/src/main/java/org/graphast/model/GraphImpl.java @@ -14,6 +14,7 @@ import org.graphast.geometry.PoI; import org.graphast.geometry.PoICategory; import org.graphast.geometry.Point; +import org.graphast.piecewise.Function; import org.graphast.util.DistanceUtils; import org.graphast.util.FileUtils; import org.slf4j.Logger; @@ -1466,5 +1467,11 @@ public void setDirectory(String directory) { this.absoluteDirectory = FileUtils.getAbsolutePath(directory); this.directory = directory; } + + @Override + public void setFuntionEdge(long edgeId, Function function) { + // TODO Auto-generated method stub + + } } diff --git a/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java new file mode 100644 index 0000000..725aa55 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + +public class BuilderDatasourceStream implements IBuilderDatasource { + + @Override + public double[][] getData() throws PiecewiseException { + return null; + } + +} diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java index 8b2a95d..bf15b67 100644 --- a/core/src/main/java/org/graphast/piecewise/Function.java +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -1,5 +1,8 @@ package org.graphast.piecewise; public class Function { - + + public double getValue(long timestamp) { + return 0; + } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index d16777e..445b61e 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -1,9 +1,7 @@ package org.graphast.piecewise; -import rcaller.RCaller; -import rcaller.RCode; -public class GeneratorFunctionLoess implements IGeneratorFunctionPiecewise { +public class GeneratorFunctionLoess implements IGeneratorFunction { private IManipulatorEngine engine; @@ -13,112 +11,13 @@ public GeneratorFunctionLoess() { public GeneratorFunctionLoess(IManipulatorEngine engine) { this.engine = engine; } - - private static final String RSCRIPT = "/usr/bin/Rscript"; - public double getValue(double timestamp) throws PiecewiseException { - - IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); - double[][] coleta = generatorFunctionPiecewise.getData(); - return getFuntionEdge(coleta, timestamp); + public double getValue(long timestamp) throws PiecewiseException { + return engine.run().getValue(timestamp); } @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - - engine.run(); - return null; - } - - private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addDoubleMatrix("matrix", data); - code.addRCode("dados.frame <- data.frame(matrix)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); - code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); - double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); - - PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); - PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); - - double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); - double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); - - - double yFinal = 0; - for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { - - PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); - PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); - double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); - double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); - - yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); - - coeficienteAngularAnterior = coeficienteAngularCurrent; - coeficienteLinearAnterior = coeficienteLinearCurrent; - } - - double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); - - PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], - pontosInflexao[pontosInflexao.length - 1]); - PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); - - double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); - double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - - yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); - - return yFinal; - } - - private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { - double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); - return gama; - } - - private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { - double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); - return alfa; - } - - class PontoGeometrico { - - double x; - double y; - - public PontoGeometrico(double x, double y) { - this.x = x; - this.y= y; - } - - public double getX() { - return x; - } - public void setX(double x) { - this.x = x; - } - public double getY() { - return y; - } - public void setY(double y) { - this.y = y; - } + return engine.run(); } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java index 64702a6..450f621 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -1,9 +1,16 @@ package org.graphast.piecewise; -public class GeneratorFunctionMatrix implements IGeneratorFunctionPiecewise { +public class GeneratorFunctionMatrix implements IGeneratorFunction { + + private IManipulatorEngine engine; + + public GeneratorFunctionMatrix(IManipulatorEngine engine) { + this.engine = engine; + } @Override - public double gerFuntionEdge(long idEdge, double timestamp) { - return 0; + public Function gerFuntionEdge(long idEdge, long timestamp) { + engine.run(); + return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java similarity index 66% rename from core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java rename to core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java index d6e5066..117a377 100644 --- a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java @@ -1,6 +1,6 @@ package org.graphast.piecewise; -public interface IGeneratorFunctionPiecewise { +public interface IGeneratorFunction { Function gerFuntionEdge(long idEdge, long timestamp); } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java new file mode 100644 index 0000000..d93fb2e --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java @@ -0,0 +1,9 @@ +package org.graphast.piecewise; + +public class ManipulatorJava implements IManipulatorEngine { + + @Override + public Function run() { + return null; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index e9120b2..9aecd9b 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -1,10 +1,107 @@ package org.graphast.piecewise; +import rcaller.RCaller; +import rcaller.RCode; + public class ManipulatorR implements IManipulatorEngine { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + private double[][] data; + public ManipulatorR(double[][] coleta) { + this.data = coleta; + } + @Override public Function run() { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + + PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); + PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + + double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); + double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); + + + double yFinal = 0; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { + + PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); + double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); + double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); + + yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + + coeficienteAngularAnterior = coeficienteAngularCurrent; + coeficienteLinearAnterior = coeficienteLinearCurrent; + } + + double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); + + PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], + pontosInflexao[pontosInflexao.length - 1]); + PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); + + double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); + double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); + + //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); return null; } + + private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { + double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); + return gama; + } + + private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { + double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); + return alfa; + } + + class PontoGeometrico { + + double x; + double y; + + public PontoGeometrico(double x, double y) { + this.x = x; + this.y= y; + } + + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } + } } diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 45f1b0b..6eb47be 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -75,7 +75,7 @@ public void verifyPointsTest() throws IOException, PiecewiseException { public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); - double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); + double value = generatorFunctionPiecewise.getValue(Long.valueOf("1201977368000")); System.out.println(value); Assert.assertNotNull(value); diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java index 8fc3eb7..3b3675a 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java @@ -9,8 +9,9 @@ public class GeneratorFunctionRefactore { // Com um graph já existente, basta setar a função. + // Loess sendo implementado em R @Test - public void definedFuntionEdgeTest() { + public void definedFuntionLoessREdgeTest() throws PiecewiseException { //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 Calendar calendar = Calendar.getInstance(); @@ -20,8 +21,53 @@ public void definedFuntionEdgeTest() { GraphGenerator graphGenerator = new GraphGenerator(); Graph graph = graphGenerator.generateExample(); - IManipulatorEngine engineR = new ManipulatorR(); - GeneratorFunctionLoess generatorFunction = new GeneratorFunctionLoess(engineR); + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] coleta = generatorFunctionPiecewise.getData(); + IManipulatorEngine engineR = new ManipulatorR(coleta); + + IGeneratorFunction generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + // Com um graph já existente, basta setar a função. + // Loess sendo implementado em Java + @Test + public void definedFuntionLoessJavaEdgeTest() { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IManipulatorEngine engineR = new ManipulatorJava(); + IGeneratorFunction generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + // Com um graph já existente, basta setar a função. + @Test + public void definedFuntionMatrixEdgeTest() throws PiecewiseException { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] coleta = generatorFunctionPiecewise.getData(); + IManipulatorEngine engineR = new ManipulatorR(coleta); + + IGeneratorFunction generatorFunction = new GeneratorFunctionMatrix(engineR); Function function = generatorFunction.gerFuntionEdge(1l, timestamp); graph.setFuntionEdge(1l, function); From fca678f0e857da594790d883f581ede005a2749c Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 4 Feb 2016 16:19:45 -0300 Subject: [PATCH 042/118] Benchmark Memory. --- core/pom.xml | 6 + .../rnn/CompareRNNSearchsMethodsAnalysis.java | 41 +++++-- .../org/graphast/util/BenchmarkMemory.java | 104 +++++++++++++++++- 3 files changed, 140 insertions(+), 11 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 403c59d..be759cd 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -90,6 +90,12 @@ postgis-jdbc 1.3.3 + + + org.apache.commons + commons-lang3 + 3.4 + diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 34af678..33a5ee2 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -12,6 +12,7 @@ import org.graphast.model.GraphBounds; import org.graphast.model.Node; import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.BenchmarkMemory; import org.graphast.util.DateUtils; import org.graphast.util.NumberUtils; @@ -23,9 +24,9 @@ public class CompareRNNSearchsMethodsAnalysis { public static void main(String[] args) throws IOException { runAnalysis("view_exp_1k", 10); - /*runAnalysis("view_exp_10k", Integer.parseInt(args[0])); + runAnalysis("view_exp_10k", Integer.parseInt(args[0])); runAnalysis("view_exp_50k", Integer.parseInt(args[0])); - runAnalysis("view_exp_100k", Integer.parseInt(args[0]));*/ + runAnalysis("view_exp_100k", Integer.parseInt(args[0])); } public static void runAnalysis(String tableName, int testTimes) throws IOException { @@ -61,17 +62,39 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { try { - Runtime.getRuntime().gc(); - long numberUseMemoryInit = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + //List listIdProcess = BenchmarkMemory.listIdProcess(); + long numberUseMemoryInit = BenchmarkMemory.getUsedMemory(); long startTime = System.nanoTime(); + long ioProcessReadInit = 0; + long ioProcessWriteInit = 0; + + /*for (Integer pid : listIdProcess) { + ioProcessReadInit = ioProcessReadInit + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.READ); + ioProcessWriteInit = ioProcessWriteInit + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.WRITE); + }*/ + NearestNeighbor solution = null; solution = rnn.search(customer, timeout, timestamp); + + long ioProcessReadEnd = 0; + long ioProcessWriteEnd = 0; + + /*for (Integer pid : listIdProcess) { + ioProcessReadEnd = ioProcessReadEnd + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.READ); + ioProcessWriteEnd = ioProcessWriteEnd + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.WRITE); + }*/ + long endTime = System.nanoTime(); - long numberUseMemoryFinal = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + long numberUseMemoryFinal = BenchmarkMemory.getUsedMemory(); long time = endTime - startTime; long numerUseMemory = (numberUseMemoryFinal - numberUseMemoryInit) / 1024; - System.out.println(" --------- >"+numerUseMemory); + + long read = ioProcessReadEnd - ioProcessReadInit; + long write = ioProcessWriteEnd - ioProcessWriteInit; + + System.out.println(" READ "+read); + System.out.println(" WRITE "+write); Long solutionId = null; Double travelTime = null; @@ -103,13 +126,12 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, gidVisited = gidVisited + "-" + nodeVisited.getLabel(); } - String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, + String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, poiCoordinate, time, solutionId, externalId, travelTime, - nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited, numberVisitedNodes) + "\n"; + nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited, numberVisitedNodes, numerUseMemory) + "\n"; System.out.println(currentLine); - fileCsv.write(currentLine); } } catch(PathNotFoundException e) { @@ -117,7 +139,6 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, } } - private static Node getRandomCustomerInGraph(GraphBounds graph) { Node node; double[] bounds = new double[]{-3.710467, -38.591078, -3.802376, -38.465530}; diff --git a/core/src/main/java/org/graphast/util/BenchmarkMemory.java b/core/src/main/java/org/graphast/util/BenchmarkMemory.java index 5bf80c6..2380ddf 100644 --- a/core/src/main/java/org/graphast/util/BenchmarkMemory.java +++ b/core/src/main/java/org/graphast/util/BenchmarkMemory.java @@ -5,7 +5,9 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -28,6 +30,90 @@ public static Map quantityMemory() throws IOException { return runPatter(input); } + + //cat /proc/10925/io + + public static Long ioProcess(int pid, PROCESS process) throws IOException { + + final String COMMAND_PROCESS = "cat /proc/"+pid+"/io \n"; + final String COMMAND_EXIT = "exit\n"; + + Process telnetProcess = createProcessBuilder(); + BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream())); + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream())); + runCommand(COMMAND_PROCESS, output); + runCommand(COMMAND_EXIT, output); + + Map runPatterIO = runPatterIO(input); + Long read_byte = runPatterIO.get("rchar"); + Long write_byte = runPatterIO.get("wchar"); + + if(read_byte != null && write_byte != null) { + if(process.equals(PROCESS.READ)) { + return read_byte; + } else { + return write_byte; + } + } + return 0l; + } + + private static Map runPatterIO(BufferedReader input) throws NumberFormatException, IOException { + + Map listMemory = new HashMap(); + String line; + + while ((line = input.readLine()) != null) { + + String[] split = line.split(PATTERN_SEPARATOR); + String keyIOProcess = split[0]; + String x2 = split[1]; + Matcher matcher = Pattern.compile(PATTERN_NUMBER).matcher(x2); + Long value = 0l; + + while (matcher.find()) { + value = Long.valueOf(matcher.group()); + } + + listMemory.put(keyIOProcess, value); + } + + return listMemory; + } + + public enum PROCESS { + READ, + WRITE + } + + public static List listIdProcess() throws IOException { + + final String COMMAND_PROCESS = "ps -ax | grep java\n"; + final String COMMAND_EXIT = "exit\n"; + + Process telnetProcess = createProcessBuilder(); + BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream())); + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream())); + runCommand(COMMAND_PROCESS, output); + runCommand(COMMAND_EXIT, output); + + return runPatterProcess(input); + } + + private static List runPatterProcess(BufferedReader input) throws IOException { + + List listProcess = new ArrayList(); + String line; + + while ((line = input.readLine()) != null) { + String[] PID = line.split(" "); + if(org.apache.commons.lang3.StringUtils.isNumeric(PID[0])) { + listProcess.add(Integer.valueOf(PID[0])); + } + } + + return listProcess; + } private static Map runPatter(BufferedReader input) throws IOException { @@ -35,7 +121,7 @@ private static Map runPatter(BufferedReader input) throws IOExcept String line; while ((line = input.readLine()) != null) { - + String[] split = line.split(PATTERN_SEPARATOR); String keyMemory = split[0]; String x2 = split[1]; @@ -64,4 +150,20 @@ private static void runCommand(final String command, BufferedWriter output) thro output.write(command); output.flush(); } + + public static long getUsedMemory() { + + Runtime.getRuntime().gc(); + long numberUseMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + return numberUseMemory; + } + + public static void main(String[] args) throws IOException { + + List listIdProcess = BenchmarkMemory.listIdProcess(); + for (Integer integer : listIdProcess) { + System.out.println(BenchmarkMemory.ioProcess(integer, PROCESS.READ)); + System.out.println(BenchmarkMemory.ioProcess(integer, PROCESS.WRITE)); + } + } } From 70568cbe3ea025a001005b7e09be4df41f855cb1 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:57:21 -0300 Subject: [PATCH 043/118] Parser json to DB com coordenadas configuradas para uma determinada Edge. --- .../piecewise/JsonTimeTravelToDB.java | 64 +++++++++++++++++++ .../org/graphast/piecewise/ManipulatorR.java | 4 +- core/src/main/resources/piecewise.sql | 15 +++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java create mode 100644 core/src/main/resources/piecewise.sql diff --git a/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java b/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java new file mode 100644 index 0000000..b98d843 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java @@ -0,0 +1,64 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.graphast.query.postgis.QueryPostgis; +import org.graphast.util.ConnectionJDBC; +import org.json.JSONArray; +import org.json.JSONObject; + +public class JsonTimeTravelToDB { + + private final String FILE_JSON_TIME_TRAVEL = "time_travel.json"; + + private String FIELD_JSON_COORDINATES = "coordinates"; + private String FIELD_JSON_TIME = "entry_time"; + private String FIELD_JSON_TOTAL_TRAVEL = "total_time"; + + public void jsonToDB() throws PiecewiseException, SQLException, ClassNotFoundException, IOException { + + JSONObject jsonObject = new JSONObject(FILE_JSON_TIME_TRAVEL); + + JSONArray coordinates = jsonObject.getJSONArray(FIELD_JSON_COORDINATES); + double timeDay = jsonObject.getDouble(FIELD_JSON_TIME); + double totalTime = jsonObject.getDouble(FIELD_JSON_TOTAL_TRAVEL); + + Object latitude = coordinates.get(0); + Object longitude = coordinates.get(1); + + Connection connectionJDBC = null; + try { + connectionJDBC = ConnectionJDBC.getConnection(); + } catch (ClassNotFoundException | SQLException | IOException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); + } + + PreparedStatement statement = connectionJDBC.prepareStatement(QueryPostgis.QUERY_CLOSER_LINESTRING); + statement.setString(0, latitude.toString()); + statement.setString(0, longitude.toString()); + + ResultSet resultSet = statement.executeQuery(QueryPostgis.QUERY_CLOSER_LINESTRING); + + while (resultSet.next()) { + + int idEdge = resultSet.getInt(0); + persisteValue(idEdge, timeDay, totalTime); + } + } + + private void persisteValue(int idEdge, double timeDay, double totalTime) throws SQLException, ClassNotFoundException, IOException { + + String insertTableSQL = QueryPostgis.INSERT_PIECEWISE; + + PreparedStatement preparedStatement = ConnectionJDBC.getConnection().prepareStatement(insertTableSQL); + preparedStatement.setInt(1, idEdge); + preparedStatement.setDouble(2, timeDay); + preparedStatement.setDouble(3, totalTime); + preparedStatement .executeUpdate(); + + } +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 9aecd9b..8811bb2 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -44,6 +44,8 @@ public Function run() { double yFinal = 0; + yFinal = coeficienteAngularAnterior; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); @@ -67,7 +69,7 @@ public Function run() { double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); - + return null; } diff --git a/core/src/main/resources/piecewise.sql b/core/src/main/resources/piecewise.sql new file mode 100644 index 0000000..abf1a20 --- /dev/null +++ b/core/src/main/resources/piecewise.sql @@ -0,0 +1,15 @@ +-- Table: public.tester + +-- DROP TABLE public.tester; + +CREATE TABLE public.piecewise +( + edgeId double precision, + timeDay double precision, + totalTime double precision +) +WITH ( + OIDS=FALSE +); +ALTER TABLE public.tester + OWNER TO postgres; \ No newline at end of file From 218c576cbf1b79c3c740228249205ee80e411b47 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:58:15 -0300 Subject: [PATCH 044/118] Insert values para piecewise table. --- core/src/main/java/org/graphast/query/postgis/QueryPostgis.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java index 9271efa..7cba9a3 100644 --- a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -3,5 +3,7 @@ public interface QueryPostgis { String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; + String QUERY_CLOSER_LINESTRING = null; + String INSERT_PIECEWISE = "INSERT INTO public.tester(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; } From 17c86917e47394ebc72f69cbd69d5da2db3f292b Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:58:52 -0300 Subject: [PATCH 045/118] Insert into para piecewise table. --- core/src/main/java/org/graphast/query/postgis/QueryPostgis.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java index 7cba9a3..2ae067b 100644 --- a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -4,6 +4,6 @@ public interface QueryPostgis { String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; String QUERY_CLOSER_LINESTRING = null; - String INSERT_PIECEWISE = "INSERT INTO public.tester(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; + String INSERT_PIECEWISE = "INSERT INTO public.piecewise(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; } From ec138410c43c7e8540543ba9c41650c6127ca12f Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:59:36 -0300 Subject: [PATCH 046/118] dependecy to json parser. --- core/pom.xml | 100 +++++++++++++++++++++------------------------------ 1 file changed, 41 insertions(+), 59 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 9f3d3dc..a5837ee 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -10,48 +10,38 @@ https://github.com/ARiDa/graphast - - maven-repo - http://arida.github.io/maven-repo/ - - + + maven-repo + http://arida.github.io/maven-repo/ + + - + - - GNU Lesser General Public License (LGPL), Version 2.1 - http://www.fsf.org/licensing/licenses/lgpl.txt - repo - - + + GNU Lesser General Public License (LGPL), Version 2.1 + http://www.fsf.org/licensing/licenses/lgpl.txt + repo + + - scm:git:git://github.com/ARiDa/graphast.git - https://github.com/ARiDa/graphast.git - https://github.com/ARiDa/graphast.git + scm:git:git://github.com/ARiDa/graphast.git + https://github.com/ARiDa/graphast.git + https://github.com/ARiDa/graphast.git graphast Graphast - + - - + + UTF-8 @@ -85,25 +75,28 @@ osmpoispbf 1.1 - + - com.github.davidmoten - rtree - 0.6.9 + com.github.davidmoten + rtree + 0.6.9 - - - + + + postgresql postgresql 9.1-901-1.jdbc4 - + + + org.json + json + 20160212 + + + @@ -151,21 +144,10 @@ - - + + From 8e2dcdbc72c2e06faaaec7f4bc212aa05bf65183 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 28 May 2016 12:08:36 -0300 Subject: [PATCH 047/118] =?UTF-8?q?Atualiza=C3=A7=C3=A3o=20da=20fun=C3=A7?= =?UTF-8?q?=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/graphast/piecewise/Function.java | 29 ++++++++++++++++++- .../piecewise/GeneratorFunctionLoess.java | 6 +--- .../piecewise/GeneratorFunctionMatrix.java | 2 +- .../piecewise/IManipulatorEngine.java | 3 +- .../graphast/piecewise/ManipulatorJava.java | 2 +- .../org/graphast/piecewise/ManipulatorR.java | 10 ++++--- ...eneratorFunctionPiecewiseDatabaseTest.java | 2 +- .../piecewise/RCallerEnvironmentsTest.java | 4 +-- 8 files changed, 41 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java index bf15b67..04c6785 100644 --- a/core/src/main/java/org/graphast/piecewise/Function.java +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -2,7 +2,34 @@ public class Function { - public double getValue(long timestamp) { + private long y; + + private long x; + + public Function(long y, long x) { + super(); + this.y = y; + this.x = x; + } + + public long getY() { + return y; + } + + public long getX() { + return x; + } + + public void setY(long y) { + this.y = y; + } + + public void setX(long x) { + this.x = x; + } + + + public long getValue(long timestamp) { return 0; } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index 445b61e..10be79b 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -12,12 +12,8 @@ public GeneratorFunctionLoess(IManipulatorEngine engine) { this.engine = engine; } - public double getValue(long timestamp) throws PiecewiseException { - return engine.run().getValue(timestamp); - } - @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - return engine.run(); + return engine.run(timestamp); } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java index 450f621..51c6fee 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -10,7 +10,7 @@ public GeneratorFunctionMatrix(IManipulatorEngine engine) { @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - engine.run(); + engine.run(timestamp); return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java index 6208c65..b9b1bf4 100644 --- a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -2,6 +2,5 @@ public interface IManipulatorEngine { - Function run(); - + Function run(long x); } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java index d93fb2e..69ee8d0 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java @@ -3,7 +3,7 @@ public class ManipulatorJava implements IManipulatorEngine { @Override - public Function run() { + public Function run(long x) { return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 8811bb2..66c25e4 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -13,7 +13,7 @@ public ManipulatorR(double[][] coleta) { } @Override - public Function run() { + public Function run(long x) { RCaller caller = new RCaller(); RCode code = caller.getRCode(); @@ -44,16 +44,17 @@ public Function run() { double yFinal = 0; + PontoGeometrico pontoGeo1 = null; yFinal = coeficienteAngularAnterior; for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { - PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); - yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + yFinal = yFinal + (pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); coeficienteAngularAnterior = coeficienteAngularCurrent; coeficienteLinearAnterior = coeficienteLinearCurrent; @@ -68,7 +69,8 @@ public Function run() { double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); + + yFinal = yFinal + (pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearFinal)) + coeficienteAngularFinal * x; return null; } diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 6eb47be..0e3bfd2 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -75,7 +75,7 @@ public void verifyPointsTest() throws IOException, PiecewiseException { public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); - double value = generatorFunctionPiecewise.getValue(Long.valueOf("1201977368000")); + Function value = generatorFunctionPiecewise.gerFuntionEdge(1l, Long.valueOf("1201977368000").longValue()); System.out.println(value); Assert.assertNotNull(value); diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java index e458b6a..7639a61 100644 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -10,8 +10,8 @@ public class RCallerEnvironmentsTest { - private static final String RSCRIPT = "/usr/bin/Rscript"; - + private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript"; + @Test public void createRscriptTest() { From bed2d54a14e2fc8c31eeb6073877c99fb7dad2f1 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 28 May 2016 12:11:39 -0300 Subject: [PATCH 048/118] =?UTF-8?q?Retirado=20coment=C3=A1rios.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/db.properties | 16 ++++++++-------- .../rnn/CompareRNNSearchsMethodsAnalysis.java | 19 ++++--------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/core/db.properties b/core/db.properties index 201ec27..982641b 100644 --- a/core/db.properties +++ b/core/db.properties @@ -1,11 +1,11 @@ # remoto - nuvem arida -#driver=org.postgresql.Driver -#host=jdbc:postgresql://arida1.mooo.com:8080/fortaleza -#user=postgres -#password=aridapostgres12 - -#local driver=org.postgresql.Driver -host=jdbc:postgresql://localhost:5432/fortaleza_novo +host=jdbc:postgresql://177.130.199.157:8080/fortaleza user=postgres -password=postgres \ No newline at end of file +password=aridapostgres12 + +#local +#driver=org.postgresql.Driver +#host=jdbc:postgresql://localhost:5432/fortaleza_novo +#user=postgres +#password=postgres \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 33a5ee2..644a7b6 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -24,9 +24,9 @@ public class CompareRNNSearchsMethodsAnalysis { public static void main(String[] args) throws IOException { runAnalysis("view_exp_1k", 10); - runAnalysis("view_exp_10k", Integer.parseInt(args[0])); - runAnalysis("view_exp_50k", Integer.parseInt(args[0])); - runAnalysis("view_exp_100k", Integer.parseInt(args[0])); + //runAnalysis("view_exp_10k", Integer.parseInt(args[0])); + //runAnalysis("view_exp_50k", Integer.parseInt(args[0])); + //runAnalysis("view_exp_100k", Integer.parseInt(args[0])); } public static void runAnalysis(String tableName, int testTimes) throws IOException { @@ -62,28 +62,17 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { try { - //List listIdProcess = BenchmarkMemory.listIdProcess(); long numberUseMemoryInit = BenchmarkMemory.getUsedMemory(); long startTime = System.nanoTime(); long ioProcessReadInit = 0; long ioProcessWriteInit = 0; - - /*for (Integer pid : listIdProcess) { - ioProcessReadInit = ioProcessReadInit + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.READ); - ioProcessWriteInit = ioProcessWriteInit + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.WRITE); - }*/ - + NearestNeighbor solution = null; solution = rnn.search(customer, timeout, timestamp); long ioProcessReadEnd = 0; long ioProcessWriteEnd = 0; - /*for (Integer pid : listIdProcess) { - ioProcessReadEnd = ioProcessReadEnd + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.READ); - ioProcessWriteEnd = ioProcessWriteEnd + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.WRITE); - }*/ - long endTime = System.nanoTime(); long numberUseMemoryFinal = BenchmarkMemory.getUsedMemory(); From 041635bee8d43e41b14d342615ec028d90084944 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 31 May 2016 15:33:45 -0300 Subject: [PATCH 049/118] =?UTF-8?q?Altera=C3=A7=C3=A3o=20da=20lib=20do=20R?= =?UTF-8?q?caller=20para=20utilizar=20maven.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/pom.xml | 8 ++++++-- .../main/java/org/graphast/piecewise/ManipulatorR.java | 4 ++-- .../piecewise/GeneratorFunctionPiecewiseDatabaseTest.java | 4 ++-- .../org/graphast/piecewise/RCallerEnvironmentsTest.java | 7 ++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index a5837ee..74cb617 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -81,8 +81,12 @@ rtree 0.6.9 - + + + com.github.jbytecode + RCaller + 2.8 + postgresql diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 66c25e4..216172c 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -1,7 +1,7 @@ package org.graphast.piecewise; -import rcaller.RCaller; -import rcaller.RCode; +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; public class ManipulatorR implements IManipulatorEngine { diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 0e3bfd2..8e2e4a8 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -8,8 +8,8 @@ import org.junit.Assert; import org.junit.Test; -import rcaller.RCaller; -import rcaller.RCode; +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; public class GeneratorFunctionPiecewiseDatabaseTest { diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java index 7639a61..c60791b 100644 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -5,12 +5,12 @@ import org.junit.Assert; import org.junit.Test; -import rcaller.RCaller; -import rcaller.RCode; +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; public class RCallerEnvironmentsTest { - private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript"; + private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript.exe"; @Test public void createRscriptTest() { @@ -24,6 +24,7 @@ public void createRscriptTest() { public void baseLinearOutXMLTest() throws IOException { RCaller caller = new RCaller(); + caller.setRscriptExecutable(RSCRIPT); RCode code = caller.getRCode(); code.addRCode("set.seed(123)"); From 34ed185b17a4fc99d1e689a65814578ebbea9dd6 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:07:49 -0200 Subject: [PATCH 050/118] =?UTF-8?q?Adicionando=20artifato=20rcaller,=20par?= =?UTF-8?q?a=20manipular=20as=20fun=C3=A7=C3=B5es=20do=20R.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/pom.xml | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index eebefb1..0af0fdb 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -14,6 +14,20 @@ maven-repo http://arida.github.io/maven-repo/ + @@ -77,10 +91,21 @@ rtree 0.7.1 + + + + postgresql + postgresql + 9.1-901-1.jdbc4 + + - - From 3979ce1893bed38f1c4bcc32e4604dbecf3fa658 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:09:42 -0200 Subject: [PATCH 051/118] =?UTF-8?q?Criando=20estutura=20de=20classes=20par?= =?UTF-8?q?a=20manipular=20dos=20entradas=20para=20a=20gera=C3=A7=C3=A3o?= =?UTF-8?q?=20da=20fun=C3=A7=C3=A3o=20das=20arestas.=20Concluido=20Entrada?= =?UTF-8?q?=20Database.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GeneratorFunctionPiecewiseCsv.java | 14 +++++ .../GeneratorFunctionPiecewiseDatabase.java | 59 +++++++++++++++++++ .../IGeneratorFunctionPiecewise.java | 7 +++ .../piecewise/PiecewiseException.java | 10 ++++ 4 files changed, 90 insertions(+) create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java create mode 100644 core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java create mode 100644 core/src/main/java/org/graphast/piecewise/PiecewiseException.java diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java new file mode 100644 index 0000000..b1d9fb0 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java @@ -0,0 +1,14 @@ +package org.graphast.piecewise; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +public class GeneratorFunctionPiecewiseCsv implements IGeneratorFunctionPiecewise { + + @Override + public double[][] getData() { + + return null; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java new file mode 100644 index 0000000..a74e1b2 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java @@ -0,0 +1,59 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.graphast.query.postgis.QueryPostgis; +import org.graphast.util.ConnectionJDBC; + +public class GeneratorFunctionPiecewiseDatabase implements IGeneratorFunctionPiecewise { + + private int FIELD_DATE_TIME = 1; + private int FIELD_DURACAO = 2; + + @Override + public double[][] getData() throws PiecewiseException { + + Connection connectionJDBC = null; + try { + connectionJDBC = ConnectionJDBC.getConnection(); + } catch (ClassNotFoundException | SQLException | IOException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); + } + + ResultSet resultSet = null; + double[][] matrixResult; + try { + Statement statement = connectionJDBC.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); + resultSet = statement.executeQuery(QueryPostgis.QUERY_ALL_DATE_TIME_WITH_DURATE); + + resultSet.last(); + int size = resultSet.getRow(); + resultSet.beforeFirst(); + + matrixResult = new double[size][2]; + + int i = 0; + while (resultSet.next()) { + + Double dateTime = resultSet.getDouble(FIELD_DATE_TIME); + Double duracao = resultSet.getDouble(FIELD_DURACAO); + double[] result = new double[2]; + result[0] = duracao; + result[1] = dateTime; + + matrixResult[i] = result; + i++; + } + + } catch (SQLException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu ao pegar as informações no DB."); + } + + + return matrixResult; + } +} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java new file mode 100644 index 0000000..14dcad7 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java @@ -0,0 +1,7 @@ +package org.graphast.piecewise; + + +public interface IGeneratorFunctionPiecewise { + + public double[][] getData() throws PiecewiseException; +} diff --git a/core/src/main/java/org/graphast/piecewise/PiecewiseException.java b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java new file mode 100644 index 0000000..28b8cb3 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + +public class PiecewiseException extends Exception { + + private static final long serialVersionUID = 1L; + + public PiecewiseException(String error) { + super(error); + } +} From cdc71ad640ca86a4c7f90ad04d3aeee54a7a12bf Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:10:26 -0200 Subject: [PATCH 052/118] =?UTF-8?q?Manipula=C3=A7=C3=A3o=20como=20Banco=20?= =?UTF-8?q?de=20Dados.=20Consulta,=20aquivo=20de=20configura=C3=A7=C3=A3o?= =?UTF-8?q?=20e=20classe=20de=20conex=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/db.properties | 11 ++++++ .../graphast/query/postgis/QueryPostgis.java | 7 ++++ .../org/graphast/util/ConnectionJDBC.java | 38 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 core/db.properties create mode 100644 core/src/main/java/org/graphast/query/postgis/QueryPostgis.java create mode 100644 core/src/main/java/org/graphast/util/ConnectionJDBC.java diff --git a/core/db.properties b/core/db.properties new file mode 100644 index 0000000..49e2234 --- /dev/null +++ b/core/db.properties @@ -0,0 +1,11 @@ +# remoto - nuvem arida +#driver=org.postgresql.Driver +#host=jdbc:postgresql://arida1.mooo.com:8080/fortaleza +#user=postgres +#password=aridapostgres12 + +#local +driver=org.postgresql.Driver +host=jdbc:postgresql://localhost:5432/piecewise +user=postgres +password=postgres \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java new file mode 100644 index 0000000..9271efa --- /dev/null +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -0,0 +1,7 @@ +package org.graphast.query.postgis; + +public interface QueryPostgis { + + String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; + +} diff --git a/core/src/main/java/org/graphast/util/ConnectionJDBC.java b/core/src/main/java/org/graphast/util/ConnectionJDBC.java new file mode 100644 index 0000000..86bb0ce --- /dev/null +++ b/core/src/main/java/org/graphast/util/ConnectionJDBC.java @@ -0,0 +1,38 @@ +package org.graphast.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +public class ConnectionJDBC { + + private static Connection connection = null; + private static final String FILE_NAME_PROPERTIES = "db.properties"; + private static final String STR_DRIVER = "driver"; + private static final String STR_HOST = "host"; + private static final String STR_USER = "user"; + private static final String STR_PASS = "password"; + + + public static Connection getConnection() throws ClassNotFoundException, + SQLException, IOException { + + if (connection == null) { + + Properties properties = new Properties(); + properties.load(new FileInputStream(new File(FILE_NAME_PROPERTIES))); + Class.forName(properties.getProperty(STR_DRIVER)); + String host = properties.getProperty(STR_HOST); + String user = properties.getProperty(STR_USER); + String password = properties.getProperty(STR_PASS); + + connection = DriverManager.getConnection(host, user, password); + } + + return connection; + } +} From 3a630c3e5b5bf254126a54a26209ccfb3ee51c38 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:10:46 -0200 Subject: [PATCH 053/118] =?UTF-8?q?Gerado=20da=20fun=C3=A7=C3=A3o=20para?= =?UTF-8?q?=20cada=20aresta=20do=20grafo.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/GeneratorFunctionPiecewise.java | 57 +++++++++++++++++++ ...eneratorFunctionPiecewiseDatabaseTest.java | 29 ++++++++++ 2 files changed, 86 insertions(+) create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java new file mode 100644 index 0000000..cb274bb --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java @@ -0,0 +1,57 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.util.Date; +import java.util.List; + +import rcaller.RCaller; +import rcaller.RCode; + +public class GeneratorFunctionPiecewise { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + + public double getValue(Date timeDay) { + + IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + + double[][] data = null; + try { + data = generatorFunctionPiecewise.getData(); + } catch (PiecewiseException e) { + e.printStackTrace(); + } + +// List executeLossFuntion = executeLossFuntion(data); + + return 0; + } + + private List executeLossFuntion(double[][] data ) throws PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addDoubleMatrix("dados.frame", data); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + try { + System.out.println(caller.getParser().getXMLFileAsString()); + } catch (IOException e) { + throw new PiecewiseException("[ERRO] Um erro quando estava sendo executada a função LOESS do R."); + } + + return null; + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java new file mode 100644 index 0000000..3d5722d --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -0,0 +1,29 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; + +import org.graphast.util.ConnectionJDBC; +import org.junit.Assert; +import org.junit.Test; + +public class GeneratorFunctionPiecewiseDatabaseTest { + + @Test + public void getConnectionTest() throws ClassNotFoundException, SQLException, IOException { + + Connection connection = ConnectionJDBC.getConnection(); + Assert.assertNotNull(connection); + } + + @Test + public void getDataTest() throws PiecewiseException { + + IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + double[][] data = generatorFunctionPiecewise.getData(); + int length = data.length; + + Assert.assertEquals(28, length); + } +} From bce4f3dd47161523c2f59253f9d7a8526472c0c8 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 13:11:21 -0200 Subject: [PATCH 054/118] =?UTF-8?q?Classe=20com=20testes=20unit=C3=A1rios?= =?UTF-8?q?=20para=20testes=20da=20biblioteca=20RCaller.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/RCallerEnvironmentsTest.java | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java new file mode 100644 index 0000000..71b28ad --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -0,0 +1,146 @@ +package org.graphast.piecewise; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +import rcaller.RCaller; +import rcaller.RCode; + +public class RCallerEnvironmentsTest { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + + @Test + public void createRscriptTest() { + + RCaller rcaller = new RCaller(); + rcaller.setRscriptExecutable(RSCRIPT); + Assert.assertNotNull(rcaller.getRCode()); + } + + @Test + public void baseLinearOutXMLTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("ols<-lm(y~x)"); + + caller.setRCode(code); + caller.runAndReturnResult("ols"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + + @Test + public void loessTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + + caller.setRCode(code); + caller.runAndReturnResult("dados.loess"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void loessWithRandomDataTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void loessWithSynteticDataTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void rcallerSetParameterTest() throws IOException, PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + double[][] d = generatorFunctionPiecewise.getData(); + + code.addDoubleMatrix("d", d); + code.addRCode("dados.frame <- data.frame(d)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("dados.loess"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + + } + + + + +} From 26695f95360ea7b929b03f90394c5cc3e4cbef22 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 14:36:53 -0200 Subject: [PATCH 055/118] =?UTF-8?q?Criada=20primeira=20fun=C3=A7=C3=A3o=20?= =?UTF-8?q?ap=C3=B3s=20o=20uso=20do=20loess?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/GeneratorFunctionPiecewise.java | 86 ++++++++++++------- ...eneratorFunctionPiecewiseDatabaseTest.java | 54 ++++++++++++ 2 files changed, 110 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java index cb274bb..829c5cb 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java @@ -1,9 +1,5 @@ package org.graphast.piecewise; -import java.io.IOException; -import java.util.Date; -import java.util.List; - import rcaller.RCaller; import rcaller.RCode; @@ -11,47 +7,77 @@ public class GeneratorFunctionPiecewise { private static final String RSCRIPT = "/usr/bin/Rscript"; - public double getValue(Date timeDay) { + public double getValue(double timestamp) throws PiecewiseException { IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - - double[][] data = null; - try { - data = generatorFunctionPiecewise.getData(); - } catch (PiecewiseException e) { - e.printStackTrace(); - } - -// List executeLossFuntion = executeLossFuntion(data); - - return 0; + double[][] coleta = generatorFunctionPiecewise.getData(); + return getFuntionEdge(coleta, timestamp); } - private List executeLossFuntion(double[][] data ) throws PiecewiseException { + private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { RCaller caller = new RCaller(); RCode code = caller.getRCode(); caller.setRscriptExecutable(RSCRIPT); - code.addDoubleMatrix("dados.frame", data); - code.addRCode("dados.loess <- loess(y ~ x)"); - code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); code.addRCode("y.predict <- predict(dados.loess, xl)"); code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); - code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); - + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + caller.setRCode(code); caller.runAndReturnResult("allObjects"); - try { - System.out.println(caller.getParser().getXMLFileAsString()); - } catch (IOException e) { - throw new PiecewiseException("[ERRO] Um erro quando estava sendo executada a função LOESS do R."); + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + + PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); + PontoGeometrico pontoFinalGeo = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + + double coeficienteAngular = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo); + double coeficienteLinear = getCoeficienteLinear(pontoInicialGeo, coeficienteAngular); + double y = coeficienteAngular * timestamp + coeficienteLinear; + + return y; + } + + private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { + double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); + return gama; + } + + private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { + double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); + return alfa; + } + + class PontoGeometrico { + + double x; + double y; + + public PontoGeometrico(double x, double y) { + this.x = x; + this.y= y; } - return null; + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } } } diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 3d5722d..c05c83f 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -8,7 +8,12 @@ import org.junit.Assert; import org.junit.Test; +import rcaller.RCaller; +import rcaller.RCode; + public class GeneratorFunctionPiecewiseDatabaseTest { + + private static final String RSCRIPT = "/usr/bin/Rscript"; @Test public void getConnectionTest() throws ClassNotFoundException, SQLException, IOException { @@ -26,4 +31,53 @@ public void getDataTest() throws PiecewiseException { Assert.assertEquals(28, length); } + + @Test + public void verifyPointsTest() throws IOException, PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); + double[][] data = generatorFunctionPiecewise.getData(); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + System.out.println(caller.getParser().getXMLFileAsString().toString()); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + Assert.assertEquals(Double.valueOf("1201977368000"), Double.valueOf(pontoInicial[0])); + Assert.assertEquals(Double.valueOf("295522763826171"), Double.valueOf(pontoInicial[1])); + + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + int length = pontosInflexao.length; + Assert.assertEquals(6, length); + + Assert.assertEquals(Double.valueOf("1202063763696"), Double.valueOf(pontosInflexao[0])); + Assert.assertEquals(Double.valueOf("55328186.6718641"), Double.valueOf(pontosInflexao[length/2])); + } + + @Test + public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { + + GeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewise(); + double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); + + System.out.println(value); + Assert.assertNotNull(value); + } } From ca60561b0bdcf81493c9d2708a76893041cbfb38 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 14:37:17 -0200 Subject: [PATCH 056/118] =?UTF-8?q?Removido=20teste=20unit=C3=A1rio=20para?= =?UTF-8?q?=20outra=20classe.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/RCallerEnvironmentsTest.java | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java index 71b28ad..e458b6a 100644 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -110,37 +110,4 @@ public void loessWithSynteticDataTest() throws IOException { System.out.println(caller.getParser().getXMLFileAsString()); Assert.assertNotNull(caller.getParser().getXMLFileAsString()); } - - @Test - public void rcallerSetParameterTest() throws IOException, PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - double[][] d = generatorFunctionPiecewise.getData(); - - code.addDoubleMatrix("d", d); - code.addRCode("dados.frame <- data.frame(d)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); - code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); - - caller.setRCode(code); - caller.runAndReturnResult("dados.loess"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - - } - - - - } From fb5d3f701903b6013c92af36cc5ea6fc13f62263 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 26 Nov 2015 15:27:18 -0200 Subject: [PATCH 057/118] =?UTF-8?q?Finalizada=20fun=C3=A7=C3=A3o=20para=20?= =?UTF-8?q?o=20c=C3=A1lculo=20do=20tempo=20gasto=20para=20percorrer=20cada?= =?UTF-8?q?=20aresta=20de=20um=20grafo=20dependente=20do=20tempo.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../piecewise/GeneratorFunctionPiecewise.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java index 829c5cb..4ba69b8 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java @@ -38,13 +38,38 @@ private double getFuntionEdge(double[][] data, double timestamp) throws Piecewis double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); - PontoGeometrico pontoFinalGeo = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); - double coeficienteAngular = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo); - double coeficienteLinear = getCoeficienteLinear(pontoInicialGeo, coeficienteAngular); - double y = coeficienteAngular * timestamp + coeficienteLinear; + double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); + double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); + - return y; + double yFinal = 0; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { + + PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); + double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); + double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); + + yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + + coeficienteAngularAnterior = coeficienteAngularCurrent; + coeficienteLinearAnterior = coeficienteLinearCurrent; + } + + double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); + + PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], + pontosInflexao[pontosInflexao.length - 1]); + PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); + + double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); + double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); + + yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); + + return yFinal; } private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { From c13140710dee58359d619d3f2f48077beb11e05e Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 27 Nov 2015 09:12:38 -0200 Subject: [PATCH 058/118] =?UTF-8?q?Adicionado=20bin=C3=A1rio=20do=20R.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 32a4bd9..2c4b80d 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ null *.aux *.synctex* bin +core/.RData From 7b3b4f26639e0c40a686109c7a1bd8a2c0f913c2 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:01:27 -0200 Subject: [PATCH 059/118] Add configure of the DB, with file properties, DAO and class java for connection. --- core/db.properties | 2 +- .../query/dao/postgis/GraphastDAO.java | 58 +++++++++++++++++++ .../query/dao/postgis/QueryPostgis.java | 12 ++++ .../org/graphast/util/ConnectionJDBC.java | 2 +- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/org/graphast/query/dao/postgis/GraphastDAO.java create mode 100644 core/src/main/java/org/graphast/query/dao/postgis/QueryPostgis.java diff --git a/core/db.properties b/core/db.properties index 49e2234..201ec27 100644 --- a/core/db.properties +++ b/core/db.properties @@ -6,6 +6,6 @@ #local driver=org.postgresql.Driver -host=jdbc:postgresql://localhost:5432/piecewise +host=jdbc:postgresql://localhost:5432/fortaleza_novo user=postgres password=postgres \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/dao/postgis/GraphastDAO.java b/core/src/main/java/org/graphast/query/dao/postgis/GraphastDAO.java new file mode 100644 index 0000000..5e2bf79 --- /dev/null +++ b/core/src/main/java/org/graphast/query/dao/postgis/GraphastDAO.java @@ -0,0 +1,58 @@ +package org.graphast.query.dao.postgis; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.graphast.model.Node; +import org.graphast.model.NodeImpl; +import org.graphast.util.ConnectionJDBC; +import org.postgis.Point; + +public class GraphastDAO { + + private static final int FIELD_ID_TAXI = 1; + private final static int FIELD_POINT_TAXI = 2; + private final static int FIELD_ID_ROAD = 3; + + public ResultSet getPoints(String table) throws ClassNotFoundException, SQLException, IOException { + + String finalQuery = String.format(QueryPostgis.QUERY_POINT_ROAD.replace("TABLE_NAME", "%s"), table); + Statement statement = ConnectionJDBC.getConnection().createStatement(); + + return statement.executeQuery(finalQuery); + } + private void addNodeInMap( Map> map, int idRoad, Node node) { + if (!map.containsKey(idRoad)) { + map.put(idRoad, new ArrayList()); + } + map.get(idRoad).add(node); + } + public Map> getPoiTaxiFortaleza() throws ClassNotFoundException, SQLException, IOException { + + Map> mapIdRoads = new HashMap>(); + + String finalQuery = String.format(QueryPostgis.QUERY_POINT_TAXI); + Statement statement = ConnectionJDBC.getConnection().createStatement(); + ResultSet result = statement.executeQuery(finalQuery); + + while (result.next()) { + + String strPointTaxi = result.getString(FIELD_POINT_TAXI); + Point point = new Point(strPointTaxi); + int idTaxi = result.getInt(FIELD_ID_TAXI); + int idRoad = result.getInt(FIELD_ID_ROAD); + Node node = new NodeImpl(idTaxi, point.getY(), point.getX()); + node.setCategory(idTaxi); + node.setLabel(Integer.valueOf(idRoad).toString()); + addNodeInMap(mapIdRoads, idRoad, node); + } + + return mapIdRoads; + } +} diff --git a/core/src/main/java/org/graphast/query/dao/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/dao/postgis/QueryPostgis.java new file mode 100644 index 0000000..9b5936d --- /dev/null +++ b/core/src/main/java/org/graphast/query/dao/postgis/QueryPostgis.java @@ -0,0 +1,12 @@ +package org.graphast.query.dao.postgis; + +public interface QueryPostgis { + + String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; + + String QUERY_POINT_ROAD = "SELECT ro.gid, ST_AsText(st_makeline(st_linemerge(ro.geom))) " + + "FROM TABLE_NAME ro GROUP BY ro.gid ORDER BY ro.gid;"; + + String QUERY_POINT_TAXI = "SELECT t.id, ST_AsText(t.point_geo), t.gid FROM view_taxi_close_linestring t"; + +} diff --git a/core/src/main/java/org/graphast/util/ConnectionJDBC.java b/core/src/main/java/org/graphast/util/ConnectionJDBC.java index 86bb0ce..b176224 100644 --- a/core/src/main/java/org/graphast/util/ConnectionJDBC.java +++ b/core/src/main/java/org/graphast/util/ConnectionJDBC.java @@ -21,7 +21,7 @@ public class ConnectionJDBC { public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException { - if (connection == null) { + if (connection == null || connection.isClosed()) { Properties properties = new Properties(); properties.load(new FileInputStream(new File(FILE_NAME_PROPERTIES))); From ed12bedcca3f92da7e0ae9750e1a12e7bd87c1b8 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:04:17 -0200 Subject: [PATCH 060/118] Create importer of the OSM Fortaleza/Ce-Brazil and ploted taxi into graph. Data taxi is of Taxi Simples. --- .../org/graphast/importer/OSMDBImporter.java | 146 ++++++++++++++++++ .../importer/PoiTaxiFortalezaImporter.java | 100 ++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 core/src/main/java/org/graphast/importer/OSMDBImporter.java create mode 100644 core/src/main/java/org/graphast/importer/PoiTaxiFortalezaImporter.java diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java new file mode 100644 index 0000000..00f271c --- /dev/null +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -0,0 +1,146 @@ +package org.graphast.importer; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.graphast.model.Edge; +import org.graphast.model.EdgeImpl; +import org.graphast.model.GraphBounds; +import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.Node; +import org.graphast.model.NodeImpl; +import org.graphast.query.dao.postgis.GraphastDAO; +import org.graphast.util.ConnectionJDBC; +import org.graphast.util.DistanceUtils; +import org.graphast.util.GeoUtils; +import org.postgis.LineString; +import org.postgis.Point; + +public class OSMDBImporter implements Importer { + + private GraphastDAO dao; + private GraphBounds graph; + private String table; + private final int FIELD_ID_LINESTRING = 1; + private final int FIELD_LINESTRING = 2; + private final int SIZE_INTERVAL = 96; + + protected static final Logger LOGGER = Logger.getGlobal(); + + public OSMDBImporter(String table, String directory) { + this.table = table; + dao = new GraphastDAO(); + graph = new GraphBoundsImpl(directory); + } + + @Override + public GraphBounds execute() { + + try { + plotNodes(); + graph.createBounds(); + + } catch (SQLException | ClassNotFoundException | IOException e) { + System.err.println("[ERRO] Ocorreu um erro na construção do grafo."); + } + return graph; + } + + private void plotNodes() throws SQLException, ClassNotFoundException, IOException { + Map> mapTaxi = dao.getPoiTaxiFortaleza(); + + ResultSet result = dao.getPoints(table); + int pointCount = 0; + while (result.next()) { + LineString lineString = new LineString(result.getString(FIELD_LINESTRING)); + Point[] arrayPoints = lineString.getPoints(); + LOGGER.log(Level.INFO, String.format("registro: %s", result.getString(FIELD_LINESTRING))); + + + int idRoad = result.getInt(FIELD_ID_LINESTRING); + Node previousNode = null; + + for (Point point : arrayPoints) { + pointCount++; + LOGGER.log(Level.INFO, String.format("Point [x,y]: %s,%s", point.getX(), point.getY())); + Node node = new NodeImpl(point.getY(), point.getX()); + node.setLabel(Long.valueOf(idRoad).toString()); + Long nodeId = graph.getNodeId(GeoUtils.latLongToInt(node.getLatitude()), GeoUtils.latLongToInt(node.getLongitude())); + + if (nodeId != null) { + LOGGER.log(Level.INFO, String.format("point already exist in graph")); + node = graph.getNode(nodeId); + } else { + graph.addNode(node); + LOGGER.log(Level.INFO, String.format("point inserted in graph with ID: %s", node.getId())); + } + + + + if (previousNode != null && !previousNode.getId().equals(node.getId())) { + LOGGER.log(Level.INFO, String.format("Add edge from previous: %s to current: %s node", previousNode.getId(), node.getId())); + Edge edge = new EdgeImpl(idRoad, previousNode.getId() .longValue(), node.getId().longValue(), 0); + addCost(edge); + graph.addEdge(edge); + if(mapTaxi.containsKey(idRoad)) { + for(int i=0; i taxiNodes = getCloseTaxi(externalId); + Node nodeTo = graph.getNode(edge.getToNode()); + + for (Node node: taxiNodes) { + + if (!graph.getCategories().contains(node.getCategory())) { + graph.addNode(node); + Edge edgeToNeighbor = new EdgeImpl(node.getId(), nodeTo.getId(), 1); + addCostZero(edgeToNeighbor); + graph.addEdge(edgeToNeighbor); + } + } + } + + private void addCostZero(Edge edge) { + + int[] costs = new int[SIZE_INTERVAL]; + for (int i : costs) { + costs[i] = 2; + } + edge.setCosts(costs); + } + + private List getCloseTaxi(long idLineString) throws ClassNotFoundException, IOException { + + List taxiNodes = new ArrayList(); + try { + PreparedStatement taxiStatement = ConnectionJDBC.getConnection().prepareStatement(QueryPostgis.QUERY_POINT_TAXI); + taxiStatement.setLong(FIELD_PARAMETER_ID_LINESTRING, idLineString); + ResultSet resultSet = taxiStatement.executeQuery(); + + while (resultSet.next()) { + + String string = resultSet.getString(FIELD_POINT_TAXI); + Point point = new Point(string); + Node node = new NodeImpl(resultSet.getInt(FIELD_ID_TAXI), point.getY(), point.getX()); + node.setCategory(resultSet.getInt(FIELD_ID_TAXI)); + node.setExternalId(resultSet.getInt(FIELD_ID_TAXI)); + taxiNodes.add(node); + } + + ConnectionJDBC.getConnection().close(); + + } catch (SQLException e) { + e.printStackTrace(); + } + + return taxiNodes; + } + +} From 475426829867d867276e70028860473a5925e8bc Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:06:38 -0200 Subject: [PATCH 061/118] add postgis dependency and add configuration main class for service run. Compare solution of the RNN. --- core/pom.xml | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 0af0fdb..a544ab9 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -14,20 +14,7 @@ maven-repo http://arida.github.io/maven-repo/ - + @@ -91,18 +78,18 @@ rtree 0.7.1 - - + postgresql postgresql 9.1-901-1.jdbc4 + + + org.postgis + postgis-jdbc + 1.3.3 + @@ -166,6 +153,26 @@ --> + + org.apache.maven.plugins + maven-shade-plugin + 2.4.2 + + + package + + shade + + + + + org.graphast.query.rnn.CompareRNNSearchsMethodsAnalysis + + + + + + From c7b2edba414c7a32cdd4ca3c7991751995dc8c82 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:09:55 -0200 Subject: [PATCH 062/118] Removed classes referente of the solution piecewise. --- .../piecewise/GeneratorFunctionPiecewise.java | 108 ----------------- .../GeneratorFunctionPiecewiseCsv.java | 14 --- .../GeneratorFunctionPiecewiseDatabase.java | 59 --------- .../IGeneratorFunctionPiecewise.java | 7 -- .../piecewise/PiecewiseException.java | 10 -- .../graphast/query/postgis/QueryPostgis.java | 7 -- ...eneratorFunctionPiecewiseDatabaseTest.java | 83 ------------- .../piecewise/RCallerEnvironmentsTest.java | 113 ------------------ 8 files changed, 401 deletions(-) delete mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java delete mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java delete mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java delete mode 100644 core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java delete mode 100644 core/src/main/java/org/graphast/piecewise/PiecewiseException.java delete mode 100644 core/src/main/java/org/graphast/query/postgis/QueryPostgis.java delete mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java delete mode 100644 core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java deleted file mode 100644 index 4ba69b8..0000000 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewise.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.graphast.piecewise; - -import rcaller.RCaller; -import rcaller.RCode; - -public class GeneratorFunctionPiecewise { - - private static final String RSCRIPT = "/usr/bin/Rscript"; - - public double getValue(double timestamp) throws PiecewiseException { - - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - double[][] coleta = generatorFunctionPiecewise.getData(); - return getFuntionEdge(coleta, timestamp); - } - - private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addDoubleMatrix("matrix", data); - code.addRCode("dados.frame <- data.frame(matrix)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); - code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); - double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); - - PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); - PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); - - double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); - double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); - - - double yFinal = 0; - for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { - - PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); - PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); - double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); - double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); - - yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); - - coeficienteAngularAnterior = coeficienteAngularCurrent; - coeficienteLinearAnterior = coeficienteLinearCurrent; - } - - double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); - - PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], - pontosInflexao[pontosInflexao.length - 1]); - PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); - - double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); - double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - - yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); - - return yFinal; - } - - private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { - double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); - return gama; - } - - private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { - double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); - return alfa; - } - - class PontoGeometrico { - - double x; - double y; - - public PontoGeometrico(double x, double y) { - this.x = x; - this.y= y; - } - - public double getX() { - return x; - } - public void setX(double x) { - this.x = x; - } - public double getY() { - return y; - } - public void setY(double y) { - this.y = y; - } - } -} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java deleted file mode 100644 index b1d9fb0..0000000 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseCsv.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.graphast.piecewise; - -import java.util.Date; -import java.util.List; -import java.util.Map; - -public class GeneratorFunctionPiecewiseCsv implements IGeneratorFunctionPiecewise { - - @Override - public double[][] getData() { - - return null; - } -} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java deleted file mode 100644 index a74e1b2..0000000 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabase.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.graphast.piecewise; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; - -import org.graphast.query.postgis.QueryPostgis; -import org.graphast.util.ConnectionJDBC; - -public class GeneratorFunctionPiecewiseDatabase implements IGeneratorFunctionPiecewise { - - private int FIELD_DATE_TIME = 1; - private int FIELD_DURACAO = 2; - - @Override - public double[][] getData() throws PiecewiseException { - - Connection connectionJDBC = null; - try { - connectionJDBC = ConnectionJDBC.getConnection(); - } catch (ClassNotFoundException | SQLException | IOException e) { - throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); - } - - ResultSet resultSet = null; - double[][] matrixResult; - try { - Statement statement = connectionJDBC.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - resultSet = statement.executeQuery(QueryPostgis.QUERY_ALL_DATE_TIME_WITH_DURATE); - - resultSet.last(); - int size = resultSet.getRow(); - resultSet.beforeFirst(); - - matrixResult = new double[size][2]; - - int i = 0; - while (resultSet.next()) { - - Double dateTime = resultSet.getDouble(FIELD_DATE_TIME); - Double duracao = resultSet.getDouble(FIELD_DURACAO); - double[] result = new double[2]; - result[0] = duracao; - result[1] = dateTime; - - matrixResult[i] = result; - i++; - } - - } catch (SQLException e) { - throw new PiecewiseException("[ERRO] Um erro ocorreu ao pegar as informações no DB."); - } - - - return matrixResult; - } -} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java deleted file mode 100644 index 14dcad7..0000000 --- a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.graphast.piecewise; - - -public interface IGeneratorFunctionPiecewise { - - public double[][] getData() throws PiecewiseException; -} diff --git a/core/src/main/java/org/graphast/piecewise/PiecewiseException.java b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java deleted file mode 100644 index 28b8cb3..0000000 --- a/core/src/main/java/org/graphast/piecewise/PiecewiseException.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.graphast.piecewise; - -public class PiecewiseException extends Exception { - - private static final long serialVersionUID = 1L; - - public PiecewiseException(String error) { - super(error); - } -} diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java deleted file mode 100644 index 9271efa..0000000 --- a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.graphast.query.postgis; - -public interface QueryPostgis { - - String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; - -} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java deleted file mode 100644 index c05c83f..0000000 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.graphast.piecewise; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.SQLException; - -import org.graphast.util.ConnectionJDBC; -import org.junit.Assert; -import org.junit.Test; - -import rcaller.RCaller; -import rcaller.RCode; - -public class GeneratorFunctionPiecewiseDatabaseTest { - - private static final String RSCRIPT = "/usr/bin/Rscript"; - - @Test - public void getConnectionTest() throws ClassNotFoundException, SQLException, IOException { - - Connection connection = ConnectionJDBC.getConnection(); - Assert.assertNotNull(connection); - } - - @Test - public void getDataTest() throws PiecewiseException { - - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - double[][] data = generatorFunctionPiecewise.getData(); - int length = data.length; - - Assert.assertEquals(28, length); - } - - @Test - public void verifyPointsTest() throws IOException, PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - IGeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewiseDatabase(); - double[][] data = generatorFunctionPiecewise.getData(); - - code.addDoubleMatrix("matrix", data); - code.addRCode("dados.frame <- data.frame(matrix)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); - code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - System.out.println(caller.getParser().getXMLFileAsString().toString()); - - double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); - Assert.assertEquals(Double.valueOf("1201977368000"), Double.valueOf(pontoInicial[0])); - Assert.assertEquals(Double.valueOf("295522763826171"), Double.valueOf(pontoInicial[1])); - - double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); - int length = pontosInflexao.length; - Assert.assertEquals(6, length); - - Assert.assertEquals(Double.valueOf("1202063763696"), Double.valueOf(pontosInflexao[0])); - Assert.assertEquals(Double.valueOf("55328186.6718641"), Double.valueOf(pontosInflexao[length/2])); - } - - @Test - public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { - - GeneratorFunctionPiecewise generatorFunctionPiecewise = new GeneratorFunctionPiecewise(); - double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); - - System.out.println(value); - Assert.assertNotNull(value); - } -} diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java deleted file mode 100644 index e458b6a..0000000 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.graphast.piecewise; - -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Test; - -import rcaller.RCaller; -import rcaller.RCode; - -public class RCallerEnvironmentsTest { - - private static final String RSCRIPT = "/usr/bin/Rscript"; - - @Test - public void createRscriptTest() { - - RCaller rcaller = new RCaller(); - rcaller.setRscriptExecutable(RSCRIPT); - Assert.assertNotNull(rcaller.getRCode()); - } - - @Test - public void baseLinearOutXMLTest() throws IOException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - - code.addRCode("set.seed(123)"); - code.addRCode("x<-rnorm(10)"); - code.addRCode("y<-rnorm(10)"); - code.addRCode("ols<-lm(y~x)"); - - caller.setRCode(code); - caller.runAndReturnResult("ols"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - } - - - @Test - public void loessTest() throws IOException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addRCode("set.seed(123)"); - code.addRCode("x<-rnorm(10)"); - code.addRCode("y<-rnorm(10)"); - code.addRCode("dados.loess <- loess(y ~ x)"); - - caller.setRCode(code); - caller.runAndReturnResult("dados.loess"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - } - - @Test - public void loessWithRandomDataTest() throws IOException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - - caller.setRscriptExecutable(RSCRIPT); - - code.addRCode("set.seed(123)"); - code.addRCode("x<-rnorm(10)"); - code.addRCode("y<-rnorm(10)"); - code.addRCode("dados.loess <- loess(y ~ x)"); - code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); - code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - } - - @Test - public void loessWithSynteticDataTest() throws IOException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addRCode("set.seed(123)"); - code.addRCode("x<-rnorm(10)"); - code.addRCode("y<-rnorm(10)"); - code.addRCode("dados.loess <- loess(y ~ x)"); - code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); - code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - System.out.println(caller.getParser().getXMLFileAsString()); - Assert.assertNotNull(caller.getParser().getXMLFileAsString()); - } -} From a48704ce0be4b323edb0431b1198a55a2c518035 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:16:32 -0200 Subject: [PATCH 063/118] Create classes with naive (Dijkstra) and proposed solution to the problem of the nearest taxi. Solutions includes unit testing and comparison between class solutions (with synthetic and real graph). --- .../query/knn/RouteQueueRNNEntry.java | 25 + .../rnn/CompareRNNSearchsMethodsAnalysis.java | 121 +++++ .../graphast/query/rnn/IRNNTimeDependent.java | 13 + .../query/rnn/RNNBreadthFirstSearch.java | 130 +++++ .../graphast/query/rnn/RNNComparatorTest.java | 188 +++++++ .../query/rnn/RNNDepthFirstSearch.java | 83 +++ .../query/rnn/RouteQueueRNNEntry.java | 25 + .../graphgenerator/GraphGenerator.java | 510 ++++++++++++++++++ .../graphgenerator/GraphGeneratorGrid.java | 129 +++++ .../GraphGeneratorGridTest.java | 213 ++++++++ .../graphast/importer/OSMDBImporterTest.java | 52 ++ .../PoiTaxiFortalezaImporterTest.java | 26 + .../knn/RNNBreadthFirstSearchTest.java | 329 +++++++++++ .../graphast/knn/RNNDepthFirstSearchTest.java | 260 +++++++++ 14 files changed, 2104 insertions(+) create mode 100644 core/src/main/java/org/graphast/query/knn/RouteQueueRNNEntry.java create mode 100644 core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java create mode 100644 core/src/main/java/org/graphast/query/rnn/IRNNTimeDependent.java create mode 100644 core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java create mode 100644 core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java create mode 100644 core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java create mode 100644 core/src/main/java/org/graphast/query/rnn/RouteQueueRNNEntry.java create mode 100644 core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java create mode 100644 core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java create mode 100644 core/src/test/java/org/graphast/importer/OSMDBImporterTest.java create mode 100644 core/src/test/java/org/graphast/importer/PoiTaxiFortalezaImporterTest.java create mode 100644 core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java create mode 100644 core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java diff --git a/core/src/main/java/org/graphast/query/knn/RouteQueueRNNEntry.java b/core/src/main/java/org/graphast/query/knn/RouteQueueRNNEntry.java new file mode 100644 index 0000000..5d50d65 --- /dev/null +++ b/core/src/main/java/org/graphast/query/knn/RouteQueueRNNEntry.java @@ -0,0 +1,25 @@ +package org.graphast.query.knn; + +import java.util.List; + +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.route.shortestpath.model.TimeEntry; + +/** + * Representa um taxista na malha com a rota do cliente a este. + * + */ +public class RouteQueueRNNEntry extends TimeEntry { + + private List routes; + + public RouteQueueRNNEntry(long id, int travelTime, int arrivalTime, long parentId, List routes) { + super(id, travelTime, arrivalTime, parentId); + this.routes = routes; + } + + public List getRoutes() { + return routes; + } + +} diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java new file mode 100644 index 0000000..32d9393 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -0,0 +1,121 @@ +package org.graphast.query.rnn; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.logging.Logger; + +import org.graphast.config.Configuration; +import org.graphast.exception.PathNotFoundException; +import org.graphast.importer.OSMDBImporter; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.DateUtils; +import org.graphast.util.NumberUtils; + +public class CompareRNNSearchsMethodsAnalysis { + private static final String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + + protected static final Logger LOGGER = Logger.getGlobal(); + + public static void main(String[] args) throws IOException { + runAnalysis("view_exp_1k", Integer.parseInt(args[0])); + runAnalysis("view_exp_10k", Integer.parseInt(args[0])); + runAnalysis("view_exp_100k", Integer.parseInt(args[0])); + runAnalysis("view_exp_300mil", Integer.parseInt(args[0])); + } + + public static void runAnalysis(String tableName, int testTimes) throws IOException { + + OSMDBImporter importer = new OSMDBImporter(tableName, PATH_GRAPH+tableName); + GraphBounds graph = importer.execute(); + + OSMDBImporter importerReverse = new OSMDBImporter(tableName, PATH_GRAPH+tableName+"_reverse"); + GraphBounds graphReverse = importerReverse.execute(); + + + RNNDepthFirstSearch rnnDFS = new RNNDepthFirstSearch(graph); + RNNBreadthFirstSearch rnnBFS = new RNNBreadthFirstSearch(graphReverse); + + Date timeout = DateUtils.parseDate(00, 20, 00); + Date timestamp = DateUtils.parseDate(00, 00, 00); + + FileWriter rnnDFSFileCsv = new FileWriter(tableName+"_rnn_dfs.csv"); + FileWriter rnnBFSFileCsv = new FileWriter(tableName+"_rnn_bfs.csv"); + + for (int i = 0; i < testTimes; i++) { + Node customer = getRandomCustomerInGraph(graph); + runSearchAndWrite(graph, rnnDFS, customer, timeout, timestamp, rnnDFSFileCsv); + runSearchAndWrite(graph, rnnBFS, customer, timeout, timestamp, rnnBFSFileCsv); + } + + rnnBFSFileCsv.close(); + rnnDFSFileCsv.close(); + } + + private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, + Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { + try { + long startTime = System.currentTimeMillis(); + NearestNeighbor solution = rnn.search(customer, timeout, timestamp); + long endTime = System.currentTimeMillis(); + + long time = endTime - startTime; + + Long solutionId = null; + Integer distance = null; + Integer nodesSize = null; + ArrayList path = null; + Long externalId = null; + if(solution != null && solution.getPath()!=null) { + solutionId = solution.getId(); + distance = solution.getDistance(); + nodesSize = solution.getPath().size(); + path = solution.getPath(); + externalId = graph.getNode(solution.getId()).getExternalId(); + + String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); + String gidCustomer = customer.getLabel(); + + Node nodePoi = graph.getNode(solutionId); + String poiCoordinate = nodePoi.getLongitude() + "," + nodePoi.getLatitude(); + String gidPoi = nodePoi.getLabel(); + + String coordinateNodeVisited = ""; + String gidVisited = ""; + for (Long visited : path) { + Node nodeVisited = graph.getNode(visited); + coordinateNodeVisited = coordinateNodeVisited + "(" + nodeVisited.getLongitude() + "," + nodeVisited.getLatitude() + ")"; + + gidVisited = gidVisited + "-" + nodeVisited.getLabel(); + } + + + + String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, + poiCoordinate, time, solutionId, externalId, distance, + nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited) + "\n"; + + System.out.println(currentLine); + + fileCsv.write(currentLine); + } + } catch(PathNotFoundException e) { + System.err.println(String.format("Customer %s (%s, %s) has no POI in subgraph", customer.getId(), customer.getLatitude(), customer.getLongitude())); + } + } + + + private static Node getRandomCustomerInGraph(GraphBounds graph) { + Node node; + double[] bounds = new double[]{-3.710467, -38.591078, -3.802376, -38.465530}; + do { + long id = Double.valueOf(NumberUtils.generatePdseurandom(0, Long.valueOf(graph.getNumberOfNodes()-1).intValue())).longValue(); + node = graph.getNode(id); + } while(node.getCategory()!=-1 || node.getLatitude()>bounds[0] || node.getLatitude()bounds[3]); + return node; + } + +} diff --git a/core/src/main/java/org/graphast/query/rnn/IRNNTimeDependent.java b/core/src/main/java/org/graphast/query/rnn/IRNNTimeDependent.java new file mode 100644 index 0000000..2d2955e --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/IRNNTimeDependent.java @@ -0,0 +1,13 @@ +package org.graphast.query.rnn; + +import java.util.Date; + +import org.graphast.exception.PathNotFoundException; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; + +public interface IRNNTimeDependent { + + public NearestNeighbor search(Node root, Date timeout, Date timestamp) throws PathNotFoundException; + +} diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java new file mode 100644 index 0000000..1765b99 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -0,0 +1,130 @@ +package org.graphast.query.rnn; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Set; + +import org.graphast.exception.PathNotFoundException; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.DateUtils; + +public class RNNBreadthFirstSearch implements IRNNTimeDependent{ + + private GraphBounds graph; + + public RNNBreadthFirstSearch(GraphBounds graph) { + this.graph = graph; + this.graph.reverseGraph(); + } + + public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServiceTime) { + + if (graph.getPoi(customer.getId()) != null) { + ArrayList path = new ArrayList(); + path.add(customer.getId()); + return new NearestNeighbor(customer.getId(), 0, path); + } + + PriorityQueue queue = new PriorityQueue(); + Map> parents = new HashMap>(); + + long maxTravelTimeMilliseconds = DateUtils.dateToMilli(maxTravelTime); + long hourServiceTimeMilliseconds = DateUtils.dateToMilli(startServiceTime); + long startServiceTimeMilliseconds = hourServiceTimeMilliseconds + maxTravelTimeMilliseconds; + + init(customer, queue, parents, hourServiceTimeMilliseconds, startServiceTimeMilliseconds); + RouteQueueRNNEntry current = null; + Set visited = new HashSet<>(); + + while(!queue.isEmpty()) { + + current = queue.poll(); + if (visited.contains(current.getId())) { + continue; + } else { + visited.add(current.getId()); + } + + if(current.getTravelTime() > maxTravelTimeMilliseconds) { + throw new PathNotFoundException(String.format("not found path in reverse graph for parameter time %s milliseconds.", + maxTravelTimeMilliseconds)); + } + + if (graph.getPoi(current.getId()) != null) { + return new NearestNeighbor(current.getId(), current.getTravelTime(), pathToTaxi(current.getId(), customer.getId(), parents)); + } + + // Acessa os vizinhos do primeiro vértice da pilha, no caso os vizinho do vértice que representa o cliente. + HashMap neighbors = graph.accessNeighborhood(graph.getNode(current.getId()), current.getArrivalTime()); + + for (Node neighbor : neighbors.keySet()) { + if (visited.contains(neighbor.getId())) { + continue; + } + int travelTime = current.getTravelTime() + neighbors.get(neighbor); + if (travelTime > maxTravelTimeMilliseconds) { + continue; + } + + List parents_list = new ArrayList(); + parents_list.add(current.getId()); + parents.put(neighbor.getId(), parents_list); + + int arrivalTime = current.getArrivalTime() - neighbors.get(neighbor); + + RouteQueueRNNEntry newRouteQueueTaxiEntry = new RouteQueueRNNEntry(neighbor.getId(), travelTime, + arrivalTime, current.getId(), current.getRoutes()); + queue.offer(newRouteQueueTaxiEntry); + } + + + } + + throw new PathNotFoundException("not found path in reverse graph"); + } + + private ArrayList pathToTaxi(Long idTaxista, Long idCustomer, Map> parents) { + + ArrayList idsPath = new ArrayList(); + idsPath.add(idTaxista); + + Long idAnterior = idTaxista; + Set keySet = parents.keySet(); + for (Iterator iter = keySet.iterator(); iter.hasNext(); ) { + Long next = iter.next(); + if(next.equals(idAnterior) && !next.equals(idCustomer)) { + List list = parents.get(next); + for (Long long1 : list) { + idsPath.add(long1); + idAnterior = long1; + iter = keySet.iterator(); + } + } + } + if(!idsPath.contains(idCustomer)) { + idsPath.add(idCustomer); + } + + return idsPath; + } + + private void init(Node customer, PriorityQueue queue, Map> parents, long startServiceTime, long arrivedTime) { + + int travelTime = Long.valueOf(startServiceTime).intValue(); + int arrivalTime = Long.valueOf(arrivedTime).intValue(); + + List parents_list = new ArrayList(); + parents_list.add(Long.valueOf(-1)); + parents.put(customer.getId(), parents_list); + + queue.offer(new RouteQueueRNNEntry(customer.getId(), travelTime, arrivalTime, -1, new ArrayList())); + } +} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java b/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java new file mode 100644 index 0000000..a279b47 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java @@ -0,0 +1,188 @@ +package org.graphast.query.rnn; + +import java.io.IOException; +import java.text.ParseException; +import java.util.Date; + +import org.graphast.config.Configuration; +import org.graphast.graphgenerator.GraphGeneratorGrid; +import org.graphast.model.GraphBounds; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.DateUtils; +import org.graphast.util.FileUtils; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; + +public class RNNComparatorTest { + + private Integer idCustomer = null; + private Date endServiceTime = null; + private Date startServiceTime = null; + private Double percentagemPoi = null; + + @Before + public void setUp() throws ParseException, IOException { + + //Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(0); + + //Tempo para atendiemento - 23h 59m 59s + endServiceTime = DateUtils.parseDate(0, 20, 00); + + //Hora que ele realiza a chamada do serviço + startServiceTime = DateUtils.parseDate(00, 00, 00); + + percentagemPoi = Double.valueOf(1); + } + + // 1k (1024 pontos) + @Test + public void taxiSearchSytenticGraph1k() throws IOException, ParseException { + + for(int i=0; i<100; i++) { + + int comprimento = 32; + int altura = 32; + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + //==== SOLUÇÃO I ==== + long startSolution1 = System.currentTimeMillis(); + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution1 = System.currentTimeMillis(); + long timeSolution1 = endSolution1-startSolution1; + + graph.reverseGraph(); + + //==== SOLUÇÃO III ==== + long startSolution3 = System.currentTimeMillis(); + RNNBreadthFirstSearch taxiSearchRestritionsOSR = new RNNBreadthFirstSearch(graph); + NearestNeighbor solution3 = taxiSearchRestritionsOSR.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution3 = System.currentTimeMillis(); + long timeSolution3 = endSolution3-startSolution3; + + System.out.println(String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", timeSolution1, timeSolution3, solution1.getId(), solution3.getId(), solution1.getDistance(), + solution3.getDistance(), solution1.getPath().size(), solution3.getPath().size(), solution1.getPath(), solution3.getPath())); + } + } + + // 10k (10000 pontos) + @Test + public void taxiSearchSytenticGraph10k() throws IOException, ParseException { + + for(int i=0; i<100; i++) { + + int comprimento = 100; + int altura = 100; + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + //==== SOLUÇÃO I ==== + long startSolution1 = System.currentTimeMillis(); + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution1 = System.currentTimeMillis(); + long timeSolution1 = endSolution1-startSolution1; + + graph.reverseGraph(); + + //==== SOLUÇÃO III ==== + long startSolution3 = System.currentTimeMillis(); + RNNBreadthFirstSearch taxiSearchRestritionsOSR = new RNNBreadthFirstSearch(graph); + NearestNeighbor solution3 = taxiSearchRestritionsOSR.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution3 = System.currentTimeMillis(); + long timeSolution3 = endSolution3-startSolution3; + + System.out.println(String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", timeSolution1, timeSolution3, solution1.getId(), solution3.getId(), solution1.getDistance(), + solution3.getDistance(), solution1.getPath().size(), solution3.getPath().size(), solution1.getPath(), solution3.getPath())); + } + } + + // 100k (99856 pontos) + @Test + public void taxiSearchSytenticGraph100k() throws IOException, ParseException { + + for(int i=0; i<100; i++) { + + int comprimento = 316; + int altura = 316; + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + GraphBounds reverseGraph = graphSynthetic.getGraph(); + graph = reverseGraph; + reverseGraph.reverseGraph(); + + //==== SOLUÇÃO I ==== + long startSolution1 = System.currentTimeMillis(); + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution1 = System.currentTimeMillis(); + long timeSolution1 = endSolution1-startSolution1; + + + //==== SOLUÇÃO III ==== + long startSolution3 = System.currentTimeMillis(); + RNNBreadthFirstSearch taxiSearchRestritionsOSR = new RNNBreadthFirstSearch(reverseGraph); + NearestNeighbor solution3 = taxiSearchRestritionsOSR.search(reverseGraph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution3 = System.currentTimeMillis(); + long timeSolution3 = endSolution3-startSolution3; + + System.out.println(String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", timeSolution1, timeSolution3, solution1.getId(), solution3.getId(), solution1.getDistance(), + solution3.getDistance(), solution1.getPath().size(), solution3.getPath().size(), solution1.getPath(), solution3.getPath())); + } + } + + // 1G (1000000 pontos) + @Test + public void taxiSearchSytenticGraph1000k() throws IOException, ParseException { + + for(int i=0; i<10; i++) { + + int comprimento = 1000; + int altura = 1000; + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + GraphBounds reverseGraph = graphSynthetic.getGraph(); + graph = reverseGraph; + reverseGraph.reverseGraph(); + + //==== SOLUÇÃO I ==== + long startSolution1 = System.currentTimeMillis(); + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution1 = System.currentTimeMillis(); + long timeSolution1 = endSolution1-startSolution1; + + + //==== SOLUÇÃO III ==== + long startSolution3 = System.currentTimeMillis(); + RNNBreadthFirstSearch taxiSearchRestritionsOSR = new RNNBreadthFirstSearch(reverseGraph); + NearestNeighbor solution3 = taxiSearchRestritionsOSR.search(reverseGraph.getNode(idCustomer), endServiceTime, startServiceTime); + long endSolution3 = System.currentTimeMillis(); + long timeSolution3 = endSolution3-startSolution3; + + System.out.println(String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", timeSolution1, timeSolution3, solution1.getId(), solution3.getId(), solution1.getDistance(), + solution3.getDistance(), solution1.getPath().size(), solution3.getPath().size(), solution1.getPath(), solution3.getPath())); + } + } + + + @AfterClass + public static void tearDown() { + + FileUtils.deleteDir(Configuration.USER_HOME + "/graphhopper/test"); + FileUtils.deleteDir(Configuration.USER_HOME + "/graphast/test"); + } +} diff --git a/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java new file mode 100644 index 0000000..be0106a --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java @@ -0,0 +1,83 @@ +package org.graphast.query.rnn; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.graphast.exception.PathNotFoundException; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.route.shortestpath.dijkstra.Dijkstra; +import org.graphast.query.route.shortestpath.dijkstra.DijkstraLinearFunction; +import org.graphast.query.route.shortestpath.model.Path; +import org.graphast.util.DateUtils; + +public class RNNDepthFirstSearch implements IRNNTimeDependent { + + private GraphBounds graph; + + public RNNDepthFirstSearch(GraphBounds graphBounds) { + this.graph = graphBounds; + } + + public NearestNeighbor search(Node root, Date timeout, Date timestamp) throws PathNotFoundException { + + long maxTravelTimeMillisenconds = DateUtils.dateToMilli(timeout); + double bestTravelTime = maxTravelTimeMillisenconds; + long currentPoi = -1; + Path pathResult = null; + + Dijkstra dijkstraShortestPathLinearFunction = new DijkstraLinearFunction(graph); + + for (Long poi : graph.getPoiIds()) { + try { + Node target = graph.getNode(poi); + Path path = dijkstraShortestPathLinearFunction.shortestPath( + target.getId(), root.getId(), timestamp); + + if (path.getTotalCost() <= maxTravelTimeMillisenconds + && path.getTotalCost() <= bestTravelTime) { + currentPoi = target.getId(); + bestTravelTime = path.getTotalCost(); + pathResult = path; + } + } catch(PathNotFoundException e) { +// System.err.println(e.getMessage()); + } + } + + NearestNeighbor nearestNeighbor = createNN(root, currentPoi, pathResult); + + if (nearestNeighbor == null) { + throw new PathNotFoundException( + "target not found for root and set timestamp"); + } + + return nearestNeighbor; + } + + private NearestNeighbor createNN(Node root, long currentPoi, Path path) { + + NearestNeighbor nearestNeighbor = new NearestNeighbor(); + if (currentPoi > -1) { + + nearestNeighbor.setDistance((int) path.getTotalCost()); + nearestNeighbor.setId(currentPoi); + + ArrayList arrayPath = new ArrayList(); + List edges = path.getEdges(); + + if (edges != null) { + for (Long edge : edges) { + arrayPath.add(graph.getEdge(edge).getFromNode()); + } + } + + arrayPath.add(root.getId()); + nearestNeighbor.setPath(arrayPath); + } + + return nearestNeighbor; + } +} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/rnn/RouteQueueRNNEntry.java b/core/src/main/java/org/graphast/query/rnn/RouteQueueRNNEntry.java new file mode 100644 index 0000000..4a384c7 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/RouteQueueRNNEntry.java @@ -0,0 +1,25 @@ +package org.graphast.query.rnn; + +import java.util.List; + +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.route.shortestpath.model.TimeEntry; + +/** + * Representa um taxista na malha com a rota do cliente a este. + * + */ +public class RouteQueueRNNEntry extends TimeEntry { + + private List routes; + + public RouteQueueRNNEntry(long id, int travelTime, int arrivalTime, long parentId, List routes) { + super(id, travelTime, arrivalTime, parentId); + this.routes = routes; + } + + public List getRoutes() { + return routes; + } + +} diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java b/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java index 92e57b5..adf6abe 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java @@ -1,5 +1,6 @@ package org.graphast.graphgenerator; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -572,5 +573,514 @@ public Graph generateAndorra() { return graph; } + +public GraphBounds generateExampleTAXI() { + + GraphBounds graph = new GraphBoundsImpl(Configuration.USER_HOME + "/graphast/test/examplePoI"); + + Node node; + Edge edge; + + node = new NodeImpl(0l, 0.0d, 1.0d); + graph.addNode(node); + + node = new NodeImpl(1l, 0.0d, 10.0d); + int[] costs = new int[]{0}; + node.setCategory(1); + node.setLabel("TAXI I"); + node.setCosts(costs); + graph.addNode(node); + + node = new NodeImpl(2l, 0.0d, 20.0d); + graph.addNode(node); + + node = new NodeImpl(3l, 0.0d, 30.0d); + graph.addNode(node); + + node = new NodeImpl(4l, 0.0d, 40.0d); + costs = new int[]{0}; + node.setCategory(2); + node.setLabel("TAXI II"); + node.setCosts(costs); + graph.addNode(node); + + node = new NodeImpl(5l, 10.0d, 0.0d); + graph.addNode(node); + + node = new NodeImpl(6l, 10.0d, 10.0d); + graph.addNode(node); + + node = new NodeImpl(7l, 10.0d, 20.0d); + graph.addNode(node); + + node = new NodeImpl(8l, 10.0d, 30.0d); + graph.addNode(node); + + node = new NodeImpl(9l, 10.0d, 40.0d); + costs = new int[]{0}; + node.setCategory(3); + node.setLabel("TAXI III"); + node.setCosts(costs); + graph.addNode(node); + + //EDGES + + edge = new EdgeImpl(1l, 0l, 1l, 15); + int[] costsEdge1 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000}; + edge.setCosts(costsEdge1); + graph.addEdge(edge); + + edge = new EdgeImpl(2l, 1l, 2l, 15); + int[] costsEdge2 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge2); + graph.addEdge(edge); + + edge = new EdgeImpl(3l, 1l, 7l, 12); + int[] costsEdge3 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge3); + graph.addEdge(edge); + + edge = new EdgeImpl(4l, 2l, 3l, 10); + int[] costsEdge4 = new int[]{600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge4); + graph.addEdge(edge); + + edge = new EdgeImpl(5l, 3l, 4l, 12); + int[] costsEdge5 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000}; + edge.setCosts(costsEdge5); + graph.addEdge(edge); + + edge = new EdgeImpl(6l, 4l, 8l, 12); + int[] costsEdge6 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge6); + graph.addEdge(edge); + + edge = new EdgeImpl(7l, 4l, 9l, 12); + int[] costsEdge7 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge7); + graph.addEdge(edge); + + edge = new EdgeImpl(8l, 5l, 0l, 12); + int[] costsEdge8 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000}; + edge.setCosts(costsEdge8); + graph.addEdge(edge); + + edge = new EdgeImpl(9l, 6l, 5l, 15); + int[] costsEdge9 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge9); + graph.addEdge(edge); + + edge = new EdgeImpl(10l, 7l, 2l, 15); + int[] costsEdge10 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge10); + graph.addEdge(edge); + + edge = new EdgeImpl(11l, 7l, 6l, 12); + int[] costsEdge11 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge11); + graph.addEdge(edge); + + edge = new EdgeImpl(12l, 8l, 7l, 12); + int[] costsEdge12 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge12); + graph.addEdge(edge); + + edge = new EdgeImpl(13l, 9l, 8l, 15); + int[] costsEdge13 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge13); + graph.addEdge(edge); + + graph.createBounds(); + + return graph; + } + + public GraphBounds generateExampleTaxi15to15minutes() { + + String separator = File.separator; + final String directory = Configuration.USER_HOME + separator + + "graphast" + separator + "test" +separator + "exampleTaxi"; + + GraphBounds graph = new GraphBoundsImpl(directory); + + graph.addNode(new NodeImpl(0l, 0.0d, 1.0d)); + + Node nodeCategory1 = new NodeImpl(1l, 0.0d, 10.0d); + int[] costs = new int[]{0}; + nodeCategory1.setCategory(1); + nodeCategory1.setLabel("TAXI I"); + nodeCategory1.setCosts(costs); + graph.addNode(nodeCategory1); + + graph.addNode(new NodeImpl(2l, 0.0d, 20.0d)); + graph.addNode(new NodeImpl(3l, 0.0d, 30.0d)); + + Node nodeCategory2 = new NodeImpl(4l, 0.0d, 40.0d); + costs = new int[]{0}; + nodeCategory2.setCategory(2); + nodeCategory2.setLabel("TAXI II"); + nodeCategory2.setCosts(costs); + graph.addNode(nodeCategory2); + + graph.addNode(new NodeImpl(5l, 10.0d, 0.0d)); + graph.addNode(new NodeImpl(6l, 10.0d, 10.0d)); + graph.addNode(new NodeImpl(7l, 10.0d, 20.0d)); + graph.addNode(new NodeImpl(8l, 10.0d, 30.0d)); + + Node nodeCategory3 = new NodeImpl(9l, 10.0d, 40.0d); + costs = new int[]{0}; + nodeCategory3.setCategory(3); + nodeCategory3.setLabel("TAXI III"); + nodeCategory3.setCosts(costs); + graph.addNode(nodeCategory3); + + //Edges com custo de 15 em 15 minutos + + Edge edge = new EdgeImpl(1l, 0l, 1l, 15); + int[] costsEdge1 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000,900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000,900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 1200000, + 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, 900000, 900000, + 900000, 900000, 900000}; + edge.setCosts(costsEdge1); + graph.addEdge(edge); + + edge = new EdgeImpl(2l, 1l, 2l, 15); + int[] costsEdge2 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge2); + graph.addEdge(edge); + + Edge edge1to7 = new EdgeImpl(3l, 1l, 7l, 12); + int[] costsEdge1To7 = new int[]{960000, 960000, 300000, 300000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 300000, 300000, 1800000, 720000, 720000, + 720000, 720000, 720000}; + edge1to7.setCosts(costsEdge1To7); + graph.addEdge(edge1to7); + + Edge edge7to1 = new EdgeImpl(3l, 7l, 1l, 12); + int[] costsEdge7to1 = new int[]{600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000}; + edge7to1.setCosts(costsEdge7to1); + graph.addEdge(edge7to1); + + edge = new EdgeImpl(4l, 2l, 3l, 10); + int[] costsEdge4 = new int[]{600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge4); + graph.addEdge(edge); + + edge = new EdgeImpl(5l, 3l, 4l, 12); + int[] costsEdge5 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, + 720000, 720000, 720000, 720000}; + edge.setCosts(costsEdge5); + graph.addEdge(edge); + + edge = new EdgeImpl(6l, 4l, 8l, 12); + int[] costsEdge6 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge6); + graph.addEdge(edge); + + edge = new EdgeImpl(7l, 4l, 9l, 12); + int[] costsEdge7 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1500000, 1500000, 1500000, 1500000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge7); + graph.addEdge(edge); + + edge = new EdgeImpl(8l, 5l, 0l, 12); + int[] costsEdge8 = new int[]{720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 1080000, 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, + 1080000, 600000, 600000, 600000, 600000, 600000}; + edge.setCosts(costsEdge8); + graph.addEdge(edge); + + Edge edge6to5 = new EdgeImpl(9l, 6l, 5l, 15); + int[] costsEdge6to5 = new int[]{900000, 900000, 960000, 960000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 1200000, 1200000, 1200000, 900000, 900000, 900000, 1500000, 1500000, 1500000, 1500000, + 900000, 900000, 900000, 900000, 900000}; + edge6to5.setCosts(costsEdge6to5); + graph.addEdge(edge6to5); + + Edge edge5to6 = new EdgeImpl(9l, 5l, 6l, 15); + int[] costsEdge5to6 = new int[]{600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, + 600000, 600000, 600000}; + edge5to6.setCosts(costsEdge5to6); + graph.addEdge(edge5to6); + + edge = new EdgeImpl(10l, 7l, 2l, 15); + int[] costsEdge10 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge10); + graph.addEdge(edge); + + edge = new EdgeImpl(11l, 7l, 6l, 12); + int[] costsEdge11 = new int[]{720000, 720000, 480000, 480000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 780000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 780000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 780000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 1080000, 1080000, 1080000, 600000, 600000, + 600000, 600000, 600000, 720000, 720000, 600000, 600000, 600000, 600000, 600000, 600000, 600000, 1080000, + 1080000, 1080000, 600000, 600000, 600000, 1080000, 120000, 120000, 1080000, 600000, 600000, + 600000, 600000, 600000}; + edge.setCosts(costsEdge11); + graph.addEdge(edge); + + edge = new EdgeImpl(12l, 8l, 7l, 12); + int[] costsEdge12 = new int[]{720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 720000, 1320000, + 1320000, 1320000, 720000, 720000, 720000, 1800000, 1800000, 1800000, 1800000, 720000, 720000, + 720000, 720000, 720000}; + edge.setCosts(costsEdge12); + graph.addEdge(edge); + + edge = new EdgeImpl(13l, 9l, 8l, 15); + int[] costsEdge13 = new int[]{900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, 900000, + 900000, 900000, 900000, 900000, 900000, 900000}; + edge.setCosts(costsEdge13); + graph.addEdge(edge); + + graph.createBounds(); + + return graph; + } + } \ No newline at end of file diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java new file mode 100644 index 0000000..a38b9b4 --- /dev/null +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java @@ -0,0 +1,129 @@ +package org.graphast.graphgenerator; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +import org.graphast.config.Configuration; +import org.graphast.model.Edge; +import org.graphast.model.EdgeImpl; +import org.graphast.model.GraphBounds; +import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.Node; +import org.graphast.model.NodeImpl; + +public class GraphGeneratorGrid { + + private int width; + private int length; + private GraphBounds graph; + private double percentagemPoi; + private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + + public GraphGeneratorGrid(int width, int length, double percentualPoi) { + this.width = width; + this.length = length; + this.percentagemPoi = percentualPoi; + this.graph = new GraphBoundsImpl(PATH_GRAPH); + } + + public void generateGraph() { + plotNodes(); + plotEdges(); + graph.createBounds(); + } + + private void plotNodes() { + + BigDecimal interador_largura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(width), 8, RoundingMode.HALF_UP); + BigDecimal interador_altura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(length), 8, RoundingMode.HALF_UP); + + Set listaIdsPoi = getListIdsPois(); + + Integer category = 0; + for (int i = 0; i < width; i++) { + BigDecimal latitude = interador_altura.multiply(BigDecimal.valueOf(i)).add(BigDecimal.valueOf(-90)); + for (int j = 0; j < length; j++) { + BigDecimal longitude = interador_largura.multiply(BigDecimal.valueOf(j)).add(BigDecimal.valueOf(-90));; + Node node = new NodeImpl(Long.valueOf(category), latitude.doubleValue(), longitude.doubleValue()); + if(listaIdsPoi.contains(category)) { + int[] costs = new int[]{0}; + node.setCategory(category); + node.setLabel("CATEGORY "+category); + node.setCosts(costs); + listaIdsPoi.remove(category); + } + graph.addNode(node); + category++; + } + } + } + + private Set getListIdsPois() { + + int quantidadeVerticesPoi = BigDecimal.valueOf(width).multiply(BigDecimal.valueOf(length)).multiply(BigDecimal.valueOf(percentagemPoi)).divide(BigDecimal.valueOf(100.0f), 8, RoundingMode.UP).intValue(); + + Set listIdPoi = new HashSet<>(); + do { + int rangeMax = width*length - 1; + Double idRandom = generatePdseurandom(0, rangeMax); + listIdPoi.add(idRandom.intValue()); + } while(listIdPoi.size() path = new ArrayList<>(); + path.add(4l); + assertEquals("Deve retornar o caminho esperado.", path.get(0), nearestNeighbor.getPath().get(0)); + } + + // TESTE 1.1: (EXCEPTION) Nenhum taxista é encontrado na malha, para a quantidade de tempo + // superior necessario para atendimento (11 minutos e 59 segundos) + @Test(expected = PathNotFoundException.class) + public void taxiSearchExpected_II() throws ParseException, IOException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(8); + + // Tempo para atendiemento + maxTravelTime = DateUtils.parseDate(0, 11, 59); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + } + + // TESTE 2: O cliente está em um vértice vizinho ao taxista. CLIENTE -> TAXISTA (NÃO REVERSO) + @Test + public void taxiSearchNeighbor() throws IOException, ParseException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(7); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + ArrayList path_result = new ArrayList<>(); + path_result.add(4l); + path_result.add(3l); + path_result.add(2l); + path_result.add(7l); + + assertEquals("Deve retornar o vid esperado.", 4l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 37, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // TESTE 2.1: O cliente está em um vértice vizinho ao taxista. CLIENTE -> TAXISTA (REVERSO) + @Test + public void taxiSearchNeighborReverso() throws IOException, ParseException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(7); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 12, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, custumer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolution() throws IOException, ParseException { + + //Cliente que realiza a chamada do serviço + Integer idCustomer = Integer.valueOf(5); + + //Tempo para atendiemento - 39 minutos + Date serviceTime = DateUtils.parseDate(0, 39, 0); + + //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundos + Date hourServiceTime = DateUtils.parseDate(00, 00, 00); + + graphBounds = new GraphGenerator().generateExampleTAXI(); + graphBoundsReverse = new GraphGenerator().generateExampleTAXI(); + graphBoundsReverse.reverseGraph(); + + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), serviceTime, hourServiceTime); + + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 39, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + + // TESTE 3: O cliente está em um vértice vizinho a dois taxistas. Mas com + // pesos das aresta para chegar ao cliente diferente. Cliente:8; Taxista 1: 4, 12 minutos; Taxista 1: 9, 15 minutos. + @Test + public void taxiSearchNeighborWithDifferentWeight() throws IOException, ParseException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(8); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + ArrayList path_result = new ArrayList<>(); + path_result.add(4l); + path_result.add(8l); + + assertEquals("Deve retornar o vid esperado.", 4l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 12, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // TESTE 4: O cliente está em um vértice vizinho a dois taxistas. Mas com + // pesos das arestas para chegar ao cliente iguais + @Test + public void taxiSearchNeighborWithEqualsWeight() throws IOException, ParseException { + + setUpNoRandomGraph(); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(8); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + ArrayList path_result = new ArrayList<>(); + path_result.add(4l); + path_result.add(8l); + + //Retorna o que possui o menor tempo para a travessia, a Queue realiza o compare para a ordenação. + assertEquals("Deve retornar o vid esperado.", 4l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 12, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolutionTimeForTheCallMidnight() throws IOException, ParseException { + + GraphBounds graphBoundsReverse = new GraphGenerator().generateExampleTaxi15to15minutes(); + graphBoundsReverse.reverseGraph(); + + //Tempo para atendiemento - 40 minutos + maxTravelTime = DateUtils.parseDate(00, 38, 00); + + //Hora que ele realiza a chamada do serviço - meia-noite + hourServiceTime = DateUtils.parseDate(00, 00, 00); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(5); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 28, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolutionTimeForTheCallHourServiceInit() throws IOException, ParseException { + + GraphBounds graphBoundsReverse = new GraphGenerator().generateExampleTaxi15to15minutes(); + graphBoundsReverse.reverseGraph(); + + //Tempo para atendiemento - 40 minutos + maxTravelTime = DateUtils.parseDate(2, 52, 00); + + //Hora que ele realiza a chamada do serviço - meia-noite e quinze minutos + hourServiceTime = DateUtils.parseDate(00, 15, 00); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(5); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 52, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + @Test + public void taxiBetterWaytRandomGraphOneByOne() throws ParseException { + + setUpRandomGraph(1,1,1); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(0); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + assertEquals("Deve retornar o vid esperado.", 0l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 0, nearestNeighbor.getDistance()/1000/60); + + ArrayList path = new ArrayList<>(); + path.add(0l); + assertEquals("Deve retornar o caminho esperado.", path , nearestNeighbor.getPath()); + } + + @Test + public void taxiBetterWaytRandomGraphTwoByTwo() throws ParseException { + + setUpRandomGraph(2, 2, 1); + + // Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(0); + + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + + Assert.assertNotNull(nearestNeighbor); + } + + @AfterClass + public static void tearDown() { + + FileUtils.deleteDir(Configuration.USER_HOME + "/graphhopper/test"); + FileUtils.deleteDir(Configuration.USER_HOME + "/graphast/test"); + } +} diff --git a/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java new file mode 100644 index 0000000..1bf9564 --- /dev/null +++ b/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java @@ -0,0 +1,260 @@ +package org.graphast.knn; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.graphast.config.Configuration; +import org.graphast.exception.PathNotFoundException; +import org.graphast.graphgenerator.GraphGenerator; +import org.graphast.importer.OSMDBImporter; +import org.graphast.model.Edge; +import org.graphast.model.GraphBounds; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.rnn.RNNDepthFirstSearch; +import org.graphast.query.route.shortestpath.dijkstra.Dijkstra; +import org.graphast.query.route.shortestpath.dijkstra.DijkstraLinearFunction; +import org.graphast.query.route.shortestpath.model.Path; +import org.graphast.util.DateUtils; +import org.graphast.util.FileUtils; +import org.graphast.util.NumberUtils; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class RNNDepthFirstSearchTest { + private static final String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + + private Integer idCustomer; + private Date maxTravelTime; + private Date hourServiceTime; + + @Before + public void setUp() throws ParseException, IOException { + + //Cliente que realiza a chamada do serviço + idCustomer = Integer.valueOf(5); + + //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundo + hourServiceTime = DateUtils.parseDate(00, 00, 00); + } + + @Test + public void reverteGraphTest() throws IOException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTAXI(); + GraphBounds graphBoundsReverse = new GraphGenerator().generateExampleTAXI(); + graphBoundsReverse.reverseGraph(); + + //checar quantidade de vértices que foram invertidos + assertEquals("A quantidade de Vértices esperado não corresponde com a quantidade retornada.", + 10, graphBounds.getNumberOfNodes()); + + //checar quantidade de edge que foram invertidos + assertEquals("A quantidade de Edges esperados não corresponde com a quantidade retornada.", + 13, graphBounds.getNumberOfEdges()); + + List latitudesTo = new ArrayList(); + List longitudeTo = new ArrayList(); + List idTo = new ArrayList(); + + for (int i = 0; i < graphBounds.getNumberOfEdges(); i++) { + Edge edge = graphBounds.getEdge(i); + double latitude = graphBounds.getNode(edge.getToNode()).getLatitude(); + double longitude = graphBounds.getNode(edge.getToNode()).getLongitude(); + double id = graphBounds.getNode(edge.getToNode()).getId(); + + latitudesTo.add(latitude); + longitudeTo.add(longitude); + idTo.add(id); + } + + //Reverse Graph + graphBounds.reverseGraph(); + + List latitudesFromReverse = new ArrayList(); + List longitudeFromReverse = new ArrayList(); + List idFromReverse = new ArrayList(); + + for (int i = 0; i < graphBounds.getNumberOfEdges(); i++) { + Edge edge = graphBounds.getEdge(i); + double latitude = graphBounds.getNode(edge.getFromNode()).getLatitude(); + double longitude = graphBounds.getNode(edge.getFromNode()).getLongitude(); + double id = graphBounds.getNode(edge.getFromNode()).getId(); + + latitudesFromReverse.add(latitude); + longitudeFromReverse.add(longitude); + idFromReverse.add(id); + } + + //checar quantidade de vértices que foram invertidos + assertEquals("A quantidade de Vértices esperado não corresponde com a quantidade retornada após o reverso do grafo.", + 10, graphBounds.getNumberOfNodes()); + + //checar quantidade de edge que foram invertidos + assertEquals("A quantidade de Edges esperados não corresponde com a quantidade retornada após o reverso do grafo.", + 13, graphBounds.getNumberOfEdges()); + + assertEquals(latitudesTo, latitudesFromReverse); + assertEquals(longitudeTo, longitudeFromReverse); + assertEquals(idTo, idFromReverse); + + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolution() { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTAXI(); + + //Tempo para atendiemento - 39 minutos + maxTravelTime = DateUtils.parseDate(0, 39, 00); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 39, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, custumer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + public void returnNullSolution() throws PathNotFoundException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTAXI(); + + //Tempo para atendiemento - 38 minutos + maxTravelTime = DateUtils.parseDate(0, 38, 00); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + Assert.assertNull(nearestNeighbor); + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolutionTimeForTheCallMidnight() throws PathNotFoundException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTaxi15to15minutes(); + + //Tempo para atendiemento - 40 minutos + maxTravelTime = DateUtils.parseDate(0, 40, 00); + + //Hora que ele realiza a chamada do serviço - meia-noite + hourServiceTime = DateUtils.parseDate(00, 00, 00); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 40, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 + @Test + public void returnBetterSolutionTimeForTheCallHourServiceInit() throws PathNotFoundException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTaxi15to15minutes(); + + //Tempo para atendiemento - 39 minutos + maxTravelTime = DateUtils.parseDate(0, 40, 00); + + //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundo + hourServiceTime = DateUtils.parseDate(00, 15, 00); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + + System.out.println(nearestNeighbor); + Assert.assertNotNull(nearestNeighbor); + + ArrayList path_result = new ArrayList<>(); + path_result.add(1l); + path_result.add(7l); + path_result.add(6l); + path_result.add(5l); + + assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); + assertEquals("Deve retornar o custo esperado.", 29, nearestNeighbor.getDistance()/1000/60); + assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + } + + @Test + public void baseTest() throws IOException, ParseException { + + GraphBounds graphBounds = new GraphGenerator().generateExampleTAXI(); + + Dijkstra dijkstraShortestPathLinearFunction = new DijkstraLinearFunction(graphBounds); + Path shortestPath = dijkstraShortestPathLinearFunction.shortestPath(1l, 5l); + Assert.assertEquals(39, shortestPath.getTotalCost()/1000/60, 0); + + } + + @Test + public void dbTest() { + + OSMDBImporter importer = new OSMDBImporter("view_exp_1k", PATH_GRAPH); + GraphBounds graphBounds = importer.execute(); + + RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); +// NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); + //Tempo para atendiemento - 39 minutos + maxTravelTime = DateUtils.parseDate(00, 59, 00); + //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundo + hourServiceTime = DateUtils.parseDate(00, 00, 00); + + for (int i = 0; i < 100; i++) { + + long numberOfNodes = graphBounds.getNumberOfNodes(); + long customer = Double.valueOf(NumberUtils.generatePdseurandom(0, Long.valueOf(numberOfNodes).intValue())).longValue(); + NearestNeighbor solution2 = taxiSearch.search(graphBounds.getNode(customer), maxTravelTime, hourServiceTime); + + Long idSolution2 = null; + Integer distance2 = null; + Integer size2 = null; + ArrayList path2 = null; + Long externalId2 = null; + if(solution2 != null) { + idSolution2 = solution2.getId(); + distance2 = solution2.getDistance(); + size2 = solution2.getPath().size(); + path2 = solution2.getPath(); + externalId2 = graphBounds.getNode(solution2.getId()).getExternalId(); + + } + + String.format("%s;%s;%s;%s;%s", idSolution2, externalId2, distance2, size2, path2); + } + } + + @AfterClass + public static void tearDown() { + + FileUtils.deleteDir(Configuration.USER_HOME + "/graphhopper/test"); + FileUtils.deleteDir(Configuration.USER_HOME + "/graphast/test"); + } +} From 137615b7ecd4abec8946b893081ad9ba10a48393 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:20:32 -0200 Subject: [PATCH 064/118] chance member level of the method Long getNodeId(int latitude, int longitude). chance of the proteced to public. And add method public Set getPoiIds() with IDs Pois. --- .../main/java/org/graphast/model/Graph.java | 17 +++++++-- .../java/org/graphast/model/GraphImpl.java | 35 +++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/graphast/model/Graph.java b/core/src/main/java/org/graphast/model/Graph.java index 16422cd..705d71b 100644 --- a/core/src/main/java/org/graphast/model/Graph.java +++ b/core/src/main/java/org/graphast/model/Graph.java @@ -1,7 +1,13 @@ package org.graphast.model; +import it.unimi.dsi.fastutil.ints.IntBigArrayBigList; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.longs.Long2IntMap; +import it.unimi.dsi.fastutil.longs.LongList; + import java.util.HashMap; import java.util.List; +import java.util.Set; import org.graphast.enums.CompressionType; import org.graphast.enums.TimeType; @@ -17,7 +23,6 @@ import it.unimi.dsi.fastutil.longs.Long2IntMap; import it.unimi.dsi.fastutil.longs.LongList; - public interface Graph { /** @@ -210,6 +215,14 @@ public interface Graph { * @return Id of a node */ public Long getNodeId(double latitude, double longitude); + + /** + * This method return a nodeId based on a given absolute latitude and longitude. + * @param latitude latitude that is given + * @param longitude longitude that is given + * @return Id of a node + */ + public Long getNodeId(int latitude, int longitude); /** * This method returns a label of a given node. @@ -345,6 +358,6 @@ public interface Graph { public String getAbsoluteDirectory(); public void setDirectory(String directory); - + public Set getPoiIds(); } \ No newline at end of file diff --git a/core/src/main/java/org/graphast/model/GraphImpl.java b/core/src/main/java/org/graphast/model/GraphImpl.java index 19603a5..755ed1e 100644 --- a/core/src/main/java/org/graphast/model/GraphImpl.java +++ b/core/src/main/java/org/graphast/model/GraphImpl.java @@ -2,11 +2,25 @@ import static org.graphast.util.GeoUtils.latLongToDouble; import static org.graphast.util.GeoUtils.latLongToInt; +import it.unimi.dsi.fastutil.BigArrays; +import it.unimi.dsi.fastutil.ints.IntBigArrayBigList; +import it.unimi.dsi.fastutil.ints.IntOpenHashSet; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.longs.Long2IntMap; +import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; +import it.unimi.dsi.fastutil.longs.Long2LongMap; +import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; +import it.unimi.dsi.fastutil.longs.LongArrayList; +import it.unimi.dsi.fastutil.longs.LongList; +import it.unimi.dsi.fastutil.objects.ObjectBigArrayBigList; +import it.unimi.dsi.fastutil.objects.ObjectBigList; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.graphast.enums.CompressionType; import org.graphast.enums.TimeType; @@ -39,6 +53,7 @@ import java.io.Serializable; + public class GraphImpl implements Graph, GraphBounds, Serializable { /* @@ -952,7 +967,8 @@ public List getGeometryByGeometryIndex(long geometryIndex) { return listPoints; } - Long getNodeId(int latitude, int longitude) { + @Override + public Long getNodeId(int latitude, int longitude) { Long result = nodeIndex.get(BigArrays.index(latitude, longitude)); @@ -973,7 +989,9 @@ Long getNodeId(int latitude, int longitude) { */ @Override public Long getNodeId(double latitude, double longitude) { - + if(this.getNumberOfNodes()==0) { + return null; + } int lat, lon; lat = latLongToInt(latitude); @@ -1778,4 +1796,17 @@ public void printInternalEdgeRepresentation() { } } + @Override + public Set getPoiIds() { + Set ids = new HashSet<>(); + long max = nodes.size64()/Node.NODE_BLOCKSIZE; + for (long id = 0; id= 0) { + ids.add(id); + } + } + return ids; + } } From 7c2201bc198c1c41bbacbb541cccfef7cbf82e9f Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:21:56 -0200 Subject: [PATCH 065/118] add util method double generatePdseurandom(int rangeMin, int rangeMax). Returns random values within a range. --- core/src/main/java/org/graphast/util/NumberUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/main/java/org/graphast/util/NumberUtils.java b/core/src/main/java/org/graphast/util/NumberUtils.java index f19eea8..2c1b426 100755 --- a/core/src/main/java/org/graphast/util/NumberUtils.java +++ b/core/src/main/java/org/graphast/util/NumberUtils.java @@ -2,6 +2,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.Random; import org.graphast.exception.GraphastException; @@ -113,4 +114,8 @@ public static int index( final short segment, final short displacement ) { return (short)segment << 16 | displacement & 0xFFFF; } + public static double generatePdseurandom(int rangeMin, int rangeMax) { + return rangeMin + (rangeMax - rangeMin) * new Random().nextDouble(); + } + } From a42246804a7eb87e434b0834db2229165cc97ca4 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:23:37 -0200 Subject: [PATCH 066/118] add util method boolean isPointInEdgeLine(GraphBounds graph, Node point, Edge edge). checks if a point is in a line. --- core/src/main/java/org/graphast/util/GeoUtils.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/util/GeoUtils.java b/core/src/main/java/org/graphast/util/GeoUtils.java index da91e13..58b36d7 100644 --- a/core/src/main/java/org/graphast/util/GeoUtils.java +++ b/core/src/main/java/org/graphast/util/GeoUtils.java @@ -1,5 +1,9 @@ package org.graphast.util; +import org.graphast.model.Edge; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; + public class GeoUtils { final private static double R_MAJOR = 6378137.0; @@ -88,6 +92,12 @@ public static double lat2YElliptical(double lat) { double ts = Math.tan(0.5 * ((Math.PI*0.5) - phi))/con; double y = 0 - R_MAJOR * Math.log(ts); return y; - } + } + + public static boolean isPointInEdgeLine(GraphBounds graph, Node point, Edge edge) { + Node start = graph.getNode(edge.getFromNode()); + Node end = graph.getNode(edge.getToNode()); + return DistanceUtils.distanceLatLong(start, point)+DistanceUtils.distanceLatLong(point, end)-DistanceUtils.distanceLatLong(start, end)<=0.1; + } } From 72549b3c676f173345f8f8bbdac53dfd456b2ea9 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 30 Nov 2015 11:24:11 -0200 Subject: [PATCH 067/118] set default value of category. --- core/src/main/java/org/graphast/model/NodeImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/model/NodeImpl.java b/core/src/main/java/org/graphast/model/NodeImpl.java index dd4df9f..f13104d 100644 --- a/core/src/main/java/org/graphast/model/NodeImpl.java +++ b/core/src/main/java/org/graphast/model/NodeImpl.java @@ -12,7 +12,7 @@ public class NodeImpl implements Node { private long externalId; - private int category; + private int category = -1; private int latitude; From ecb98da79a6d1aec2b9f57c8eede0265bb5b1068 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 4 Dec 2015 17:40:07 -0300 Subject: [PATCH 068/118] Added data collection in nanoseconds --- .../query/rnn/CompareRNNSearchsMethodsAnalysis.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 32d9393..601750b 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -39,7 +39,7 @@ public static void runAnalysis(String tableName, int testTimes) throws IOExcepti RNNDepthFirstSearch rnnDFS = new RNNDepthFirstSearch(graph); RNNBreadthFirstSearch rnnBFS = new RNNBreadthFirstSearch(graphReverse); - Date timeout = DateUtils.parseDate(00, 20, 00); + Date timeout = DateUtils.parseDate(00, 50, 00); Date timestamp = DateUtils.parseDate(00, 00, 00); FileWriter rnnDFSFileCsv = new FileWriter(tableName+"_rnn_dfs.csv"); @@ -58,9 +58,9 @@ public static void runAnalysis(String tableName, int testTimes) throws IOExcepti private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { try { - long startTime = System.currentTimeMillis(); + long startTime = System.nanoTime(); NearestNeighbor solution = rnn.search(customer, timeout, timestamp); - long endTime = System.currentTimeMillis(); + long endTime = System.nanoTime(); long time = endTime - startTime; @@ -74,7 +74,8 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, distance = solution.getDistance(); nodesSize = solution.getPath().size(); path = solution.getPath(); - externalId = graph.getNode(solution.getId()).getExternalId(); + //externalId = graph.getNode(solution.getId()).getExternalId(); + externalId = Integer.valueOf(graph.getNode(solution.getId()).getCategory()).longValue(); String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); String gidCustomer = customer.getLabel(); @@ -92,12 +93,11 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, gidVisited = gidVisited + "-" + nodeVisited.getLabel(); } - - String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, poiCoordinate, time, solutionId, externalId, distance, nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited) + "\n"; + System.out.println(currentLine); fileCsv.write(currentLine); From d3effc06e2eec977b0bf913756aadf9f91f88631 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 4 Dec 2015 17:40:50 -0300 Subject: [PATCH 069/118] Add test for verify cost edge --- .../graphast/importer/OSMDBImporterTest.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java b/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java index 1199cf0..413a53c 100644 --- a/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java +++ b/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java @@ -1,6 +1,7 @@ package org.graphast.importer; import org.graphast.config.Configuration; +import org.graphast.model.Edge; import org.graphast.model.GraphBounds; import org.junit.Assert; import org.junit.Test; @@ -14,9 +15,24 @@ public void createGraphTest1k() { OSMDBImporter importer = new OSMDBImporter("view_exp_1k", PATH_GRAPH); GraphBounds graph = importer.execute(); Assert.assertNotNull(graph); - Assert.assertEquals(15, graph.getCategories().size()); - Assert.assertEquals(809+15, graph.getNumberOfNodes()); - Assert.assertEquals(844+15, graph.getNumberOfEdges()); + //Assert.assertEquals(15, graph.getCategories().size()); + //Assert.assertEquals(809+15, graph.getNumberOfNodes()); + //Assert.assertEquals(844+15, graph.getNumberOfEdges()); + + Assert.assertEquals(14, graph.getCategories().size()); + Assert.assertEquals(809, graph.getNumberOfNodes()); + Assert.assertEquals(844, graph.getNumberOfEdges()); + } + + @Test + public void costEdgeTest() { + OSMDBImporter importer = new OSMDBImporter("view_exp_1k", PATH_GRAPH); + GraphBounds graphBounds = importer.execute(); + + for (int i = 0; i < graphBounds.getNumberOfEdges() - 1; i++) { + Edge edge = graphBounds.getEdge(i); + Assert.assertEquals(96, edge.getCosts().length); + } } @Test From bdbf4eece4314a4416bce58edaf7836f725de576 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 4 Dec 2015 17:41:14 -0300 Subject: [PATCH 070/118] NearestNeighbor nearestNeighbor = null; --- .../main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java index be0106a..53bf8b8 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNDepthFirstSearch.java @@ -59,9 +59,10 @@ public NearestNeighbor search(Node root, Date timeout, Date timestamp) throws Pa private NearestNeighbor createNN(Node root, long currentPoi, Path path) { - NearestNeighbor nearestNeighbor = new NearestNeighbor(); + NearestNeighbor nearestNeighbor = null; if (currentPoi > -1) { + nearestNeighbor = new NearestNeighbor(); nearestNeighbor.setDistance((int) path.getTotalCost()); nearestNeighbor.setId(currentPoi); From d59a92279a9f49ef16c2d8864c07331f767ea277 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 4 Dec 2015 17:42:12 -0300 Subject: [PATCH 071/118] define node with taxi and fixed erro cost edge --- .../java/org/graphast/importer/OSMDBImporter.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java index 00f271c..7fdb261 100644 --- a/core/src/main/java/org/graphast/importer/OSMDBImporter.java +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -84,14 +84,14 @@ private void plotNodes() throws SQLException, ClassNotFoundException, IOExceptio if (previousNode != null && !previousNode.getId().equals(node.getId())) { LOGGER.log(Level.INFO, String.format("Add edge from previous: %s to current: %s node", previousNode.getId(), node.getId())); - Edge edge = new EdgeImpl(idRoad, previousNode.getId() .longValue(), node.getId().longValue(), 0); + Edge edge = new EdgeImpl(idRoad, previousNode.getId().longValue(), node.getId().longValue(), 0, String.valueOf(idRoad)); addCost(edge); graph.addEdge(edge); if(mapTaxi.containsKey(idRoad)) { for(int i=0; i Date: Tue, 8 Dec 2015 13:30:37 -0300 Subject: [PATCH 072/118] alterado biblioteca que coloca log. --- .../org/graphast/importer/OSMDBImporter.java | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java index 7fdb261..b986bf9 100644 --- a/core/src/main/java/org/graphast/importer/OSMDBImporter.java +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -5,8 +5,6 @@ import java.sql.SQLException; import java.util.List; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import org.graphast.model.Edge; import org.graphast.model.EdgeImpl; @@ -20,6 +18,9 @@ import org.graphast.util.GeoUtils; import org.postgis.LineString; import org.postgis.Point; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class OSMDBImporter implements Importer { @@ -29,8 +30,7 @@ public class OSMDBImporter implements Importer { private final int FIELD_ID_LINESTRING = 1; private final int FIELD_LINESTRING = 2; private final int SIZE_INTERVAL = 96; - - protected static final Logger LOGGER = Logger.getGlobal(); + protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); public OSMDBImporter(String table, String directory) { this.table = table; @@ -59,7 +59,7 @@ private void plotNodes() throws SQLException, ClassNotFoundException, IOExceptio while (result.next()) { LineString lineString = new LineString(result.getString(FIELD_LINESTRING)); Point[] arrayPoints = lineString.getPoints(); - LOGGER.log(Level.INFO, String.format("registro: %s", result.getString(FIELD_LINESTRING))); + LOGGER.info(String.format("registro: %s", result.getString(FIELD_LINESTRING))); int idRoad = result.getInt(FIELD_ID_LINESTRING); @@ -67,23 +67,23 @@ private void plotNodes() throws SQLException, ClassNotFoundException, IOExceptio for (Point point : arrayPoints) { pointCount++; - LOGGER.log(Level.INFO, String.format("Point [x,y]: %s,%s", point.getX(), point.getY())); + LOGGER.info( String.format("Point [x,y]: %s,%s", point.getX(), point.getY())); Node node = new NodeImpl(point.getY(), point.getX()); node.setLabel(Long.valueOf(idRoad).toString()); Long nodeId = graph.getNodeId(GeoUtils.latLongToInt(node.getLatitude()), GeoUtils.latLongToInt(node.getLongitude())); if (nodeId != null) { - LOGGER.log(Level.INFO, String.format("point already exist in graph")); + LOGGER.info(String.format("point already exist in graph")); node = graph.getNode(nodeId); } else { graph.addNode(node); - LOGGER.log(Level.INFO, String.format("point inserted in graph with ID: %s", node.getId())); + LOGGER.info(String.format("point inserted in graph with ID: %s", node.getId())); } if (previousNode != null && !previousNode.getId().equals(node.getId())) { - LOGGER.log(Level.INFO, String.format("Add edge from previous: %s to current: %s node", previousNode.getId(), node.getId())); + LOGGER.info( String.format("Add edge from previous: %s to current: %s node", previousNode.getId(), node.getId())); Edge edge = new EdgeImpl(idRoad, previousNode.getId().longValue(), node.getId().longValue(), 0, String.valueOf(idRoad)); addCost(edge); graph.addEdge(edge); @@ -98,14 +98,14 @@ private void plotNodes() throws SQLException, ClassNotFoundException, IOExceptio } } } - LOGGER.log(Level.INFO, String.format("Graph now has %s nodes", graph.getNumberOfNodes())); - LOGGER.log(Level.INFO, String.format("Graph now has %s edges",graph.getNumberOfEdges())); + LOGGER.info(String.format("Graph now has %s nodes", graph.getNumberOfNodes())); + LOGGER.info(String.format("Graph now has %s edges",graph.getNumberOfEdges())); previousNode = node; } } - LOGGER.log(Level.INFO, String.format("Total points parsed are: %s", pointCount)); + LOGGER.info( String.format("Total points parsed are: %s", pointCount)); ConnectionJDBC.getConnection().close(); } @@ -126,8 +126,8 @@ private void defineNodeWithTaxi(Node taxi, Node node) { @Deprecated private void connectTaxiToEdge(Node taxi, Node end) { graph.addNode(taxi); - LOGGER.log(Level.INFO, String.format("Taxi %s lies in edge and now taxi is node: %s", taxi.getCategory(), taxi.getId())); - LOGGER.log(Level.INFO, String.format("Connect Taxi %s with node: %s", taxi.getCategory(), end.getId())); + LOGGER.info(String.format("Taxi %s lies in edge and now taxi is node: %s", taxi.getCategory(), taxi.getId())); + LOGGER.info(String.format("Connect Taxi %s with node: %s", taxi.getCategory(), end.getId())); Edge edge = new EdgeImpl(taxi.getId().longValue(), end.getId().longValue(), 0); addCostZero(edge); graph.addEdge(edge); @@ -135,19 +135,14 @@ private void connectTaxiToEdge(Node taxi, Node end) { private void addCost(Edge edge) { - int[] costs = new int[SIZE_INTERVAL]; - Node nodeFrom = graph.getNode(edge.getFromNode()); Node nodeTo = graph.getNode(edge.getToNode()); - double distanceBetweenLatLongHaversine = DistanceUtils.distanceLatLong(nodeFrom.getLatitude(), nodeFrom.getLongitude(), + double distanceBetweenLatLongHaversine = + DistanceUtils.distanceLatLong(nodeFrom.getLatitude(), nodeFrom.getLongitude(), nodeTo.getLatitude(), nodeTo.getLongitude()); - - for (int i = 0; i < costs.length; i++) { - costs[i] = Double.valueOf(distanceBetweenLatLongHaversine) - .intValue(); - } - edge.setCosts(costs); + int[] edgesCosts = CostGenerator.generateSyntheticEdgesCosts(Double.valueOf(distanceBetweenLatLongHaversine).intValue()); + edge.setCosts(edgesCosts); } } From d736dca028d2617f50bbfee50c4cdcf9ddc2526d Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:31:12 -0300 Subject: [PATCH 073/118] add NumberVisitedNodes --- .../GraphGeneratorGridTest.java | 49 ++++++++++--------- .../knn/RNNBreadthFirstSearchTest.java | 5 +- .../graphast/knn/RNNDepthFirstSearchTest.java | 17 ++++--- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java index 7dbe5a3..43a2057 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java @@ -1,38 +1,43 @@ package org.graphast.graphgenerator; +import org.graphast.config.Configuration; import org.graphast.model.GraphBounds; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; public class GraphGeneratorGridTest { - + + private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + @Test public void gererateGraphSynthetic2x2() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(2,2,0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 2,2,0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); - Assert.assertEquals(graph.getNumberOfNodes(), 4); - Assert.assertEquals(graph.getNumberOfEdges(), 8); + Assert.assertEquals(4, graph.getNumberOfNodes()); + Assert.assertEquals(8, graph.getNumberOfEdges()); + Assert.assertNotNull(graph.getEdgeCost(graph.getEdge(0), 0)); } @Test public void gererateGraphSynthetic4x4() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(4,4, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 4,4, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); - Assert.assertEquals(graph.getNumberOfNodes(), 16); - Assert.assertEquals(graph.getNumberOfEdges(), 48); + Assert.assertEquals(16, graph.getNumberOfNodes()); + Assert.assertEquals(48, graph.getNumberOfEdges()); + Assert.assertNotNull(graph.getEdgeCost(graph.getEdge(0), 0)); } @Test public void gererateGraphSynthetic5x5() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(5,5, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 5,5, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -43,7 +48,7 @@ public void gererateGraphSynthetic5x5() { @Test public void gererateGraphSynthetic100x100() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(100,100, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 100,100, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -51,13 +56,14 @@ public void gererateGraphSynthetic100x100() { Assert.assertEquals(graph.getNumberOfEdges(), 39600); } + @Ignore @Test public void gererateGraphSyntheticLimit() { int comprimento = 992; int altura = 992; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -65,13 +71,14 @@ public void gererateGraphSyntheticLimit() { Assert.assertEquals(graph.getNumberOfEdges(), 2*altura*(2*(comprimento-1))); } + @Ignore @Test public void gererateGraphSyntheticLimitWithPoi() { int comprimento = 992; int altura = 992; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -87,7 +94,7 @@ public void gererateGraphSyntheticDifferentSize() { int comprimento = 3; int altura = 2; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 0); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -101,7 +108,7 @@ public void gererateGraphSyntheticWithPoiShort() { int comprimento = 4; int altura = 4; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento, altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -114,7 +121,7 @@ public void gererateGraphSyntheticWithPoi() { int comprimento = 10; int altura = 10; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento, altura, 2); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, 2); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -128,7 +135,7 @@ public void gererateGraphSyntheticMin() { int altura = 1; int percentagemPoi = 1; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento, altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -139,14 +146,13 @@ public void gererateGraphSyntheticMin() { // 1k (1024 pontos) @Test - @Ignore public void gererateGraphSyntheticLimitWithPoi1k() { int comprimento = 32; int altura = 32; int qtdPoi = 10; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -157,7 +163,6 @@ public void gererateGraphSyntheticLimitWithPoi1k() { // 10k (10000 pontos) @Test - @Ignore public void gererateGraphSyntheticLimitWithPoi10k() { int comprimento = 100; @@ -165,7 +170,7 @@ public void gererateGraphSyntheticLimitWithPoi10k() { int qtdPoi = 100; int percentualPoi = 1; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentualPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentualPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -176,14 +181,13 @@ public void gererateGraphSyntheticLimitWithPoi10k() { // 100k (99856 pontos) @Test - @Ignore public void gererateGraphSyntheticLimitWithPoi100k() { int comprimento = 316; int altura = 316; int qtdPoi = 998; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -194,14 +198,13 @@ public void gererateGraphSyntheticLimitWithPoi100k() { // 1G (1000000 pontos) @Test - @Ignore public void gererateGraphSyntheticLimitWithPoi1000k() { int comprimento = 1000; int altura = 1000; int qtdPoi = 10000; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, 1); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); diff --git a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java index a938b86..cb69f67 100644 --- a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java +++ b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java @@ -28,7 +28,8 @@ public class RNNBreadthFirstSearchTest { private Integer idCustomer; private Date maxTravelTime; private Date hourServiceTime; - + private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + public void setUpNoRandomGraph() throws ParseException, IOException { // Tempo para atendiemento @@ -50,7 +51,7 @@ public void setUpRandomGraph(int qtdX, int qtdY, int percentPois) throws ParseEx // Hora que ele realiza a chamada do serviço meia-noite e vinte minutos hourServiceTime = DateUtils.parseDate(00, 00, 00); - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(qtdX, qtdY, percentPois); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, qtdX, qtdY, percentPois); graphSynthetic.generateGraph(); graphBoundsReverse = graphSynthetic.getGraph(); diff --git a/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java index 1bf9564..93c999f 100644 --- a/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java +++ b/core/src/test/java/org/graphast/knn/RNNDepthFirstSearchTest.java @@ -15,7 +15,7 @@ import org.graphast.model.Edge; import org.graphast.model.GraphBounds; import org.graphast.query.knn.NearestNeighbor; -import org.graphast.query.rnn.RNNDepthFirstSearch; +import org.graphast.query.rnn.RNNBacktrackingSearch; import org.graphast.query.route.shortestpath.dijkstra.Dijkstra; import org.graphast.query.route.shortestpath.dijkstra.DijkstraLinearFunction; import org.graphast.query.route.shortestpath.model.Path; @@ -25,6 +25,7 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; public class RNNDepthFirstSearchTest { @@ -115,7 +116,7 @@ public void returnBetterSolution() { //Tempo para atendiemento - 39 minutos maxTravelTime = DateUtils.parseDate(0, 39, 00); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); System.out.println(nearestNeighbor); @@ -130,6 +131,7 @@ public void returnBetterSolution() { assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); assertEquals("Deve retornar o custo esperado.", 39, nearestNeighbor.getDistance()/1000/60); assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + Assert.assertNotNull(nearestNeighbor.getNumberVisitedNodes()); } // Tem três taxista na malha, custumer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 @@ -140,7 +142,7 @@ public void returnNullSolution() throws PathNotFoundException { //Tempo para atendiemento - 38 minutos maxTravelTime = DateUtils.parseDate(0, 38, 00); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); Assert.assertNull(nearestNeighbor); } @@ -157,7 +159,7 @@ public void returnBetterSolutionTimeForTheCallMidnight() throws PathNotFoundExce //Hora que ele realiza a chamada do serviço - meia-noite hourServiceTime = DateUtils.parseDate(00, 00, 00); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); System.out.println(nearestNeighbor); @@ -172,6 +174,7 @@ public void returnBetterSolutionTimeForTheCallMidnight() throws PathNotFoundExce assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); assertEquals("Deve retornar o custo esperado.", 40, nearestNeighbor.getDistance()/1000/60); assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + Assert.assertNotNull(nearestNeighbor.getNumberVisitedNodes()); } // Tem três taxista na malha, customer = 5, cab 1 = 1, cab 2 = 4 e cab 3 = 9 @@ -186,7 +189,7 @@ public void returnBetterSolutionTimeForTheCallHourServiceInit() throws PathNotFo //Hora que ele realiza a chamada do serviço - meia-noite e vinte segundo hourServiceTime = DateUtils.parseDate(00, 15, 00); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); System.out.println(nearestNeighbor); @@ -201,6 +204,7 @@ public void returnBetterSolutionTimeForTheCallHourServiceInit() throws PathNotFo assertEquals("Deve retornar o vid esperado.", 1l, nearestNeighbor.getId()); assertEquals("Deve retornar o custo esperado.", 29, nearestNeighbor.getDistance()/1000/60); assertEquals("Deve retornar o caminho esperado.", path_result , nearestNeighbor.getPath()); + Assert.assertNotNull(nearestNeighbor.getNumberVisitedNodes()); } @Test @@ -214,13 +218,14 @@ public void baseTest() throws IOException, ParseException { } + @Ignore @Test public void dbTest() { OSMDBImporter importer = new OSMDBImporter("view_exp_1k", PATH_GRAPH); GraphBounds graphBounds = importer.execute(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graphBounds); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graphBounds); // NearestNeighbor nearestNeighbor = taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); //Tempo para atendiemento - 39 minutos maxTravelTime = DateUtils.parseDate(00, 59, 00); From 0d37048c82023cbd7ea61d9f45260c211a88573c Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:31:56 -0300 Subject: [PATCH 074/118] add parameter of the number visited nodes --- .../route/shortestpath/dijkstra/Dijkstra.java | 18 +++++++---- .../dijkstra/DijkstraConstantWeight.java | 3 +- .../dijkstra/DijkstraLinearFunction.java | 31 ++++++++++++------- .../query/route/shortestpath/model/Path.java | 10 ++++++ 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java index 29558f0..5c9e76c 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java @@ -54,25 +54,31 @@ protected List reconstructPath(long id, HashMap pa public Path shortestPath(Node source, Node target, Date time) { PriorityQueue queue = new PriorityQueue(); HashMap wasTraversed = new HashMap(); + List allWasViseted = new ArrayList(); HashMap parents = new HashMap(); TimeEntry removed = null; int targetId = convertToInt(target.getId()); - int t = DateUtils.dateToMilli(time); + int timeInMilli = DateUtils.dateToMilli(time); - init(source, target, queue, parents, t); + init(source, target, queue, parents, timeInMilli); - while(!queue.isEmpty()){ + while(!queue.isEmpty()) { removed = queue.poll(); - wasTraversed.put(removed.getId(), wasRemoved); + long idRemoved = removed.getId(); + wasTraversed.put(idRemoved, wasRemoved); + allWasViseted.add(idRemoved); + //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); if(removed.getId() == targetId) { Path path = new Path(); path.constructPath(removed.getId(), parents, graph); + path.setNumberVisitedNodes(allWasViseted.size()); return path; } - expandVertex(target, removed, wasTraversed, queue, parents); + expandVertex(target, removed, wasTraversed, allWasViseted, queue, parents); } + throw new PathNotFoundException("Path not found between (" + source.getLatitude() + "," + source.getLongitude() + ") and (" + target.getLatitude() + "," + target.getLongitude() + ")"); } @@ -85,7 +91,7 @@ public void init(Node source, Node target, PriorityQueue queue, } public abstract void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, - PriorityQueue queue, HashMap parents); + List wasVisited, PriorityQueue queue, HashMap parents); @Override public Path shortestPath(Node source, Node target) { diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java index c33fc6d..9efbd83 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java @@ -3,6 +3,7 @@ import it.unimi.dsi.fastutil.longs.Long2IntMap; import java.util.HashMap; +import java.util.List; import java.util.PriorityQueue; import org.graphast.model.Edge; @@ -22,7 +23,7 @@ public DijkstraConstantWeight(GraphBounds graphBounds) { super(graphBounds); } - public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, + public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, List wasVisited, PriorityQueue queue, HashMap parents){ Long2IntMap neig = graph.accessNeighborhood(graph.getNode(removed.getId())); diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java index f419a06..8bf5eb7 100755 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java @@ -23,7 +23,7 @@ import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import it.unimi.dsi.fastutil.longs.LongSet; -public class DijkstraLinearFunction extends Dijkstra{ +public class DijkstraLinearFunction extends Dijkstra { public DijkstraLinearFunction(Graph graph) { super(graph); @@ -33,15 +33,15 @@ public DijkstraLinearFunction(GraphBounds graphBounds) { super(graphBounds); } - public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, + public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, List allWasViseted, PriorityQueue queue, HashMap parents){ - HashMap neig = graph.accessNeighborhood(graph.getNode(removed.getId()), removed.getArrivalTime()); + HashMap neighbors = graph.accessNeighborhood(graph.getNode(removed.getId()), removed.getArrivalTime()); - for (Node v : neig.keySet()) { - long vid = v.getId(); - int at = graph.getArrival(removed.getArrivalTime(), neig.get(v)); - int tt = removed.getTravelTime() + neig.get(v); + for (Node node : neighbors.keySet()) { + long vid = node.getId(); + int at = graph.getArrival(removed.getArrivalTime(), neighbors.get(node)); + int tt = removed.getTravelTime() + neighbors.get(node); TimeEntry newEntry = new TimeEntry( vid, tt, at, removed.getId()); Edge edge = null; @@ -49,9 +49,12 @@ public void expandVertex(Node target, TimeEntry removed, HashMap if(!wasTraversed.containsKey(vid)){ queue.offer(newEntry); + long idNewEntry = newEntry.getId(); wasTraversed.put(newEntry.getId(), newEntry.getTravelTime()); + allWasViseted.add(idNewEntry); + //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); - distance = neig.get(v); + distance = neighbors.get(node); edge = getEdge(removed.getId(), vid, distance); parents.put(vid, new RouteEntry(removed.getId(), distance, edge.getId(), edge.getLabel())); }else{ @@ -60,11 +63,15 @@ public void expandVertex(Node target, TimeEntry removed, HashMap if(cost>newEntry.getTravelTime()){ queue.remove(newEntry); queue.offer(newEntry); - wasTraversed.remove(newEntry.getId()); - wasTraversed.put(newEntry.getId(), newEntry.getTravelTime()); + long idNewEntry = newEntry.getId(); + wasTraversed.remove(idNewEntry); + wasTraversed.put(idNewEntry, newEntry.getTravelTime()); + allWasViseted.add(idNewEntry); + //System.out.println("vertex already visited"); + //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); - parents.remove(v); - distance = neig.get(v); + parents.remove(node); + distance = neighbors.get(node); edge = getEdge(removed.getId(), vid, distance); parents.put(vid, new RouteEntry(removed.getId(), distance, edge.getId(), edge.getLabel())); } diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/model/Path.java b/core/src/main/java/org/graphast/query/route/shortestpath/model/Path.java index 396a765..2388705 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/model/Path.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/model/Path.java @@ -24,6 +24,7 @@ public class Path { private List listOfPoIs; private long totalDistance; private double totalCost; + private int numberVisitedNodes; public Path() { @@ -328,4 +329,13 @@ public double getTotalCost() { return totalCost; } + + public int getNumberVisitedNodes() { + return numberVisitedNodes; + } + + + public void setNumberVisitedNodes(int numberVisitedNodes) { + this.numberVisitedNodes = numberVisitedNodes; + } } \ No newline at end of file From e0318a00b86931679dce6b56e0981b8c8ac5c245 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:32:09 -0300 Subject: [PATCH 075/118] add cost in the graph --- .../graphgenerator/GraphGeneratorGrid.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java index a38b9b4..5beb2a3 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java @@ -6,7 +6,7 @@ import java.util.Random; import java.util.Set; -import org.graphast.config.Configuration; +import org.graphast.importer.CostGenerator; import org.graphast.model.Edge; import org.graphast.model.EdgeImpl; import org.graphast.model.GraphBounds; @@ -20,13 +20,18 @@ public class GraphGeneratorGrid { private int length; private GraphBounds graph; private double percentagemPoi; - private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + private BigDecimal distance_largura; + private BigDecimal distance_altura; - public GraphGeneratorGrid(int width, int length, double percentualPoi) { + public GraphGeneratorGrid(String pathGraph, int width, int length, double percentualPoi) { this.width = width; this.length = length; this.percentagemPoi = percentualPoi; - this.graph = new GraphBoundsImpl(PATH_GRAPH); + this.graph = new GraphBoundsImpl(pathGraph); + } + + public GraphGeneratorGrid(String pathGraph, int size, double percentualPoi) { + this(pathGraph, size, size, percentualPoi); } public void generateGraph() { @@ -39,6 +44,8 @@ private void plotNodes() { BigDecimal interador_largura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(width), 8, RoundingMode.HALF_UP); BigDecimal interador_altura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(length), 8, RoundingMode.HALF_UP); + this.distance_largura = interador_largura; + this.distance_altura = interador_altura; Set listaIdsPoi = getListIdsPois(); @@ -79,24 +86,26 @@ private Set getListIdsPois() { private void plotEdges() { for (int i = 0; i < width*length - 1; i++) { + //altura if(i < (width * length - width)) { - Edge edgeOutTopDown = new EdgeImpl(Long.valueOf(i).longValue(), Long.valueOf(i+length).longValue(), 0); + Edge edgeOutTopDown = new EdgeImpl(Long.valueOf(i).longValue(), Long.valueOf(i+length).longValue(), distance_altura.intValue()); addCost(edgeOutTopDown); graph.addEdge(edgeOutTopDown); - Edge edgeInTopDown = new EdgeImpl(Long.valueOf(i+length).longValue(), Long.valueOf(i).longValue(), 0); + Edge edgeInTopDown = new EdgeImpl(Long.valueOf(i+length).longValue(), Long.valueOf(i).longValue(), distance_altura.intValue()); addCost(edgeInTopDown); graph.addEdge(edgeInTopDown); } - + + //largura if(i%width != width - 1) { - Edge edgeOutLeftRight = new EdgeImpl(Long.valueOf(i).longValue(), Long.valueOf(i+1).longValue(), 0); + Edge edgeOutLeftRight = new EdgeImpl(Long.valueOf(i).longValue(), Long.valueOf(i+1).longValue(), distance_largura.intValue()); addCost(edgeOutLeftRight); graph.addEdge(edgeOutLeftRight); - Edge edgeInLeftRight = new EdgeImpl(Long.valueOf(i+1).longValue(), Long.valueOf(i).longValue(), 0); + Edge edgeInLeftRight = new EdgeImpl(Long.valueOf(i+1).longValue(), Long.valueOf(i).longValue(), distance_largura.intValue()); addCost(edgeInLeftRight); graph.addEdge(edgeInLeftRight); @@ -111,12 +120,8 @@ private double generatePdseurandom(int rangeMin, int rangeMax) { private void addCost(Edge edge) { - int[] costs = new int[96]; - - for (int i : costs) { - costs[i] = Double.valueOf(generatePdseurandom(1, 1200)).intValue(); - } - edge.setCosts(costs); + int[] edgesCosts = CostGenerator.generateSyntheticEdgesCosts(edge.getDistance()); + edge.setCosts(edgesCosts); } public boolean isConnected() { From 8694d97b76257fa30d7c82d53324ccf37e641219 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:32:27 -0300 Subject: [PATCH 076/118] add parameter of the number visited nodes --- .../org/graphast/query/knn/NearestNeighbor.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java b/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java index 642bc9a..3b1ab63 100755 --- a/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java +++ b/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java @@ -6,6 +6,7 @@ public class NearestNeighbor implements Comparable { private long id; private int distance; private ArrayList path; + private int numberVisitedNodes; public NearestNeighbor() {} @@ -20,6 +21,11 @@ public NearestNeighbor(long id, int distance, ArrayList path){ this.distance = distance; this.path = path; } + + public NearestNeighbor(long id, int distance, ArrayList path, int numberVisitedNodes){ + this(id, distance, path); + this.numberVisitedNodes = numberVisitedNodes; + } public long getId() { return id; @@ -46,7 +52,8 @@ public void setPath(ArrayList path) { } public String toString(){ - return "(NN:"+id+" TT: "+distance+ " Path: " + path + ")"; + return "(NN:"+id+" TT: "+distance+ " Path: " + path + + " Number Visited Nodes: " + numberVisitedNodes + ")"; } public int compareTo(NearestNeighbor o) { @@ -60,4 +67,12 @@ public int compareTo(NearestNeighbor o) { public boolean equals(NearestNeighbor o){ return((id == o.id) && (distance == o.distance)); } + + public int getNumberVisitedNodes() { + return numberVisitedNodes; + } + + public void setNumberVisitedNodes(int numberVisitedNodes) { + this.numberVisitedNodes = numberVisitedNodes; + } } From 569b9f31e0523a378e3dbc3db1596f4159c0bbff Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:32:41 -0300 Subject: [PATCH 077/118] add parameter of the number visited nodes --- .../graphast/importer/GraphGeneratorGrid.java | 136 ++++++++++++++++++ ...Search.java => RNNBacktrackingSearch.java} | 37 ++--- .../query/rnn/RNNBreadthFirstSearch.java | 16 ++- .../graphast/query/rnn/RNNComparatorTest.java | 17 +-- 4 files changed, 178 insertions(+), 28 deletions(-) create mode 100644 core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java rename core/src/main/java/org/graphast/query/rnn/{RNNDepthFirstSearch.java => RNNBacktrackingSearch.java} (62%) diff --git a/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java b/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java new file mode 100644 index 0000000..fb79957 --- /dev/null +++ b/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java @@ -0,0 +1,136 @@ +package org.graphast.importer; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +import org.graphast.importer.CostGenerator; +import org.graphast.model.Edge; +import org.graphast.model.EdgeImpl; +import org.graphast.model.GraphBounds; +import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.Node; +import org.graphast.model.NodeImpl; + +public class GraphGeneratorGrid { + + private int width; + private int length; + private GraphBounds graph; + private double percentagemPoi; + private BigDecimal distance_largura; + private BigDecimal distance_altura; + + public GraphGeneratorGrid(String pathGraph, int width, int length, double percentualPoi) { + this.width = width; + this.length = length; + this.percentagemPoi = percentualPoi; + this.graph = new GraphBoundsImpl(pathGraph); + } + + public GraphGeneratorGrid(String pathGraph, int size, double percentualPoi) { + this(pathGraph, size, size, percentualPoi); + } + + public void generateGraph() { + plotNodes(); + plotEdges(); + graph.createBounds(); + } + + private void plotNodes() { + + BigDecimal interador_largura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(width), 8, RoundingMode.HALF_UP); + BigDecimal interador_altura = BigDecimal.valueOf(180).divide(BigDecimal.valueOf(length), 8, RoundingMode.HALF_UP); + this.distance_largura = interador_largura; + this.distance_altura = interador_altura; + + Set listaIdsPoi = getListIdsPois(); + + Integer category = 0; + for (int i = 0; i < width; i++) { + BigDecimal latitude = interador_altura.multiply(BigDecimal.valueOf(i)).add(BigDecimal.valueOf(-90)); + for (int j = 0; j < length; j++) { + BigDecimal longitude = interador_largura.multiply(BigDecimal.valueOf(j)).add(BigDecimal.valueOf(-90));; + Node node = new NodeImpl(Long.valueOf(category), latitude.doubleValue(), longitude.doubleValue()); + if(listaIdsPoi.contains(category)) { + int[] costs = new int[]{0}; + node.setCategory(category); + node.setExternalId(category); + node.setLabel("CATEGORY "+category); + node.setCosts(costs); + listaIdsPoi.remove(category); + } + graph.addNode(node); + category++; + } + } + } + + private Set getListIdsPois() { + + int quantidadeVerticesPoi = BigDecimal.valueOf(width).multiply(BigDecimal.valueOf(length)).multiply(BigDecimal.valueOf(percentagemPoi)).divide(BigDecimal.valueOf(100.0f), 8, RoundingMode.UP).intValue(); + + Set listIdPoi = new HashSet<>(); + do { + int rangeMax = width*length - 1; + Double idRandom = generatePdseurandom(0, rangeMax); + listIdPoi.add(idRandom.intValue()); + } while(listIdPoi.size() -1) { + NearestNeighbor nearestNeighbor = createNN(root, currentPoi, numberVisitedNodes, pathResult); + return nearestNeighbor; } - return nearestNeighbor; + throw new PathNotFoundException( + "target not found for root and set timestamp"); } - private NearestNeighbor createNN(Node root, long currentPoi, Path path) { + private NearestNeighbor createNN(Node root, long currentPoi, int numberVisitedNodes, Path path) { - NearestNeighbor nearestNeighbor = null; - if (currentPoi > -1) { + NearestNeighbor nearestNeighbor = new NearestNeighbor(); - nearestNeighbor = new NearestNeighbor(); - nearestNeighbor.setDistance((int) path.getTotalCost()); + double totalCostInMilissegundo = path.getTotalCost(); + double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); + + nearestNeighbor.setDistance(Double.valueOf(totalCostInNanosegundos).intValue()); nearestNeighbor.setId(currentPoi); + nearestNeighbor.setNumberVisitedNodes(numberVisitedNodes); ArrayList arrayPath = new ArrayList(); List edges = path.getEdges(); @@ -77,7 +81,6 @@ private NearestNeighbor createNN(Node root, long currentPoi, Path path) { arrayPath.add(root.getId()); nearestNeighbor.setPath(arrayPath); - } return nearestNeighbor; } diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java index 1765b99..88bd941 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -27,10 +27,16 @@ public RNNBreadthFirstSearch(GraphBounds graph) { public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServiceTime) { + int numberVisitedNodes = 0; + if (graph.getPoi(customer.getId()) != null) { ArrayList path = new ArrayList(); path.add(customer.getId()); - return new NearestNeighbor(customer.getId(), 0, path); + numberVisitedNodes = numberVisitedNodes + 1; + + NearestNeighbor nearestNeighbor = new NearestNeighbor(customer.getId(), 0, path); + nearestNeighbor.setNumberVisitedNodes(numberVisitedNodes); + return nearestNeighbor; } PriorityQueue queue = new PriorityQueue(); @@ -47,6 +53,7 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi while(!queue.isEmpty()) { current = queue.poll(); + numberVisitedNodes = numberVisitedNodes + 1; if (visited.contains(current.getId())) { continue; } else { @@ -59,13 +66,17 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi } if (graph.getPoi(current.getId()) != null) { - return new NearestNeighbor(current.getId(), current.getTravelTime(), pathToTaxi(current.getId(), customer.getId(), parents)); + ArrayList pathToTaxi = pathToTaxi(current.getId(), customer.getId(), parents); + NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), current.getTravelTime(), + pathToTaxi, numberVisitedNodes); + return nearestNeighbor; } // Acessa os vizinhos do primeiro vértice da pilha, no caso os vizinho do vértice que representa o cliente. HashMap neighbors = graph.accessNeighborhood(graph.getNode(current.getId()), current.getArrivalTime()); for (Node neighbor : neighbors.keySet()) { + numberVisitedNodes = numberVisitedNodes + 1; if (visited.contains(neighbor.getId())) { continue; } @@ -85,7 +96,6 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi queue.offer(newRouteQueueTaxiEntry); } - } throw new PathNotFoundException("not found path in reverse graph"); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java b/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java index a279b47..263117a 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java @@ -20,6 +20,7 @@ public class RNNComparatorTest { private Date endServiceTime = null; private Date startServiceTime = null; private Double percentagemPoi = null; + private String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; @Before public void setUp() throws ParseException, IOException { @@ -45,13 +46,13 @@ public void taxiSearchSytenticGraph1k() throws IOException, ParseException { int comprimento = 32; int altura = 32; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); //==== SOLUÇÃO I ==== long startSolution1 = System.currentTimeMillis(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graph); NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); long endSolution1 = System.currentTimeMillis(); long timeSolution1 = endSolution1-startSolution1; @@ -79,13 +80,13 @@ public void taxiSearchSytenticGraph10k() throws IOException, ParseException { int comprimento = 100; int altura = 100; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); //==== SOLUÇÃO I ==== long startSolution1 = System.currentTimeMillis(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graph); NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); long endSolution1 = System.currentTimeMillis(); long timeSolution1 = endSolution1-startSolution1; @@ -113,7 +114,7 @@ public void taxiSearchSytenticGraph100k() throws IOException, ParseException { int comprimento = 316; int altura = 316; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -123,7 +124,7 @@ public void taxiSearchSytenticGraph100k() throws IOException, ParseException { //==== SOLUÇÃO I ==== long startSolution1 = System.currentTimeMillis(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graph); NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); long endSolution1 = System.currentTimeMillis(); long timeSolution1 = endSolution1-startSolution1; @@ -150,7 +151,7 @@ public void taxiSearchSytenticGraph1000k() throws IOException, ParseException { int comprimento = 1000; int altura = 1000; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(comprimento,altura, percentagemPoi); + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -160,7 +161,7 @@ public void taxiSearchSytenticGraph1000k() throws IOException, ParseException { //==== SOLUÇÃO I ==== long startSolution1 = System.currentTimeMillis(); - RNNDepthFirstSearch taxiSearch = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch taxiSearch = new RNNBacktrackingSearch(graph); NearestNeighbor solution1 = taxiSearch.search(graph.getNode(idCustomer), endServiceTime, startServiceTime); long endSolution1 = System.currentTimeMillis(); long timeSolution1 = endSolution1-startSolution1; From e25991726bc529df0c478b873099d721a790eb5c Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:33:13 -0300 Subject: [PATCH 078/118] create compare method synthetic analysis. --- ...areRNNSearchsMethodsSyntheticAnalysis.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java new file mode 100644 index 0000000..e8d0796 --- /dev/null +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java @@ -0,0 +1,116 @@ +package org.graphast.query.rnn; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.logging.Logger; + +import org.graphast.config.Configuration; +import org.graphast.exception.PathNotFoundException; +import org.graphast.importer.GraphGeneratorGrid; +import org.graphast.model.GraphBounds; +import org.graphast.model.Node; +import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.DateUtils; +import org.graphast.util.NumberUtils; + +public class CompareRNNSearchsMethodsSyntheticAnalysis { + + private static final String PATH_GRAPH = Configuration.USER_HOME + "/graphast/test/example"; + protected static final Logger LOGGER = Logger.getGlobal(); + + public static void main(String[] args) throws IOException { + + runAnalysis("1k", 32, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + runAnalysis("10k", 100, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + runAnalysis("100k", 316 , Integer.parseInt(args[0]), Integer.parseInt(args[1])); + runAnalysis("1000k", 1000, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + } + + public static void runAnalysis(String experimentName, int side, int percentagemPoi, int testTimes) throws IOException { + + GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH+experimentName, side, percentagemPoi); + graphSynthetic.generateGraph(); + GraphBounds graph = graphSynthetic.getGraph(); + + GraphGeneratorGrid graphSyntheticReverse = new GraphGeneratorGrid(PATH_GRAPH+experimentName, side, percentagemPoi); + graphSyntheticReverse.generateGraph(); + GraphBounds graphReverse = graphSynthetic.getGraph(); + + + RNNBacktrackingSearch rnnDFS = new RNNBacktrackingSearch(graph); + RNNBreadthFirstSearch rnnBFS = new RNNBreadthFirstSearch(graphReverse); + + Date timeout = DateUtils.parseDate(00, 50, 00); + Date timestamp = DateUtils.parseDate(00, 00, 00); + + FileWriter rnnDFSFileCsv = new FileWriter(experimentName+"_"+percentagemPoi+"_rnn_dfs_baseline.csv"); + FileWriter rnnBFSFileCsv = new FileWriter(experimentName+"_"+percentagemPoi+"_rnn_bfs_solution.csv"); + + for (int i = 0; i < testTimes; i++) { + Node customer = getRandomCustomerInGraph(graph); + runSearchAndWrite(graph, rnnDFS, customer, timeout, timestamp, rnnDFSFileCsv); + runSearchAndWrite(graph, rnnBFS, customer, timeout, timestamp, rnnBFSFileCsv); + } + + rnnBFSFileCsv.close(); + rnnDFSFileCsv.close(); + } + + private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, + Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { + try { + long startTime = System.nanoTime(); + NearestNeighbor solution = rnn.search(customer, timeout, timestamp); + long endTime = System.nanoTime(); + + long time = endTime - startTime; + + Long solutionId = null; + Integer distance = null; + Integer nodesSize = null; + ArrayList path = null; + int numberVisitedNodes = 0; + if(solution != null && solution.getPath()!=null) { + solutionId = solution.getId(); + distance = solution.getDistance(); + nodesSize = solution.getPath().size(); + path = solution.getPath(); + numberVisitedNodes = solution.getNumberVisitedNodes(); + + String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); + + Node nodePoi = graph.getNode(solutionId); + String poiCoordinate = nodePoi.getLongitude() + "," + nodePoi.getLatitude(); + String gidPoi = nodePoi.getLabel(); + + String coordinateNodeVisited = ""; + for (Long visited : path) { + Node nodeVisited = graph.getNode(visited); + coordinateNodeVisited = coordinateNodeVisited + "(" + nodeVisited.getLongitude() + "," + nodeVisited.getLatitude() + ")"; + } + + String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, + poiCoordinate, time, solutionId, distance, nodesSize, path, coordinateNodeVisited, gidPoi, numberVisitedNodes) + "\n"; + + + System.out.println(currentLine); + + fileCsv.write(currentLine); + } + } catch(PathNotFoundException e) { + System.err.println(String.format("Customer %s (%s, %s) has no POI in subgraph", customer.getId(), customer.getLatitude(), customer.getLongitude())); + } + } + + + private static Node getRandomCustomerInGraph(GraphBounds graph) { + Node node; + do { + long id = Double.valueOf(NumberUtils.generatePdseurandom(0, Long.valueOf(graph.getNumberOfNodes()-1).intValue())).longValue(); + node = graph.getNode(id); + } while(node.getCategory()!=-1); + return node; + } +} From 30caf61c841cb84469768327873e45cee3f5bd58 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:33:26 -0300 Subject: [PATCH 079/118] add parameter of the number visited nodes --- .../query/rnn/CompareRNNSearchsMethodsAnalysis.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 601750b..1258eff 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -36,23 +36,24 @@ public static void runAnalysis(String tableName, int testTimes) throws IOExcepti GraphBounds graphReverse = importerReverse.execute(); - RNNDepthFirstSearch rnnDFS = new RNNDepthFirstSearch(graph); + RNNBacktrackingSearch rnnDFS = new RNNBacktrackingSearch(graph); RNNBreadthFirstSearch rnnBFS = new RNNBreadthFirstSearch(graphReverse); Date timeout = DateUtils.parseDate(00, 50, 00); Date timestamp = DateUtils.parseDate(00, 00, 00); - FileWriter rnnDFSFileCsv = new FileWriter(tableName+"_rnn_dfs.csv"); - FileWriter rnnBFSFileCsv = new FileWriter(tableName+"_rnn_bfs.csv"); + FileWriter rnnBacktrackingFileCsv = new FileWriter(tableName+"_rnn_baseline.csv"); + FileWriter rnnBFSFileCsv = new FileWriter(tableName+"_rnn_bfs_proposed_solution.csv"); for (int i = 0; i < testTimes; i++) { Node customer = getRandomCustomerInGraph(graph); - runSearchAndWrite(graph, rnnDFS, customer, timeout, timestamp, rnnDFSFileCsv); + runSearchAndWrite(graph, rnnDFS, customer, timeout, timestamp, rnnBacktrackingFileCsv); runSearchAndWrite(graph, rnnBFS, customer, timeout, timestamp, rnnBFSFileCsv); } + rnnBacktrackingFileCsv.close(); rnnBFSFileCsv.close(); - rnnDFSFileCsv.close(); + } private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, @@ -74,7 +75,6 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, distance = solution.getDistance(); nodesSize = solution.getPath().size(); path = solution.getPath(); - //externalId = graph.getNode(solution.getId()).getExternalId(); externalId = Integer.valueOf(graph.getNode(solution.getId()).getCategory()).longValue(); String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); From d226e520ae90b7e1b9069d22660996d3d2176b94 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 13:40:52 -0300 Subject: [PATCH 080/118] Milissegundo to Nanosegundos --- .../org/graphast/query/rnn/RNNBreadthFirstSearch.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java index 88bd941..5f707a7 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -66,9 +66,13 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi } if (graph.getPoi(current.getId()) != null) { + + double totalCostInMilissegundo = current.getTravelTime(); + double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); ArrayList pathToTaxi = pathToTaxi(current.getId(), customer.getId(), parents); - NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), current.getTravelTime(), - pathToTaxi, numberVisitedNodes); + + NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), + Double.valueOf(totalCostInNanosegundos).intValue(), pathToTaxi, numberVisitedNodes); return nearestNeighbor; } From f037ac94cc01ba495ff4ef2872476ca79489eb62 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 17:17:14 -0300 Subject: [PATCH 081/118] add travel time. --- .../org/graphast/query/knn/NearestNeighbor.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java b/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java index 3b1ab63..a4cd720 100755 --- a/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java +++ b/core/src/main/java/org/graphast/query/knn/NearestNeighbor.java @@ -5,6 +5,7 @@ public class NearestNeighbor implements Comparable { private long id; private int distance; + private double travelTime; private ArrayList path; private int numberVisitedNodes; @@ -26,6 +27,14 @@ public NearestNeighbor(long id, int distance, ArrayList path, int numberVi this(id, distance, path); this.numberVisitedNodes = numberVisitedNodes; } + + public NearestNeighbor(long id, double travelTime, ArrayList path, int numberVisitedNodes){ + this.id = id; + this.path = path; + this.travelTime = travelTime; + this.numberVisitedNodes = numberVisitedNodes; + } + public long getId() { return id; @@ -75,4 +84,12 @@ public int getNumberVisitedNodes() { public void setNumberVisitedNodes(int numberVisitedNodes) { this.numberVisitedNodes = numberVisitedNodes; } + + public double getTravelTime() { + return travelTime; + } + + public void setTravelTime(double travelTime) { + this.travelTime = travelTime; + } } From a3e723aa9bd174e5cdb7fe336fe562b4a844ee12 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 8 Dec 2015 17:18:18 -0300 Subject: [PATCH 082/118] add travel time. --- .../org/graphast/importer/CostGenerator.java | 9 +++--- .../org/graphast/importer/OSMDBImporter.java | 4 ++- .../rnn/CompareRNNSearchsMethodsAnalysis.java | 18 ++++++------ ...areRNNSearchsMethodsSyntheticAnalysis.java | 6 ++-- .../query/rnn/RNNBacktrackingSearch.java | 2 +- .../query/rnn/RNNBreadthFirstSearch.java | 2 +- .../GraphGeneratorGridTest.java | 28 +++++++++---------- ...rid.java => GraphGeneratorGridTester.java} | 6 ++-- .../knn/RNNBreadthFirstSearchTest.java | 4 +-- .../org/graphast/knn}/RNNComparatorTest.java | 14 ++++++---- 10 files changed, 50 insertions(+), 43 deletions(-) rename core/src/test/java/org/graphast/graphgenerator/{GraphGeneratorGrid.java => GraphGeneratorGridTester.java} (94%) rename core/src/{main/java/org/graphast/query/rnn => test/java/org/graphast/knn}/RNNComparatorTest.java (91%) diff --git a/core/src/main/java/org/graphast/importer/CostGenerator.java b/core/src/main/java/org/graphast/importer/CostGenerator.java index 710b5af..e580a91 100644 --- a/core/src/main/java/org/graphast/importer/CostGenerator.java +++ b/core/src/main/java/org/graphast/importer/CostGenerator.java @@ -4,18 +4,19 @@ import org.graphast.model.Graph; + public class CostGenerator { public static int[] generateSyntheticEdgesCosts(int distance) { Random random = new Random(); - int minSpeed, maxSpeed; + int minSpeed, maxSpeed; //Millimeters Per Millisecond (mm/ms) int[] syntheticCosts = new int[96]; for(int i=0; i<24; i++) { - minSpeed = 14; - maxSpeed = 17; + minSpeed = 14; // 50km/h + maxSpeed = 17; // 60km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); @@ -46,7 +47,7 @@ public static int[] generateSyntheticEdgesCosts(int distance) { } for(int i=44; i<56; i++) { - minSpeed = 1; + minSpeed = 1; //3km/h maxSpeed = 4; syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java index b986bf9..c3e23d1 100644 --- a/core/src/main/java/org/graphast/importer/OSMDBImporter.java +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -142,7 +142,9 @@ private void addCost(Edge edge) { DistanceUtils.distanceLatLong(nodeFrom.getLatitude(), nodeFrom.getLongitude(), nodeTo.getLatitude(), nodeTo.getLongitude()); - int[] edgesCosts = CostGenerator.generateSyntheticEdgesCosts(Double.valueOf(distanceBetweenLatLongHaversine).intValue()); + double distanceBetweenLatLongHaversineInMill = distanceBetweenLatLongHaversine * 1000; + + int[] edgesCosts = CostGenerator.generateSyntheticEdgesCosts(Double.valueOf(distanceBetweenLatLongHaversineInMill).intValue()); edge.setCosts(edgesCosts); } } diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 1258eff..9b92909 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -22,9 +22,9 @@ public class CompareRNNSearchsMethodsAnalysis { public static void main(String[] args) throws IOException { runAnalysis("view_exp_1k", Integer.parseInt(args[0])); - runAnalysis("view_exp_10k", Integer.parseInt(args[0])); - runAnalysis("view_exp_100k", Integer.parseInt(args[0])); - runAnalysis("view_exp_300mil", Integer.parseInt(args[0])); + /*runAnalysis("view_exp_10k", Integer.parseInt(args[0])); + runAnalysis("view_exp_50k", Integer.parseInt(args[0])); + runAnalysis("view_exp_100k", Integer.parseInt(args[0]));*/ } public static void runAnalysis(String tableName, int testTimes) throws IOException { @@ -66,16 +66,18 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, long time = endTime - startTime; Long solutionId = null; - Integer distance = null; + Double travelTime = null; Integer nodesSize = null; ArrayList path = null; Long externalId = null; + int numberVisitedNodes = 0; if(solution != null && solution.getPath()!=null) { solutionId = solution.getId(); - distance = solution.getDistance(); + travelTime = solution.getTravelTime(); nodesSize = solution.getPath().size(); path = solution.getPath(); externalId = Integer.valueOf(graph.getNode(solution.getId()).getCategory()).longValue(); + numberVisitedNodes = solution.getNumberVisitedNodes(); String coordinatesCustomer = customer.getLongitude() + "," + customer.getLatitude(); String gidCustomer = customer.getLabel(); @@ -93,9 +95,9 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, gidVisited = gidVisited + "-" + nodeVisited.getLabel(); } - String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, - poiCoordinate, time, solutionId, externalId, distance, - nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited) + "\n"; + String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, + poiCoordinate, time, solutionId, externalId, travelTime, + nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited, numberVisitedNodes) + "\n"; System.out.println(currentLine); diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java index e8d0796..cd05aeb 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java @@ -68,13 +68,13 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, long time = endTime - startTime; Long solutionId = null; - Integer distance = null; + Double travelTime = null; Integer nodesSize = null; ArrayList path = null; int numberVisitedNodes = 0; if(solution != null && solution.getPath()!=null) { solutionId = solution.getId(); - distance = solution.getDistance(); + travelTime = solution.getTravelTime(); nodesSize = solution.getPath().size(); path = solution.getPath(); numberVisitedNodes = solution.getNumberVisitedNodes(); @@ -92,7 +92,7 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, } String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, - poiCoordinate, time, solutionId, distance, nodesSize, path, coordinateNodeVisited, gidPoi, numberVisitedNodes) + "\n"; + poiCoordinate, time, solutionId, travelTime, nodesSize, path, coordinateNodeVisited, gidPoi, numberVisitedNodes) + "\n"; System.out.println(currentLine); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java index 07cfcba..e62b0b2 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java @@ -66,7 +66,7 @@ private NearestNeighbor createNN(Node root, long currentPoi, int numberVisitedNo double totalCostInMilissegundo = path.getTotalCost(); double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); - nearestNeighbor.setDistance(Double.valueOf(totalCostInNanosegundos).intValue()); + nearestNeighbor.setTravelTime(Double.valueOf(totalCostInNanosegundos)); nearestNeighbor.setId(currentPoi); nearestNeighbor.setNumberVisitedNodes(numberVisitedNodes); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java index 5f707a7..6eccc3d 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -72,7 +72,7 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi ArrayList pathToTaxi = pathToTaxi(current.getId(), customer.getId(), parents); NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), - Double.valueOf(totalCostInNanosegundos).intValue(), pathToTaxi, numberVisitedNodes); + Double.valueOf(totalCostInNanosegundos), pathToTaxi, numberVisitedNodes); return nearestNeighbor; } diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java index 43a2057..e6405be 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTest.java @@ -13,7 +13,7 @@ public class GraphGeneratorGridTest { @Test public void gererateGraphSynthetic2x2() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 2,2,0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, 2,2,0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -25,7 +25,7 @@ public void gererateGraphSynthetic2x2() { @Test public void gererateGraphSynthetic4x4() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 4,4, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, 4,4, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -37,7 +37,7 @@ public void gererateGraphSynthetic4x4() { @Test public void gererateGraphSynthetic5x5() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 5,5, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, 5,5, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -48,7 +48,7 @@ public void gererateGraphSynthetic5x5() { @Test public void gererateGraphSynthetic100x100() { - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, 100,100, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, 100,100, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -63,7 +63,7 @@ public void gererateGraphSyntheticLimit() { int comprimento = 992; int altura = 992; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -78,7 +78,7 @@ public void gererateGraphSyntheticLimitWithPoi() { int comprimento = 992; int altura = 992; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -94,7 +94,7 @@ public void gererateGraphSyntheticDifferentSize() { int comprimento = 3; int altura = 2; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 0); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 0); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -108,7 +108,7 @@ public void gererateGraphSyntheticWithPoiShort() { int comprimento = 4; int altura = 4; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento, altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -121,7 +121,7 @@ public void gererateGraphSyntheticWithPoi() { int comprimento = 10; int altura = 10; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, 2); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento, altura, 2); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -135,7 +135,7 @@ public void gererateGraphSyntheticMin() { int altura = 1; int percentagemPoi = 1; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento, altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento, altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -152,7 +152,7 @@ public void gererateGraphSyntheticLimitWithPoi1k() { int altura = 32; int qtdPoi = 10; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -170,7 +170,7 @@ public void gererateGraphSyntheticLimitWithPoi10k() { int qtdPoi = 100; int percentualPoi = 1; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentualPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentualPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -187,7 +187,7 @@ public void gererateGraphSyntheticLimitWithPoi100k() { int altura = 316; int qtdPoi = 998; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -204,7 +204,7 @@ public void gererateGraphSyntheticLimitWithPoi1000k() { int altura = 1000; int qtdPoi = 10000; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, 1); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, 1); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java similarity index 94% rename from core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java rename to core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java index 5beb2a3..013d748 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGrid.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java @@ -14,7 +14,7 @@ import org.graphast.model.Node; import org.graphast.model.NodeImpl; -public class GraphGeneratorGrid { +public class GraphGeneratorGridTester { private int width; private int length; @@ -23,14 +23,14 @@ public class GraphGeneratorGrid { private BigDecimal distance_largura; private BigDecimal distance_altura; - public GraphGeneratorGrid(String pathGraph, int width, int length, double percentualPoi) { + public GraphGeneratorGridTester(String pathGraph, int width, int length, double percentualPoi) { this.width = width; this.length = length; this.percentagemPoi = percentualPoi; this.graph = new GraphBoundsImpl(pathGraph); } - public GraphGeneratorGrid(String pathGraph, int size, double percentualPoi) { + public GraphGeneratorGridTester(String pathGraph, int size, double percentualPoi) { this(pathGraph, size, size, percentualPoi); } diff --git a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java index cb69f67..f62b972 100644 --- a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java +++ b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java @@ -10,7 +10,7 @@ import org.graphast.config.Configuration; import org.graphast.exception.PathNotFoundException; import org.graphast.graphgenerator.GraphGenerator; -import org.graphast.graphgenerator.GraphGeneratorGrid; +import org.graphast.graphgenerator.GraphGeneratorGridTester; import org.graphast.model.GraphBounds; import org.graphast.query.knn.NearestNeighbor; import org.graphast.query.rnn.RNNBreadthFirstSearch; @@ -51,7 +51,7 @@ public void setUpRandomGraph(int qtdX, int qtdY, int percentPois) throws ParseEx // Hora que ele realiza a chamada do serviço meia-noite e vinte minutos hourServiceTime = DateUtils.parseDate(00, 00, 00); - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, qtdX, qtdY, percentPois); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, qtdX, qtdY, percentPois); graphSynthetic.generateGraph(); graphBoundsReverse = graphSynthetic.getGraph(); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java b/core/src/test/java/org/graphast/knn/RNNComparatorTest.java similarity index 91% rename from core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java rename to core/src/test/java/org/graphast/knn/RNNComparatorTest.java index 263117a..29e22dd 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNComparatorTest.java +++ b/core/src/test/java/org/graphast/knn/RNNComparatorTest.java @@ -1,13 +1,15 @@ -package org.graphast.query.rnn; +package org.graphast.knn; import java.io.IOException; import java.text.ParseException; import java.util.Date; import org.graphast.config.Configuration; -import org.graphast.graphgenerator.GraphGeneratorGrid; +import org.graphast.graphgenerator.GraphGeneratorGridTester; import org.graphast.model.GraphBounds; import org.graphast.query.knn.NearestNeighbor; +import org.graphast.query.rnn.RNNBacktrackingSearch; +import org.graphast.query.rnn.RNNBreadthFirstSearch; import org.graphast.util.DateUtils; import org.graphast.util.FileUtils; import org.junit.AfterClass; @@ -46,7 +48,7 @@ public void taxiSearchSytenticGraph1k() throws IOException, ParseException { int comprimento = 32; int altura = 32; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -80,7 +82,7 @@ public void taxiSearchSytenticGraph10k() throws IOException, ParseException { int comprimento = 100; int altura = 100; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -114,7 +116,7 @@ public void taxiSearchSytenticGraph100k() throws IOException, ParseException { int comprimento = 316; int altura = 316; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); @@ -151,7 +153,7 @@ public void taxiSearchSytenticGraph1000k() throws IOException, ParseException { int comprimento = 1000; int altura = 1000; - GraphGeneratorGrid graphSynthetic = new GraphGeneratorGrid(PATH_GRAPH, comprimento,altura, percentagemPoi); + GraphGeneratorGridTester graphSynthetic = new GraphGeneratorGridTester(PATH_GRAPH, comprimento,altura, percentagemPoi); graphSynthetic.generateGraph(); GraphBounds graph = graphSynthetic.getGraph(); From 09bf2fabda91ebc27cfff61bf751f05b6ea5ef48 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 15 Dec 2015 07:22:22 -0300 Subject: [PATCH 083/118] logger --- .../org/graphast/importer/CostGenerator.java | 44 +++++++----- core/src/main/resources/log4j.properties | 4 +- .../graphast/importer/OSMDBImporterTest.java | 67 ++++++++++++++++--- 3 files changed, 88 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/org/graphast/importer/CostGenerator.java b/core/src/main/java/org/graphast/importer/CostGenerator.java index e580a91..6667212 100644 --- a/core/src/main/java/org/graphast/importer/CostGenerator.java +++ b/core/src/main/java/org/graphast/importer/CostGenerator.java @@ -14,6 +14,7 @@ public static int[] generateSyntheticEdgesCosts(int distance) { int minSpeed, maxSpeed; //Millimeters Per Millisecond (mm/ms) int[] syntheticCosts = new int[96]; + //1:00h to 6:00h for(int i=0; i<24; i++) { minSpeed = 14; // 50km/h maxSpeed = 17; // 60km/h @@ -22,73 +23,82 @@ public static int[] generateSyntheticEdgesCosts(int distance) { } + //6:00h to 7:00h for(int i=24; i<28; i++) { - minSpeed = 6; - maxSpeed = 9; + minSpeed = 6; //21km/h + maxSpeed = 9; //32km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //7:00h to 9:00h for(int i=28; i<36; i++) { - minSpeed = 1; - maxSpeed = 4; + minSpeed = 1; //3km/h + maxSpeed = 4; //14km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //9:00h to 11:00h for(int i=36; i<44; i++) { - minSpeed = 6; - maxSpeed = 9; + minSpeed = 6; //21km/h + maxSpeed = 9; //32km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //11:00h to 14:00h for(int i=44; i<56; i++) { minSpeed = 1; //3km/h - maxSpeed = 4; + maxSpeed = 4; //14km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //14:00h to 16:00h for(int i=56; i<64; i++) { - minSpeed = 14; - maxSpeed = 17; + minSpeed = 14; //50km/h + maxSpeed = 17; //60km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //16:00h to 17:00h for(int i=64; i<68; i++) { - minSpeed = 6; - maxSpeed = 9; + minSpeed = 6; //21km/h + maxSpeed = 9; //30/km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //17:00h to 20:00h for(int i=68; i<80; i++) { - minSpeed = 1; - maxSpeed = 4; + minSpeed = 1; //3km/h + maxSpeed = 4; //14km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //20:00h to 22:00h for(int i=80; i<88; i++) { - minSpeed = 6; - maxSpeed = 9; + minSpeed = 6; //21km/h + maxSpeed = 9; //30/km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); } + //22:00h to 00:00h for(int i=88; i<96; i++) { - minSpeed = 14; - maxSpeed = 17; + minSpeed = 14; //50km/h + maxSpeed = 17; //60km/h syntheticCosts[i] = distance/(random.nextInt(maxSpeed-minSpeed)+minSpeed); diff --git a/core/src/main/resources/log4j.properties b/core/src/main/resources/log4j.properties index 31249d0..0fa3f73 100755 --- a/core/src/main/resources/log4j.properties +++ b/core/src/main/resources/log4j.properties @@ -24,4 +24,6 @@ log4j.category.org.apache=INFO log4j.category.br.com.nex2me.mobme.network.road.model.RoadGraphAdapter=INFO log4j.category.br.com.nex2me.mobme.core.model.GraphAdapter=INFO log4j.category.com.thinkaurelius.titan=ERROR -log4j.category.org.apache.cassandra=ERROR \ No newline at end of file +log4j.category.org.apache.cassandra=ERROR + +log4j.category.org.graphast.importer.OSMDBImporter=ERROR \ No newline at end of file diff --git a/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java b/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java index 413a53c..df92fca 100644 --- a/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java +++ b/core/src/test/java/org/graphast/importer/OSMDBImporterTest.java @@ -1,8 +1,13 @@ package org.graphast.importer; +import java.util.HashMap; + +import it.unimi.dsi.fastutil.longs.Long2IntMap; + import org.graphast.config.Configuration; import org.graphast.model.Edge; import org.graphast.model.GraphBounds; +import org.graphast.model.Node; import org.junit.Assert; import org.junit.Test; @@ -19,9 +24,18 @@ public void createGraphTest1k() { //Assert.assertEquals(809+15, graph.getNumberOfNodes()); //Assert.assertEquals(844+15, graph.getNumberOfEdges()); - Assert.assertEquals(14, graph.getCategories().size()); - Assert.assertEquals(809, graph.getNumberOfNodes()); - Assert.assertEquals(844, graph.getNumberOfEdges()); + //Assert.assertEquals(14, graph.getCategories().size()); + //Assert.assertEquals(809, graph.getNumberOfNodes()); + //Assert.assertEquals(844, graph.getNumberOfEdges()); + + for (int i = 0; i < graph.getNumberOfNodes(); i++) { + Node node = graph.getNode(i); + HashMap accessNeighborhood = graph.accessNeighborhood(node, 0); + int size = accessNeighborhood.size(); + //System.out.println(String.format("the node %s contens %s neighbors", i, size)); + System.out.println(String.format("%s,%s", i, size)); + + } } @Test @@ -41,9 +55,18 @@ public void createGraphTest10k() { OSMDBImporter importer = new OSMDBImporter("view_exp_10k", PATH_GRAPH); GraphBounds graph = importer.execute(); Assert.assertNotNull(graph); - Assert.assertEquals(77, graph.getCategories().size()); - Assert.assertEquals(6947, graph.getNumberOfNodes()); - Assert.assertEquals(8411, graph.getNumberOfEdges()); + Assert.assertEquals(73, graph.getCategories().size()); //77 + Assert.assertEquals(6870, graph.getNumberOfNodes()); //6947 + Assert.assertEquals(8334, graph.getNumberOfEdges()); // + + for (int i = 0; i < graph.getNumberOfNodes(); i++) { + Node node = graph.getNode(i); + HashMap accessNeighborhood = graph.accessNeighborhood(node, 0); + int size = accessNeighborhood.size(); + //System.out.println(String.format("the node %s contens %s neighbors", i, size)); + System.out.println(String.format("%s,%s", i, size)); + + } } @Test @@ -51,9 +74,35 @@ public void createGraphTest100k() { OSMDBImporter importer = new OSMDBImporter("view_exp_100k", PATH_GRAPH); GraphBounds graph = importer.execute(); Assert.assertNotNull(graph); - Assert.assertEquals(425, graph.getCategories().size()); - Assert.assertEquals(75919, graph.getNumberOfNodes()); - Assert.assertEquals(98653, graph.getNumberOfEdges()); + Assert.assertEquals(379, graph.getCategories().size());//379 + Assert.assertEquals(75489, graph.getNumberOfNodes()); //75919 + Assert.assertEquals(98223, graph.getNumberOfEdges()); //98653 + + for (int i = 0; i < graph.getNumberOfNodes(); i++) { + Node node = graph.getNode(i); + HashMap accessNeighborhood = graph.accessNeighborhood(node, 0); + int size = accessNeighborhood.size(); + System.out.println(String.format("%s,%s", i, size)); + + } + } + + @Test + public void createGraphTest50k() { + OSMDBImporter importer = new OSMDBImporter("view_exp_50k", PATH_GRAPH); + GraphBounds graph = importer.execute(); + Assert.assertNotNull(graph); + Assert.assertEquals(328, graph.getCategories().size()); // 425 + Assert.assertEquals(33595, graph.getNumberOfNodes()); // 236216 + Assert.assertEquals(42385, graph.getNumberOfEdges()); + + for (int i = 0; i < graph.getNumberOfNodes(); i++) { + Node node = graph.getNode(i); + HashMap accessNeighborhood = graph.accessNeighborhood(node, 0); + int size = accessNeighborhood.size(); + System.out.println(String.format("%s,%s", i, size)); + + } } @Test From 31da47856736219a00f567d7c302312081dba0a8 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 15 Jan 2016 10:51:43 -0300 Subject: [PATCH 084/118] Generalizado forma de passar o tamanho da rede. Por parametro. --- ...areRNNSearchsMethodsSyntheticAnalysis.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java index cd05aeb..ef02bd3 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsSyntheticAnalysis.java @@ -22,10 +22,25 @@ public class CompareRNNSearchsMethodsSyntheticAnalysis { public static void main(String[] args) throws IOException { - runAnalysis("1k", 32, Integer.parseInt(args[0]), Integer.parseInt(args[1])); - runAnalysis("10k", 100, Integer.parseInt(args[0]), Integer.parseInt(args[1])); - runAnalysis("100k", 316 , Integer.parseInt(args[0]), Integer.parseInt(args[1])); - runAnalysis("1000k", 1000, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + int side = 0; + switch (args[0]) { + case "1k": + side = 32; + break; + case "10k": + side = 100; + break; + case "100k": + side = 316; + break; + case "1000k": + side = 1000; + break; + default: + break; + } + + runAnalysis(args[0], side, Integer.parseInt(args[1]), Integer.parseInt(args[2])); } public static void runAnalysis(String experimentName, int side, int percentagemPoi, int testTimes) throws IOException { From f64919b28736c1b50c08d8c99db4bf269cdf8cf2 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 15 Jan 2016 10:52:44 -0300 Subject: [PATCH 085/118] =?UTF-8?q?Alterado=20forma=20de=20capturar=20quan?= =?UTF-8?q?tidade=20de=20n=C3=B3s=20visitados.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../query/route/shortestpath/dijkstra/Dijkstra.java | 9 +++++---- .../shortestpath/dijkstra/DijkstraConstantWeight.java | 4 ++-- .../shortestpath/dijkstra/DijkstraLinearFunction.java | 9 ++------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java index 5c9e76c..baa3282 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/Dijkstra.java @@ -6,8 +6,10 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; +import java.util.Set; import org.graphast.exception.PathNotFoundException; import org.graphast.model.Graph; @@ -54,20 +56,19 @@ protected List reconstructPath(long id, HashMap pa public Path shortestPath(Node source, Node target, Date time) { PriorityQueue queue = new PriorityQueue(); HashMap wasTraversed = new HashMap(); - List allWasViseted = new ArrayList(); + Set allWasViseted = new HashSet(); HashMap parents = new HashMap(); TimeEntry removed = null; int targetId = convertToInt(target.getId()); int timeInMilli = DateUtils.dateToMilli(time); init(source, target, queue, parents, timeInMilli); + allWasViseted.add(source.getId()); while(!queue.isEmpty()) { removed = queue.poll(); long idRemoved = removed.getId(); wasTraversed.put(idRemoved, wasRemoved); - allWasViseted.add(idRemoved); - //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); if(removed.getId() == targetId) { Path path = new Path(); @@ -91,7 +92,7 @@ public void init(Node source, Node target, PriorityQueue queue, } public abstract void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, - List wasVisited, PriorityQueue queue, HashMap parents); + Set wasVisited, PriorityQueue queue, HashMap parents); @Override public Path shortestPath(Node source, Node target) { diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java index 9efbd83..fe33122 100644 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraConstantWeight.java @@ -3,8 +3,8 @@ import it.unimi.dsi.fastutil.longs.Long2IntMap; import java.util.HashMap; -import java.util.List; import java.util.PriorityQueue; +import java.util.Set; import org.graphast.model.Edge; import org.graphast.model.Graph; @@ -23,7 +23,7 @@ public DijkstraConstantWeight(GraphBounds graphBounds) { super(graphBounds); } - public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, List wasVisited, + public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, Set wasVisited, PriorityQueue queue, HashMap parents){ Long2IntMap neig = graph.accessNeighborhood(graph.getNode(removed.getId())); diff --git a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java index 8bf5eb7..a24a1e7 100755 --- a/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java +++ b/core/src/main/java/org/graphast/query/route/shortestpath/dijkstra/DijkstraLinearFunction.java @@ -33,13 +33,14 @@ public DijkstraLinearFunction(GraphBounds graphBounds) { super(graphBounds); } - public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, List allWasViseted, + public void expandVertex(Node target, TimeEntry removed, HashMap wasTraversed, Set allWasViseted, PriorityQueue queue, HashMap parents){ HashMap neighbors = graph.accessNeighborhood(graph.getNode(removed.getId()), removed.getArrivalTime()); for (Node node : neighbors.keySet()) { long vid = node.getId(); + allWasViseted.add(vid); int at = graph.getArrival(removed.getArrivalTime(), neighbors.get(node)); int tt = removed.getTravelTime() + neighbors.get(node); TimeEntry newEntry = new TimeEntry( vid, tt, at, removed.getId()); @@ -49,10 +50,7 @@ public void expandVertex(Node target, TimeEntry removed, HashMap if(!wasTraversed.containsKey(vid)){ queue.offer(newEntry); - long idNewEntry = newEntry.getId(); wasTraversed.put(newEntry.getId(), newEntry.getTravelTime()); - allWasViseted.add(idNewEntry); - //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); distance = neighbors.get(node); edge = getEdge(removed.getId(), vid, distance); @@ -66,9 +64,6 @@ public void expandVertex(Node target, TimeEntry removed, HashMap long idNewEntry = newEntry.getId(); wasTraversed.remove(idNewEntry); wasTraversed.put(idNewEntry, newEntry.getTravelTime()); - allWasViseted.add(idNewEntry); - //System.out.println("vertex already visited"); - //System.out.println(String.format("was viseted %s vertices", allWasViseted.size())); parents.remove(node); distance = neighbors.get(node); From 240ff52030174187b660f62a45fe15f53b29a2e9 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 15 Jan 2016 10:55:11 -0300 Subject: [PATCH 086/118] refactorado nome de variavel --- .../org/graphast/query/rnn/RNNBacktrackingSearch.java | 2 +- .../org/graphast/query/rnn/RNNBreadthFirstSearch.java | 4 ++-- .../java/org/graphast/knn/RNNBreadthFirstSearchTest.java | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java index e62b0b2..bfac129 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBacktrackingSearch.java @@ -66,7 +66,7 @@ private NearestNeighbor createNN(Node root, long currentPoi, int numberVisitedNo double totalCostInMilissegundo = path.getTotalCost(); double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); - nearestNeighbor.setTravelTime(Double.valueOf(totalCostInNanosegundos)); + nearestNeighbor.setTravelTime(totalCostInNanosegundos); nearestNeighbor.setId(currentPoi); nearestNeighbor.setNumberVisitedNodes(numberVisitedNodes); diff --git a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java index 6eccc3d..6e07b6e 100644 --- a/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java +++ b/core/src/main/java/org/graphast/query/rnn/RNNBreadthFirstSearch.java @@ -71,8 +71,8 @@ public NearestNeighbor search(Node customer, Date maxTravelTime, Date startServi double totalCostInNanosegundos = totalCostInMilissegundo * Math.pow(10, 6); ArrayList pathToTaxi = pathToTaxi(current.getId(), customer.getId(), parents); - NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(), - Double.valueOf(totalCostInNanosegundos), pathToTaxi, numberVisitedNodes); + NearestNeighbor nearestNeighbor = new NearestNeighbor(current.getId(),totalCostInNanosegundos, + pathToTaxi, numberVisitedNodes); return nearestNeighbor; } diff --git a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java index f62b972..60de5cd 100644 --- a/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java +++ b/core/src/test/java/org/graphast/knn/RNNBreadthFirstSearchTest.java @@ -39,8 +39,8 @@ public void setUpNoRandomGraph() throws ParseException, IOException { hourServiceTime = DateUtils.parseDate(00, 00, 00); graphBounds = new GraphGenerator().generateExampleTAXI(); - graphBoundsReverse = new GraphGenerator().generateExampleTAXI(); - graphBoundsReverse.reverseGraph(); +// graphBoundsReverse = new GraphGenerator().generateExampleTAXI(); +// graphBoundsReverse.reverseGraph(); } public void setUpRandomGraph(int qtdX, int qtdY, int percentPois) throws ParseException { @@ -94,8 +94,8 @@ public void taxiSearchExpected_II() throws ParseException, IOException { // Tempo para atendiemento maxTravelTime = DateUtils.parseDate(0, 11, 59); - RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBoundsReverse); - taxiSearch.search(graphBoundsReverse.getNode(idCustomer), maxTravelTime, hourServiceTime); + RNNBreadthFirstSearch taxiSearch = new RNNBreadthFirstSearch(graphBounds); + taxiSearch.search(graphBounds.getNode(idCustomer), maxTravelTime, hourServiceTime); } // TESTE 2: O cliente está em um vértice vizinho ao taxista. CLIENTE -> TAXISTA (NÃO REVERSO) From 26da7041173003c0b353e19cc1211fb544369fe0 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Fri, 15 Jan 2016 10:55:42 -0300 Subject: [PATCH 087/118] adicionando parametro para captura consumo de memoria total. --- .../rnn/CompareRNNSearchsMethodsAnalysis.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 9b92909..34af678 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -21,7 +21,8 @@ public class CompareRNNSearchsMethodsAnalysis { protected static final Logger LOGGER = Logger.getGlobal(); public static void main(String[] args) throws IOException { - runAnalysis("view_exp_1k", Integer.parseInt(args[0])); + + runAnalysis("view_exp_1k", 10); /*runAnalysis("view_exp_10k", Integer.parseInt(args[0])); runAnalysis("view_exp_50k", Integer.parseInt(args[0])); runAnalysis("view_exp_100k", Integer.parseInt(args[0]));*/ @@ -59,12 +60,19 @@ public static void runAnalysis(String tableName, int testTimes) throws IOExcepti private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { try { + + Runtime.getRuntime().gc(); + long numberUseMemoryInit = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long startTime = System.nanoTime(); - NearestNeighbor solution = rnn.search(customer, timeout, timestamp); + NearestNeighbor solution = null; + solution = rnn.search(customer, timeout, timestamp); long endTime = System.nanoTime(); + long numberUseMemoryFinal = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long time = endTime - startTime; - + long numerUseMemory = (numberUseMemoryFinal - numberUseMemoryInit) / 1024; + System.out.println(" --------- >"+numerUseMemory); + Long solutionId = null; Double travelTime = null; Integer nodesSize = null; From 294e6095604992b6ba9830fcb192e49bc7aa5e86 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Sat, 16 Jan 2016 22:42:45 -0300 Subject: [PATCH 088/118] Adicionada classe de Benchmark. --- .../org/graphast/util/BenchmarkMemory.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 core/src/main/java/org/graphast/util/BenchmarkMemory.java diff --git a/core/src/main/java/org/graphast/util/BenchmarkMemory.java b/core/src/main/java/org/graphast/util/BenchmarkMemory.java new file mode 100644 index 0000000..5bf80c6 --- /dev/null +++ b/core/src/main/java/org/graphast/util/BenchmarkMemory.java @@ -0,0 +1,67 @@ +package org.graphast.util; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class BenchmarkMemory { + + private static final String PATTERN_SEPARATOR = ":"; + private static final String PATTERN_NUMBER = "-?\\d+"; + + public static Map quantityMemory() throws IOException { + + final String COMMAND_MEMORY = "egrep --color 'Mem|Cache|Swap' /proc/meminfo\n"; + final String COMMAND_EXIT = "exit\n"; + + Process telnetProcess = createProcessBuilder(); + BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream())); + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream())); + runCommand(COMMAND_MEMORY, output); + runCommand(COMMAND_EXIT, output); + + return runPatter(input); + } + + private static Map runPatter(BufferedReader input) throws IOException { + + Map listMemory = new HashMap(); + String line; + + while ((line = input.readLine()) != null) { + + String[] split = line.split(PATTERN_SEPARATOR); + String keyMemory = split[0]; + String x2 = split[1]; + Matcher matcher = Pattern.compile(PATTERN_NUMBER).matcher(x2); + Long value = 0l; + + while (matcher.find()) { + value = Long.valueOf(matcher.group()); + } + + listMemory.put(keyMemory, value); + } + + return listMemory; + } + + private static Process createProcessBuilder() throws IOException { + + ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash"); + processBuilder.redirectErrorStream(true); + Process telnetProcess = processBuilder.start(); + return telnetProcess; + } + + private static void runCommand(final String command, BufferedWriter output) throws IOException { + output.write(command); + output.flush(); + } +} From c4c5538f5ec16bca307489a9b002268b6cf3196d Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 4 Feb 2016 16:19:45 -0300 Subject: [PATCH 089/118] Benchmark Memory. --- core/pom.xml | 6 + .../rnn/CompareRNNSearchsMethodsAnalysis.java | 41 +++++-- .../org/graphast/util/BenchmarkMemory.java | 104 +++++++++++++++++- 3 files changed, 140 insertions(+), 11 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index a544ab9..94dbd3c 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -90,6 +90,12 @@ postgis-jdbc 1.3.3 + + + org.apache.commons + commons-lang3 + 3.4 + diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 34af678..33a5ee2 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -12,6 +12,7 @@ import org.graphast.model.GraphBounds; import org.graphast.model.Node; import org.graphast.query.knn.NearestNeighbor; +import org.graphast.util.BenchmarkMemory; import org.graphast.util.DateUtils; import org.graphast.util.NumberUtils; @@ -23,9 +24,9 @@ public class CompareRNNSearchsMethodsAnalysis { public static void main(String[] args) throws IOException { runAnalysis("view_exp_1k", 10); - /*runAnalysis("view_exp_10k", Integer.parseInt(args[0])); + runAnalysis("view_exp_10k", Integer.parseInt(args[0])); runAnalysis("view_exp_50k", Integer.parseInt(args[0])); - runAnalysis("view_exp_100k", Integer.parseInt(args[0]));*/ + runAnalysis("view_exp_100k", Integer.parseInt(args[0])); } public static void runAnalysis(String tableName, int testTimes) throws IOException { @@ -61,17 +62,39 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { try { - Runtime.getRuntime().gc(); - long numberUseMemoryInit = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + //List listIdProcess = BenchmarkMemory.listIdProcess(); + long numberUseMemoryInit = BenchmarkMemory.getUsedMemory(); long startTime = System.nanoTime(); + long ioProcessReadInit = 0; + long ioProcessWriteInit = 0; + + /*for (Integer pid : listIdProcess) { + ioProcessReadInit = ioProcessReadInit + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.READ); + ioProcessWriteInit = ioProcessWriteInit + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.WRITE); + }*/ + NearestNeighbor solution = null; solution = rnn.search(customer, timeout, timestamp); + + long ioProcessReadEnd = 0; + long ioProcessWriteEnd = 0; + + /*for (Integer pid : listIdProcess) { + ioProcessReadEnd = ioProcessReadEnd + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.READ); + ioProcessWriteEnd = ioProcessWriteEnd + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.WRITE); + }*/ + long endTime = System.nanoTime(); - long numberUseMemoryFinal = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + long numberUseMemoryFinal = BenchmarkMemory.getUsedMemory(); long time = endTime - startTime; long numerUseMemory = (numberUseMemoryFinal - numberUseMemoryInit) / 1024; - System.out.println(" --------- >"+numerUseMemory); + + long read = ioProcessReadEnd - ioProcessReadInit; + long write = ioProcessWriteEnd - ioProcessWriteInit; + + System.out.println(" READ "+read); + System.out.println(" WRITE "+write); Long solutionId = null; Double travelTime = null; @@ -103,13 +126,12 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, gidVisited = gidVisited + "-" + nodeVisited.getLabel(); } - String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, + String currentLine = String.format("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s", coordinatesCustomer, poiCoordinate, time, solutionId, externalId, travelTime, - nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited, numberVisitedNodes) + "\n"; + nodesSize, path, coordinateNodeVisited, gidCustomer, gidPoi, gidVisited, numberVisitedNodes, numerUseMemory) + "\n"; System.out.println(currentLine); - fileCsv.write(currentLine); } } catch(PathNotFoundException e) { @@ -117,7 +139,6 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, } } - private static Node getRandomCustomerInGraph(GraphBounds graph) { Node node; double[] bounds = new double[]{-3.710467, -38.591078, -3.802376, -38.465530}; diff --git a/core/src/main/java/org/graphast/util/BenchmarkMemory.java b/core/src/main/java/org/graphast/util/BenchmarkMemory.java index 5bf80c6..2380ddf 100644 --- a/core/src/main/java/org/graphast/util/BenchmarkMemory.java +++ b/core/src/main/java/org/graphast/util/BenchmarkMemory.java @@ -5,7 +5,9 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -28,6 +30,90 @@ public static Map quantityMemory() throws IOException { return runPatter(input); } + + //cat /proc/10925/io + + public static Long ioProcess(int pid, PROCESS process) throws IOException { + + final String COMMAND_PROCESS = "cat /proc/"+pid+"/io \n"; + final String COMMAND_EXIT = "exit\n"; + + Process telnetProcess = createProcessBuilder(); + BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream())); + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream())); + runCommand(COMMAND_PROCESS, output); + runCommand(COMMAND_EXIT, output); + + Map runPatterIO = runPatterIO(input); + Long read_byte = runPatterIO.get("rchar"); + Long write_byte = runPatterIO.get("wchar"); + + if(read_byte != null && write_byte != null) { + if(process.equals(PROCESS.READ)) { + return read_byte; + } else { + return write_byte; + } + } + return 0l; + } + + private static Map runPatterIO(BufferedReader input) throws NumberFormatException, IOException { + + Map listMemory = new HashMap(); + String line; + + while ((line = input.readLine()) != null) { + + String[] split = line.split(PATTERN_SEPARATOR); + String keyIOProcess = split[0]; + String x2 = split[1]; + Matcher matcher = Pattern.compile(PATTERN_NUMBER).matcher(x2); + Long value = 0l; + + while (matcher.find()) { + value = Long.valueOf(matcher.group()); + } + + listMemory.put(keyIOProcess, value); + } + + return listMemory; + } + + public enum PROCESS { + READ, + WRITE + } + + public static List listIdProcess() throws IOException { + + final String COMMAND_PROCESS = "ps -ax | grep java\n"; + final String COMMAND_EXIT = "exit\n"; + + Process telnetProcess = createProcessBuilder(); + BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream())); + BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream())); + runCommand(COMMAND_PROCESS, output); + runCommand(COMMAND_EXIT, output); + + return runPatterProcess(input); + } + + private static List runPatterProcess(BufferedReader input) throws IOException { + + List listProcess = new ArrayList(); + String line; + + while ((line = input.readLine()) != null) { + String[] PID = line.split(" "); + if(org.apache.commons.lang3.StringUtils.isNumeric(PID[0])) { + listProcess.add(Integer.valueOf(PID[0])); + } + } + + return listProcess; + } private static Map runPatter(BufferedReader input) throws IOException { @@ -35,7 +121,7 @@ private static Map runPatter(BufferedReader input) throws IOExcept String line; while ((line = input.readLine()) != null) { - + String[] split = line.split(PATTERN_SEPARATOR); String keyMemory = split[0]; String x2 = split[1]; @@ -64,4 +150,20 @@ private static void runCommand(final String command, BufferedWriter output) thro output.write(command); output.flush(); } + + public static long getUsedMemory() { + + Runtime.getRuntime().gc(); + long numberUseMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + return numberUseMemory; + } + + public static void main(String[] args) throws IOException { + + List listIdProcess = BenchmarkMemory.listIdProcess(); + for (Integer integer : listIdProcess) { + System.out.println(BenchmarkMemory.ioProcess(integer, PROCESS.READ)); + System.out.println(BenchmarkMemory.ioProcess(integer, PROCESS.WRITE)); + } + } } From fbe8d5d4b56383d7c581a6e97770db08da2ac925 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 28 May 2016 12:11:39 -0300 Subject: [PATCH 090/118] =?UTF-8?q?Retirado=20coment=C3=A1rios.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/db.properties | 16 ++++++++-------- .../rnn/CompareRNNSearchsMethodsAnalysis.java | 19 ++++--------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/core/db.properties b/core/db.properties index 201ec27..982641b 100644 --- a/core/db.properties +++ b/core/db.properties @@ -1,11 +1,11 @@ # remoto - nuvem arida -#driver=org.postgresql.Driver -#host=jdbc:postgresql://arida1.mooo.com:8080/fortaleza -#user=postgres -#password=aridapostgres12 - -#local driver=org.postgresql.Driver -host=jdbc:postgresql://localhost:5432/fortaleza_novo +host=jdbc:postgresql://177.130.199.157:8080/fortaleza user=postgres -password=postgres \ No newline at end of file +password=aridapostgres12 + +#local +#driver=org.postgresql.Driver +#host=jdbc:postgresql://localhost:5432/fortaleza_novo +#user=postgres +#password=postgres \ No newline at end of file diff --git a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java index 33a5ee2..644a7b6 100644 --- a/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java +++ b/core/src/main/java/org/graphast/query/rnn/CompareRNNSearchsMethodsAnalysis.java @@ -24,9 +24,9 @@ public class CompareRNNSearchsMethodsAnalysis { public static void main(String[] args) throws IOException { runAnalysis("view_exp_1k", 10); - runAnalysis("view_exp_10k", Integer.parseInt(args[0])); - runAnalysis("view_exp_50k", Integer.parseInt(args[0])); - runAnalysis("view_exp_100k", Integer.parseInt(args[0])); + //runAnalysis("view_exp_10k", Integer.parseInt(args[0])); + //runAnalysis("view_exp_50k", Integer.parseInt(args[0])); + //runAnalysis("view_exp_100k", Integer.parseInt(args[0])); } public static void runAnalysis(String tableName, int testTimes) throws IOException { @@ -62,28 +62,17 @@ private static void runSearchAndWrite(GraphBounds graph, IRNNTimeDependent rnn, Node customer, Date timeout, Date timestamp, FileWriter fileCsv) throws IOException { try { - //List listIdProcess = BenchmarkMemory.listIdProcess(); long numberUseMemoryInit = BenchmarkMemory.getUsedMemory(); long startTime = System.nanoTime(); long ioProcessReadInit = 0; long ioProcessWriteInit = 0; - - /*for (Integer pid : listIdProcess) { - ioProcessReadInit = ioProcessReadInit + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.READ); - ioProcessWriteInit = ioProcessWriteInit + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.WRITE); - }*/ - + NearestNeighbor solution = null; solution = rnn.search(customer, timeout, timestamp); long ioProcessReadEnd = 0; long ioProcessWriteEnd = 0; - /*for (Integer pid : listIdProcess) { - ioProcessReadEnd = ioProcessReadEnd + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.READ); - ioProcessWriteEnd = ioProcessWriteEnd + BenchmarkMemory.ioProcess(pid, BenchmarkMemory.PROCESS.WRITE); - }*/ - long endTime = System.nanoTime(); long numberUseMemoryFinal = BenchmarkMemory.getUsedMemory(); From e1a1ebbcbf9e7ca90fba307c1b41dd165aafa61e Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 31 May 2016 16:17:26 -0300 Subject: [PATCH 091/118] Alterado GraphBoundsImpl para GraphImpl --- .../main/java/org/graphast/importer/GraphGeneratorGrid.java | 5 ++--- core/src/main/java/org/graphast/importer/OSMDBImporter.java | 4 ++-- .../java/org/graphast/graphgenerator/GraphGenerator.java | 4 ++-- .../graphast/graphgenerator/GraphGeneratorGridTester.java | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java b/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java index fb79957..db6f9e9 100644 --- a/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java +++ b/core/src/main/java/org/graphast/importer/GraphGeneratorGrid.java @@ -6,11 +6,10 @@ import java.util.Random; import java.util.Set; -import org.graphast.importer.CostGenerator; import org.graphast.model.Edge; import org.graphast.model.EdgeImpl; import org.graphast.model.GraphBounds; -import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.GraphImpl; import org.graphast.model.Node; import org.graphast.model.NodeImpl; @@ -27,7 +26,7 @@ public GraphGeneratorGrid(String pathGraph, int width, int length, double percen this.width = width; this.length = length; this.percentagemPoi = percentualPoi; - this.graph = new GraphBoundsImpl(pathGraph); + this.graph = new GraphImpl(pathGraph); } public GraphGeneratorGrid(String pathGraph, int size, double percentualPoi) { diff --git a/core/src/main/java/org/graphast/importer/OSMDBImporter.java b/core/src/main/java/org/graphast/importer/OSMDBImporter.java index c3e23d1..b8c7ada 100644 --- a/core/src/main/java/org/graphast/importer/OSMDBImporter.java +++ b/core/src/main/java/org/graphast/importer/OSMDBImporter.java @@ -9,7 +9,7 @@ import org.graphast.model.Edge; import org.graphast.model.EdgeImpl; import org.graphast.model.GraphBounds; -import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.GraphImpl; import org.graphast.model.Node; import org.graphast.model.NodeImpl; import org.graphast.query.dao.postgis.GraphastDAO; @@ -35,7 +35,7 @@ public class OSMDBImporter implements Importer { public OSMDBImporter(String table, String directory) { this.table = table; dao = new GraphastDAO(); - graph = new GraphBoundsImpl(directory); + graph = new GraphImpl(directory); } @Override diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java b/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java index adf6abe..42d0c89 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGenerator.java @@ -576,7 +576,7 @@ public Graph generateAndorra() { public GraphBounds generateExampleTAXI() { - GraphBounds graph = new GraphBoundsImpl(Configuration.USER_HOME + "/graphast/test/examplePoI"); + GraphBounds graph = new GraphImpl(Configuration.USER_HOME + "/graphast/test/examplePoI"); Node node; Edge edge; @@ -727,7 +727,7 @@ public GraphBounds generateExampleTaxi15to15minutes() { final String directory = Configuration.USER_HOME + separator + "graphast" + separator + "test" +separator + "exampleTaxi"; - GraphBounds graph = new GraphBoundsImpl(directory); + GraphBounds graph = new GraphImpl(directory); graph.addNode(new NodeImpl(0l, 0.0d, 1.0d)); diff --git a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java index 013d748..533a81d 100644 --- a/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java +++ b/core/src/test/java/org/graphast/graphgenerator/GraphGeneratorGridTester.java @@ -10,7 +10,7 @@ import org.graphast.model.Edge; import org.graphast.model.EdgeImpl; import org.graphast.model.GraphBounds; -import org.graphast.model.GraphBoundsImpl; +import org.graphast.model.GraphImpl; import org.graphast.model.Node; import org.graphast.model.NodeImpl; @@ -27,7 +27,7 @@ public GraphGeneratorGridTester(String pathGraph, int width, int length, double this.width = width; this.length = length; this.percentagemPoi = percentualPoi; - this.graph = new GraphBoundsImpl(pathGraph); + this.graph = new GraphImpl(pathGraph); } public GraphGeneratorGridTester(String pathGraph, int size, double percentualPoi) { From 51514db7e4f36cb12ab2e7d9f1c714bcd42be49d Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 18 Jan 2016 12:07:06 -0300 Subject: [PATCH 092/118] =?UTF-8?q?descoplando=20gerador=20de=20fun=C3=A7?= =?UTF-8?q?=C3=A3o=20do=20edge=20do=20graph=20e=20o=20engine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/graphast/model/Graph.java | 6 + .../piecewise/BuilderDatasourceCSV.java | 10 ++ .../piecewise/BuilderDatasourceDB.java | 59 +++++++++ .../java/org/graphast/piecewise/Function.java | 5 + .../piecewise/GeneratorFunctionLoess.java | 124 ++++++++++++++++++ .../piecewise/GeneratorFunctionMatrix.java | 9 ++ .../piecewise/IBuilderDatasource.java | 7 + .../IGeneratorFunctionPiecewise.java | 6 + .../piecewise/IManipulatorEngine.java | 7 + .../org/graphast/piecewise/ManipulatorR.java | 10 ++ ...eneratorFunctionPiecewiseDatabaseTest.java | 83 ++++++++++++ .../piecewise/GeneratorFunctionRefactore.java | 34 +++++ .../GeneratorGraphFunctionPiecewiseTest.java | 23 ++++ 13 files changed, 383 insertions(+) create mode 100644 core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java create mode 100644 core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java create mode 100644 core/src/main/java/org/graphast/piecewise/Function.java create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java create mode 100644 core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java create mode 100644 core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java create mode 100644 core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java create mode 100644 core/src/main/java/org/graphast/piecewise/ManipulatorR.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java diff --git a/core/src/main/java/org/graphast/model/Graph.java b/core/src/main/java/org/graphast/model/Graph.java index 1345a4f..e41a93e 100644 --- a/core/src/main/java/org/graphast/model/Graph.java +++ b/core/src/main/java/org/graphast/model/Graph.java @@ -14,6 +14,7 @@ import org.graphast.geometry.BBox; import org.graphast.geometry.PoI; import org.graphast.geometry.Point; +import org.graphast.piecewise.Function; import org.graphast.util.FileUtils; @@ -330,6 +331,11 @@ public interface Graph { public String getAbsoluteDirectory(); public void setDirectory(String directory); +<<<<<<< HEAD public Set getPoiIds(); +======= + + public void setFuntionEdge(long edgeId, Function function); +>>>>>>> descoplando gerador de função do edge do graph e o engine } \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java new file mode 100644 index 0000000..2876a14 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + + +public class BuilderDatasourceCSV implements IBuilderDatasource { + + @Override + public double[][] getData() { + return null; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java new file mode 100644 index 0000000..2ca233a --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java @@ -0,0 +1,59 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.graphast.query.postgis.QueryPostgis; +import org.graphast.util.ConnectionJDBC; + +public class BuilderDatasourceDB implements IBuilderDatasource { + + private int FIELD_DATE_TIME = 1; + private int FIELD_DURACAO = 2; + + @Override + public double[][] getData() throws PiecewiseException { + + Connection connectionJDBC = null; + try { + connectionJDBC = ConnectionJDBC.getConnection(); + } catch (ClassNotFoundException | SQLException | IOException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); + } + + ResultSet resultSet = null; + double[][] matrixResult; + try { + Statement statement = connectionJDBC.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); + resultSet = statement.executeQuery(QueryPostgis.QUERY_ALL_DATE_TIME_WITH_DURATE); + + resultSet.last(); + int size = resultSet.getRow(); + resultSet.beforeFirst(); + + matrixResult = new double[size][2]; + + int i = 0; + while (resultSet.next()) { + + Double dateTime = resultSet.getDouble(FIELD_DATE_TIME); + Double duracao = resultSet.getDouble(FIELD_DURACAO); + double[] result = new double[2]; + result[0] = duracao; + result[1] = dateTime; + + matrixResult[i] = result; + i++; + } + + } catch (SQLException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu ao pegar as informações no DB."); + } + + + return matrixResult; + } +} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java new file mode 100644 index 0000000..8b2a95d --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -0,0 +1,5 @@ +package org.graphast.piecewise; + +public class Function { + +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java new file mode 100644 index 0000000..d16777e --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -0,0 +1,124 @@ +package org.graphast.piecewise; + +import rcaller.RCaller; +import rcaller.RCode; + +public class GeneratorFunctionLoess implements IGeneratorFunctionPiecewise { + + private IManipulatorEngine engine; + + public GeneratorFunctionLoess() { + } + + public GeneratorFunctionLoess(IManipulatorEngine engine) { + this.engine = engine; + } + + private static final String RSCRIPT = "/usr/bin/Rscript"; + + public double getValue(double timestamp) throws PiecewiseException { + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] coleta = generatorFunctionPiecewise.getData(); + return getFuntionEdge(coleta, timestamp); + } + + @Override + public Function gerFuntionEdge(long idEdge, long timestamp) { + + engine.run(); + return null; + } + + private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + + PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); + PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + + double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); + double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); + + + double yFinal = 0; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { + + PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); + double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); + double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); + + yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + + coeficienteAngularAnterior = coeficienteAngularCurrent; + coeficienteLinearAnterior = coeficienteLinearCurrent; + } + + double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); + + PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], + pontosInflexao[pontosInflexao.length - 1]); + PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); + + double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); + double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); + + yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); + + return yFinal; + } + + private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { + double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); + return gama; + } + + private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { + double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); + return alfa; + } + + class PontoGeometrico { + + double x; + double y; + + public PontoGeometrico(double x, double y) { + this.x = x; + this.y= y; + } + + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } + } +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java new file mode 100644 index 0000000..64702a6 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -0,0 +1,9 @@ +package org.graphast.piecewise; + +public class GeneratorFunctionMatrix implements IGeneratorFunctionPiecewise { + + @Override + public double gerFuntionEdge(long idEdge, double timestamp) { + return 0; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java b/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java new file mode 100644 index 0000000..0cfbe64 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java @@ -0,0 +1,7 @@ +package org.graphast.piecewise; + + +public interface IBuilderDatasource { + + public double[][] getData() throws PiecewiseException; +} diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java new file mode 100644 index 0000000..d6e5066 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java @@ -0,0 +1,6 @@ +package org.graphast.piecewise; + +public interface IGeneratorFunctionPiecewise { + + Function gerFuntionEdge(long idEdge, long timestamp); +} diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java new file mode 100644 index 0000000..6208c65 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -0,0 +1,7 @@ +package org.graphast.piecewise; + +public interface IManipulatorEngine { + + Function run(); + +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java new file mode 100644 index 0000000..e9120b2 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + +public class ManipulatorR implements IManipulatorEngine { + + @Override + public Function run() { + + return null; + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java new file mode 100644 index 0000000..45f1b0b --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -0,0 +1,83 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; + +import org.graphast.util.ConnectionJDBC; +import org.junit.Assert; +import org.junit.Test; + +import rcaller.RCaller; +import rcaller.RCode; + +public class GeneratorFunctionPiecewiseDatabaseTest { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + + @Test + public void getConnectionTest() throws ClassNotFoundException, SQLException, IOException { + + Connection connection = ConnectionJDBC.getConnection(); + Assert.assertNotNull(connection); + } + + @Test + public void getDataTest() throws PiecewiseException { + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] data = generatorFunctionPiecewise.getData(); + int length = data.length; + + Assert.assertEquals(28, length); + } + + @Test + public void verifyPointsTest() throws IOException, PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] data = generatorFunctionPiecewise.getData(); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + System.out.println(caller.getParser().getXMLFileAsString().toString()); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + Assert.assertEquals(Double.valueOf("1201977368000"), Double.valueOf(pontoInicial[0])); + Assert.assertEquals(Double.valueOf("295522763826171"), Double.valueOf(pontoInicial[1])); + + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + int length = pontosInflexao.length; + Assert.assertEquals(6, length); + + Assert.assertEquals(Double.valueOf("1202063763696"), Double.valueOf(pontosInflexao[0])); + Assert.assertEquals(Double.valueOf("55328186.6718641"), Double.valueOf(pontosInflexao[length/2])); + } + + @Test + public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { + + GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); + double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); + + System.out.println(value); + Assert.assertNotNull(value); + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java new file mode 100644 index 0000000..8fc3eb7 --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java @@ -0,0 +1,34 @@ +package org.graphast.piecewise; + +import java.util.Calendar; + +import org.graphast.graphgenerator.GraphGenerator; +import org.graphast.model.Graph; +import org.junit.Test; + +public class GeneratorFunctionRefactore { + + // Com um graph já existente, basta setar a função. + @Test + public void definedFuntionEdgeTest() { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IManipulatorEngine engineR = new ManipulatorR(); + GeneratorFunctionLoess generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + //Criar o graph com as funções dos edges + @Test + public void createGraphWithFuntionEdgeTest() { + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java new file mode 100644 index 0000000..a03a6c2 --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java @@ -0,0 +1,23 @@ +package org.graphast.piecewise; + +import org.graphast.config.Configuration; +import org.graphast.model.Graph; +import org.graphast.model.GraphImpl; +import org.graphast.model.NodeImpl; +import org.junit.Test; + +public class GeneratorGraphFunctionPiecewiseTest { + + + @Test + public void createGraphTest() throws PiecewiseException { + + Graph graph = new GraphImpl(Configuration.USER_HOME + "/graphast/test/examplepiecewise"); + + NodeImpl node1 = new NodeImpl(1l, 10d, 10d, "label node 1"); + graph.addNode(node1); + + NodeImpl node2 = new NodeImpl(1l, 10d, 10d, "label node 1"); + graph.addNode(node2); + } +} From 74dc51a8511389dc7d5559149acb1dc563cc549e Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 18 Jan 2016 12:58:38 -0300 Subject: [PATCH 093/118] =?UTF-8?q?desacoplando=20implementa=C3=A7=C3=A3o?= =?UTF-8?q?=20de=20gerar=20a=20fun=C3=A7=C3=A3o=20para=20de=20uma=20aresta?= =?UTF-8?q?.=20Implementa=C3=A7=C3=A3o=20regressa=20linerar=20Loess.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/graphast/model/GraphImpl.java | 7 ++ .../piecewise/BuilderDatasourceStream.java | 10 ++ .../java/org/graphast/piecewise/Function.java | 5 +- .../piecewise/GeneratorFunctionLoess.java | 109 +----------------- .../piecewise/GeneratorFunctionMatrix.java | 13 ++- ...Piecewise.java => IGeneratorFunction.java} | 2 +- .../graphast/piecewise/ManipulatorJava.java | 9 ++ .../org/graphast/piecewise/ManipulatorR.java | 97 ++++++++++++++++ ...eneratorFunctionPiecewiseDatabaseTest.java | 2 +- .../piecewise/GeneratorFunctionRefactore.java | 52 ++++++++- 10 files changed, 192 insertions(+), 114 deletions(-) create mode 100644 core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java rename core/src/main/java/org/graphast/piecewise/{IGeneratorFunctionPiecewise.java => IGeneratorFunction.java} (66%) create mode 100644 core/src/main/java/org/graphast/piecewise/ManipulatorJava.java diff --git a/core/src/main/java/org/graphast/model/GraphImpl.java b/core/src/main/java/org/graphast/model/GraphImpl.java index 6bd8099..93964b6 100644 --- a/core/src/main/java/org/graphast/model/GraphImpl.java +++ b/core/src/main/java/org/graphast/model/GraphImpl.java @@ -28,6 +28,7 @@ import org.graphast.geometry.PoI; import org.graphast.geometry.PoICategory; import org.graphast.geometry.Point; +import org.graphast.piecewise.Function; import org.graphast.util.DistanceUtils; import org.graphast.util.FileUtils; import org.slf4j.Logger; @@ -1470,6 +1471,12 @@ public void setDirectory(String directory) { this.absoluteDirectory = FileUtils.getAbsolutePath(directory); this.directory = directory; } + + @Override + public void setFuntionEdge(long edgeId, Function function) { + // TODO Auto-generated method stub + + } @Override public Set getPoiIds() { diff --git a/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java new file mode 100644 index 0000000..725aa55 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + +public class BuilderDatasourceStream implements IBuilderDatasource { + + @Override + public double[][] getData() throws PiecewiseException { + return null; + } + +} diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java index 8b2a95d..bf15b67 100644 --- a/core/src/main/java/org/graphast/piecewise/Function.java +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -1,5 +1,8 @@ package org.graphast.piecewise; public class Function { - + + public double getValue(long timestamp) { + return 0; + } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index d16777e..445b61e 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -1,9 +1,7 @@ package org.graphast.piecewise; -import rcaller.RCaller; -import rcaller.RCode; -public class GeneratorFunctionLoess implements IGeneratorFunctionPiecewise { +public class GeneratorFunctionLoess implements IGeneratorFunction { private IManipulatorEngine engine; @@ -13,112 +11,13 @@ public GeneratorFunctionLoess() { public GeneratorFunctionLoess(IManipulatorEngine engine) { this.engine = engine; } - - private static final String RSCRIPT = "/usr/bin/Rscript"; - public double getValue(double timestamp) throws PiecewiseException { - - IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); - double[][] coleta = generatorFunctionPiecewise.getData(); - return getFuntionEdge(coleta, timestamp); + public double getValue(long timestamp) throws PiecewiseException { + return engine.run().getValue(timestamp); } @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - - engine.run(); - return null; - } - - private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addDoubleMatrix("matrix", data); - code.addRCode("dados.frame <- data.frame(matrix)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); - code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); - double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); - - PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); - PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); - - double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); - double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); - - - double yFinal = 0; - for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { - - PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); - PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); - double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); - double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); - - yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); - - coeficienteAngularAnterior = coeficienteAngularCurrent; - coeficienteLinearAnterior = coeficienteLinearCurrent; - } - - double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); - - PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], - pontosInflexao[pontosInflexao.length - 1]); - PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); - - double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); - double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - - yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); - - return yFinal; - } - - private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { - double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); - return gama; - } - - private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { - double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); - return alfa; - } - - class PontoGeometrico { - - double x; - double y; - - public PontoGeometrico(double x, double y) { - this.x = x; - this.y= y; - } - - public double getX() { - return x; - } - public void setX(double x) { - this.x = x; - } - public double getY() { - return y; - } - public void setY(double y) { - this.y = y; - } + return engine.run(); } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java index 64702a6..450f621 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -1,9 +1,16 @@ package org.graphast.piecewise; -public class GeneratorFunctionMatrix implements IGeneratorFunctionPiecewise { +public class GeneratorFunctionMatrix implements IGeneratorFunction { + + private IManipulatorEngine engine; + + public GeneratorFunctionMatrix(IManipulatorEngine engine) { + this.engine = engine; + } @Override - public double gerFuntionEdge(long idEdge, double timestamp) { - return 0; + public Function gerFuntionEdge(long idEdge, long timestamp) { + engine.run(); + return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java similarity index 66% rename from core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java rename to core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java index d6e5066..117a377 100644 --- a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java @@ -1,6 +1,6 @@ package org.graphast.piecewise; -public interface IGeneratorFunctionPiecewise { +public interface IGeneratorFunction { Function gerFuntionEdge(long idEdge, long timestamp); } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java new file mode 100644 index 0000000..d93fb2e --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java @@ -0,0 +1,9 @@ +package org.graphast.piecewise; + +public class ManipulatorJava implements IManipulatorEngine { + + @Override + public Function run() { + return null; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index e9120b2..9aecd9b 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -1,10 +1,107 @@ package org.graphast.piecewise; +import rcaller.RCaller; +import rcaller.RCode; + public class ManipulatorR implements IManipulatorEngine { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + private double[][] data; + public ManipulatorR(double[][] coleta) { + this.data = coleta; + } + @Override public Function run() { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + + PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); + PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + + double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); + double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); + + + double yFinal = 0; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { + + PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); + double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); + double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); + + yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + + coeficienteAngularAnterior = coeficienteAngularCurrent; + coeficienteLinearAnterior = coeficienteLinearCurrent; + } + + double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); + + PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], + pontosInflexao[pontosInflexao.length - 1]); + PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); + + double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); + double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); + + //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); return null; } + + private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { + double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); + return gama; + } + + private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { + double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); + return alfa; + } + + class PontoGeometrico { + + double x; + double y; + + public PontoGeometrico(double x, double y) { + this.x = x; + this.y= y; + } + + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } + } } diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 45f1b0b..6eb47be 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -75,7 +75,7 @@ public void verifyPointsTest() throws IOException, PiecewiseException { public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); - double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); + double value = generatorFunctionPiecewise.getValue(Long.valueOf("1201977368000")); System.out.println(value); Assert.assertNotNull(value); diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java index 8fc3eb7..3b3675a 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java @@ -9,8 +9,9 @@ public class GeneratorFunctionRefactore { // Com um graph já existente, basta setar a função. + // Loess sendo implementado em R @Test - public void definedFuntionEdgeTest() { + public void definedFuntionLoessREdgeTest() throws PiecewiseException { //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 Calendar calendar = Calendar.getInstance(); @@ -20,8 +21,53 @@ public void definedFuntionEdgeTest() { GraphGenerator graphGenerator = new GraphGenerator(); Graph graph = graphGenerator.generateExample(); - IManipulatorEngine engineR = new ManipulatorR(); - GeneratorFunctionLoess generatorFunction = new GeneratorFunctionLoess(engineR); + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] coleta = generatorFunctionPiecewise.getData(); + IManipulatorEngine engineR = new ManipulatorR(coleta); + + IGeneratorFunction generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + // Com um graph já existente, basta setar a função. + // Loess sendo implementado em Java + @Test + public void definedFuntionLoessJavaEdgeTest() { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IManipulatorEngine engineR = new ManipulatorJava(); + IGeneratorFunction generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + // Com um graph já existente, basta setar a função. + @Test + public void definedFuntionMatrixEdgeTest() throws PiecewiseException { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] coleta = generatorFunctionPiecewise.getData(); + IManipulatorEngine engineR = new ManipulatorR(coleta); + + IGeneratorFunction generatorFunction = new GeneratorFunctionMatrix(engineR); Function function = generatorFunction.gerFuntionEdge(1l, timestamp); graph.setFuntionEdge(1l, function); From 874503a11972580344b52002ba7d6a1ab0b25ddb Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:57:21 -0300 Subject: [PATCH 094/118] Parser json to DB com coordenadas configuradas para uma determinada Edge. --- .../piecewise/JsonTimeTravelToDB.java | 64 +++++++++++++++++++ .../org/graphast/piecewise/ManipulatorR.java | 4 +- core/src/main/resources/piecewise.sql | 15 +++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java create mode 100644 core/src/main/resources/piecewise.sql diff --git a/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java b/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java new file mode 100644 index 0000000..b98d843 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java @@ -0,0 +1,64 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.graphast.query.postgis.QueryPostgis; +import org.graphast.util.ConnectionJDBC; +import org.json.JSONArray; +import org.json.JSONObject; + +public class JsonTimeTravelToDB { + + private final String FILE_JSON_TIME_TRAVEL = "time_travel.json"; + + private String FIELD_JSON_COORDINATES = "coordinates"; + private String FIELD_JSON_TIME = "entry_time"; + private String FIELD_JSON_TOTAL_TRAVEL = "total_time"; + + public void jsonToDB() throws PiecewiseException, SQLException, ClassNotFoundException, IOException { + + JSONObject jsonObject = new JSONObject(FILE_JSON_TIME_TRAVEL); + + JSONArray coordinates = jsonObject.getJSONArray(FIELD_JSON_COORDINATES); + double timeDay = jsonObject.getDouble(FIELD_JSON_TIME); + double totalTime = jsonObject.getDouble(FIELD_JSON_TOTAL_TRAVEL); + + Object latitude = coordinates.get(0); + Object longitude = coordinates.get(1); + + Connection connectionJDBC = null; + try { + connectionJDBC = ConnectionJDBC.getConnection(); + } catch (ClassNotFoundException | SQLException | IOException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); + } + + PreparedStatement statement = connectionJDBC.prepareStatement(QueryPostgis.QUERY_CLOSER_LINESTRING); + statement.setString(0, latitude.toString()); + statement.setString(0, longitude.toString()); + + ResultSet resultSet = statement.executeQuery(QueryPostgis.QUERY_CLOSER_LINESTRING); + + while (resultSet.next()) { + + int idEdge = resultSet.getInt(0); + persisteValue(idEdge, timeDay, totalTime); + } + } + + private void persisteValue(int idEdge, double timeDay, double totalTime) throws SQLException, ClassNotFoundException, IOException { + + String insertTableSQL = QueryPostgis.INSERT_PIECEWISE; + + PreparedStatement preparedStatement = ConnectionJDBC.getConnection().prepareStatement(insertTableSQL); + preparedStatement.setInt(1, idEdge); + preparedStatement.setDouble(2, timeDay); + preparedStatement.setDouble(3, totalTime); + preparedStatement .executeUpdate(); + + } +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 9aecd9b..8811bb2 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -44,6 +44,8 @@ public Function run() { double yFinal = 0; + yFinal = coeficienteAngularAnterior; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); @@ -67,7 +69,7 @@ public Function run() { double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); - + return null; } diff --git a/core/src/main/resources/piecewise.sql b/core/src/main/resources/piecewise.sql new file mode 100644 index 0000000..abf1a20 --- /dev/null +++ b/core/src/main/resources/piecewise.sql @@ -0,0 +1,15 @@ +-- Table: public.tester + +-- DROP TABLE public.tester; + +CREATE TABLE public.piecewise +( + edgeId double precision, + timeDay double precision, + totalTime double precision +) +WITH ( + OIDS=FALSE +); +ALTER TABLE public.tester + OWNER TO postgres; \ No newline at end of file From b19fff52b4fd62a28b60a98036d68e566033be04 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:58:15 -0300 Subject: [PATCH 095/118] Insert values para piecewise table. --- .../java/org/graphast/query/postgis/QueryPostgis.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 core/src/main/java/org/graphast/query/postgis/QueryPostgis.java diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java new file mode 100644 index 0000000..7cba9a3 --- /dev/null +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -0,0 +1,9 @@ +package org.graphast.query.postgis; + +public interface QueryPostgis { + + String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; + String QUERY_CLOSER_LINESTRING = null; + String INSERT_PIECEWISE = "INSERT INTO public.tester(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; + +} From 532458c4e101c75966ee10d7494cab93cc4035f1 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:58:52 -0300 Subject: [PATCH 096/118] Insert into para piecewise table. --- core/src/main/java/org/graphast/query/postgis/QueryPostgis.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java index 7cba9a3..2ae067b 100644 --- a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -4,6 +4,6 @@ public interface QueryPostgis { String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; String QUERY_CLOSER_LINESTRING = null; - String INSERT_PIECEWISE = "INSERT INTO public.tester(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; + String INSERT_PIECEWISE = "INSERT INTO public.piecewise(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; } From 2f4346a96f1b13d9b053263368590cb980ab5206 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:59:36 -0300 Subject: [PATCH 097/118] dependecy to json parser. --- core/pom.xml | 60 +++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index be759cd..4218b70 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -14,31 +14,30 @@ maven-repo http://arida.github.io/maven-repo/ - - + - - GNU Lesser General Public License (LGPL), Version 2.1 - http://www.fsf.org/licensing/licenses/lgpl.txt - repo - - + + GNU Lesser General Public License (LGPL), Version 2.1 + http://www.fsf.org/licensing/licenses/lgpl.txt + repo + + - scm:git:git://github.com/ARiDa/graphast.git - https://github.com/ARiDa/graphast.git - https://github.com/ARiDa/graphast.git + scm:git:git://github.com/ARiDa/graphast.git + https://github.com/ARiDa/graphast.git + https://github.com/ARiDa/graphast.git graphast Graphast - + - - + + UTF-8 @@ -72,14 +71,14 @@ osmpoispbf 1.1 - + - com.github.davidmoten - rtree - 0.6.9 + com.github.davidmoten + rtree + 0.6.9 - + postgresql postgresql 9.1-901-1.jdbc4 @@ -97,6 +96,12 @@ 3.4 + + org.json + json + 20160212 + + @@ -144,21 +149,7 @@ - - + org.apache.maven.plugins maven-shade-plugin @@ -179,6 +170,7 @@ + From a8d5e446ada5bb4b0ea7778a7a4537909857bf64 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 31 May 2016 17:14:09 -0300 Subject: [PATCH 098/118] =?UTF-8?q?Corre=C3=A7oes=20ap=C3=B3s=20pull=20req?= =?UTF-8?q?uest.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/pom.xml | 7 ++ .../main/java/org/graphast/model/Graph.java | 3 - .../java/org/graphast/piecewise/Function.java | 29 ++++- .../piecewise/GeneratorFunctionLoess.java | 6 +- .../piecewise/GeneratorFunctionMatrix.java | 2 +- .../piecewise/IManipulatorEngine.java | 3 +- .../graphast/piecewise/ManipulatorJava.java | 2 +- .../org/graphast/piecewise/ManipulatorR.java | 15 ++- .../piecewise/PiecewiseException.java | 11 ++ ...eneratorFunctionPiecewiseDatabaseTest.java | 6 +- .../piecewise/RCallerEnvironmentsTest.java | 113 ++++++++++++++++++ 11 files changed, 175 insertions(+), 22 deletions(-) create mode 100644 core/src/main/java/org/graphast/piecewise/PiecewiseException.java create mode 100644 core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java diff --git a/core/pom.xml b/core/pom.xml index 4218b70..0c79cca 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -101,6 +101,13 @@ json 20160212 + + + + com.github.jbytecode + RCaller + 2.8 + diff --git a/core/src/main/java/org/graphast/model/Graph.java b/core/src/main/java/org/graphast/model/Graph.java index e41a93e..0d96db6 100644 --- a/core/src/main/java/org/graphast/model/Graph.java +++ b/core/src/main/java/org/graphast/model/Graph.java @@ -331,11 +331,8 @@ public interface Graph { public String getAbsoluteDirectory(); public void setDirectory(String directory); -<<<<<<< HEAD public Set getPoiIds(); -======= public void setFuntionEdge(long edgeId, Function function); ->>>>>>> descoplando gerador de função do edge do graph e o engine } \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java index bf15b67..04c6785 100644 --- a/core/src/main/java/org/graphast/piecewise/Function.java +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -2,7 +2,34 @@ public class Function { - public double getValue(long timestamp) { + private long y; + + private long x; + + public Function(long y, long x) { + super(); + this.y = y; + this.x = x; + } + + public long getY() { + return y; + } + + public long getX() { + return x; + } + + public void setY(long y) { + this.y = y; + } + + public void setX(long x) { + this.x = x; + } + + + public long getValue(long timestamp) { return 0; } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index 445b61e..10be79b 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -12,12 +12,8 @@ public GeneratorFunctionLoess(IManipulatorEngine engine) { this.engine = engine; } - public double getValue(long timestamp) throws PiecewiseException { - return engine.run().getValue(timestamp); - } - @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - return engine.run(); + return engine.run(timestamp); } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java index 450f621..51c6fee 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -10,7 +10,7 @@ public GeneratorFunctionMatrix(IManipulatorEngine engine) { @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - engine.run(); + engine.run(timestamp); return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java index 6208c65..b9b1bf4 100644 --- a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -2,6 +2,5 @@ public interface IManipulatorEngine { - Function run(); - + Function run(long x); } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java index d93fb2e..69ee8d0 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java @@ -3,7 +3,7 @@ public class ManipulatorJava implements IManipulatorEngine { @Override - public Function run() { + public Function run(long x) { return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 8811bb2..35136f0 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -1,7 +1,8 @@ package org.graphast.piecewise; -import rcaller.RCaller; -import rcaller.RCode; +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; + public class ManipulatorR implements IManipulatorEngine { @@ -13,7 +14,7 @@ public ManipulatorR(double[][] coleta) { } @Override - public Function run() { + public Function run(long x) { RCaller caller = new RCaller(); RCode code = caller.getRCode(); @@ -44,16 +45,17 @@ public Function run() { double yFinal = 0; + PontoGeometrico pontoGeo1 = null; yFinal = coeficienteAngularAnterior; for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { - PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); - yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + yFinal = yFinal + (pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); coeficienteAngularAnterior = coeficienteAngularCurrent; coeficienteLinearAnterior = coeficienteLinearCurrent; @@ -68,7 +70,8 @@ public Function run() { double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); + + yFinal = yFinal + (pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearFinal)) + coeficienteAngularFinal * x; return null; } diff --git a/core/src/main/java/org/graphast/piecewise/PiecewiseException.java b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java new file mode 100644 index 0000000..39e7c86 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java @@ -0,0 +1,11 @@ +package org.graphast.piecewise; + +public class PiecewiseException extends Exception { + + private static final long serialVersionUID = 1L; + + public PiecewiseException(String string) { + super(string); + } + +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 6eb47be..8e2e4a8 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -8,8 +8,8 @@ import org.junit.Assert; import org.junit.Test; -import rcaller.RCaller; -import rcaller.RCode; +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; public class GeneratorFunctionPiecewiseDatabaseTest { @@ -75,7 +75,7 @@ public void verifyPointsTest() throws IOException, PiecewiseException { public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); - double value = generatorFunctionPiecewise.getValue(Long.valueOf("1201977368000")); + Function value = generatorFunctionPiecewise.gerFuntionEdge(1l, Long.valueOf("1201977368000").longValue()); System.out.println(value); Assert.assertNotNull(value); diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java new file mode 100644 index 0000000..cb886ce --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -0,0 +1,113 @@ +package org.graphast.piecewise; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; + +public class RCallerEnvironmentsTest { + + private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript"; + + @Test + public void createRscriptTest() { + + RCaller rcaller = new RCaller(); + rcaller.setRscriptExecutable(RSCRIPT); + Assert.assertNotNull(rcaller.getRCode()); + } + + @Test + public void baseLinearOutXMLTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("ols<-lm(y~x)"); + + caller.setRCode(code); + caller.runAndReturnResult("ols"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + + @Test + public void loessTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + + caller.setRCode(code); + caller.runAndReturnResult("dados.loess"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void loessWithRandomDataTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void loessWithSynteticDataTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } +} From a0d679761b51b78de7172eafed94a5e95a49ec1b Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 31 May 2016 17:22:18 -0300 Subject: [PATCH 099/118] =?UTF-8?q?corre=C3=A7=C3=B5es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/pom.xml | 6 ++++++ core/src/main/java/org/graphast/piecewise/ManipulatorR.java | 1 + .../org/graphast/piecewise/RCallerEnvironmentsTest.java | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) mode change 100755 => 100644 core/pom.xml diff --git a/core/pom.xml b/core/pom.xml old mode 100755 new mode 100644 index 0c79cca..d5aebf3 --- a/core/pom.xml +++ b/core/pom.xml @@ -78,6 +78,12 @@ 0.6.9 + + com.github.jbytecode + RCaller + 2.8 + + postgresql postgresql diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 35136f0..7f30e84 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -4,6 +4,7 @@ import com.github.rcaller.rStuff.RCode; + public class ManipulatorR implements IManipulatorEngine { private static final String RSCRIPT = "/usr/bin/Rscript"; diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java index cb886ce..c60791b 100644 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -10,7 +10,7 @@ public class RCallerEnvironmentsTest { - private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript"; + private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript.exe"; @Test public void createRscriptTest() { @@ -24,6 +24,7 @@ public void createRscriptTest() { public void baseLinearOutXMLTest() throws IOException { RCaller caller = new RCaller(); + caller.setRscriptExecutable(RSCRIPT); RCode code = caller.getRCode(); code.addRCode("set.seed(123)"); From f148dd207e9344e2fea44a0dd77710d9c4b1d1df Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Thu, 2 Jun 2016 11:10:07 -0300 Subject: [PATCH 100/118] =?UTF-8?q?Ocultando=20informa=C3=A7=C3=B5es=20do?= =?UTF-8?q?=20bando=20de=20dados.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/db.properties | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/core/db.properties b/core/db.properties index 982641b..16de993 100644 --- a/core/db.properties +++ b/core/db.properties @@ -1,11 +1,11 @@ -# remoto - nuvem arida -driver=org.postgresql.Driver -host=jdbc:postgresql://177.130.199.157:8080/fortaleza -user=postgres -password=aridapostgres12 - -#local -#driver=org.postgresql.Driver -#host=jdbc:postgresql://localhost:5432/fortaleza_novo -#user=postgres -#password=postgres \ No newline at end of file +########################################################### +## +## CONFIGURAÇÃO DB +## +########################################################### + + +driver=DRIVER +host=HOST +user=USER +password=PASSWORD \ No newline at end of file From ddfc9607dde56e0e02a15f4f5fbe67a8ff40fbd1 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Sat, 4 Jun 2016 10:29:28 -0300 Subject: [PATCH 101/118] Removido arquivo de propriedades. --- core/db.properties | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 core/db.properties diff --git a/core/db.properties b/core/db.properties deleted file mode 100644 index 16de993..0000000 --- a/core/db.properties +++ /dev/null @@ -1,11 +0,0 @@ -########################################################### -## -## CONFIGURAÇÃO DB -## -########################################################### - - -driver=DRIVER -host=HOST -user=USER -password=PASSWORD \ No newline at end of file From c7c0db6497052f4e0cc823766ce573710a1ba639 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 18 Jan 2016 12:07:06 -0300 Subject: [PATCH 102/118] =?UTF-8?q?descoplando=20gerador=20de=20fun=C3=A7?= =?UTF-8?q?=C3=A3o=20do=20edge=20do=20graph=20e=20o=20engine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/graphast/model/Graph.java | 3 + .../piecewise/BuilderDatasourceCSV.java | 10 ++ .../piecewise/BuilderDatasourceDB.java | 59 +++++++++ .../java/org/graphast/piecewise/Function.java | 5 + .../piecewise/GeneratorFunctionLoess.java | 124 ++++++++++++++++++ .../piecewise/GeneratorFunctionMatrix.java | 9 ++ .../piecewise/IBuilderDatasource.java | 7 + .../IGeneratorFunctionPiecewise.java | 6 + .../piecewise/IManipulatorEngine.java | 7 + .../org/graphast/piecewise/ManipulatorR.java | 10 ++ ...eneratorFunctionPiecewiseDatabaseTest.java | 83 ++++++++++++ .../piecewise/GeneratorFunctionRefactore.java | 34 +++++ .../GeneratorGraphFunctionPiecewiseTest.java | 23 ++++ 13 files changed, 380 insertions(+) create mode 100644 core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java create mode 100644 core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java create mode 100644 core/src/main/java/org/graphast/piecewise/Function.java create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java create mode 100644 core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java create mode 100644 core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java create mode 100644 core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java create mode 100644 core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java create mode 100644 core/src/main/java/org/graphast/piecewise/ManipulatorR.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java create mode 100644 core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java diff --git a/core/src/main/java/org/graphast/model/Graph.java b/core/src/main/java/org/graphast/model/Graph.java index e76f847..0280170 100644 --- a/core/src/main/java/org/graphast/model/Graph.java +++ b/core/src/main/java/org/graphast/model/Graph.java @@ -14,6 +14,7 @@ import org.graphast.geometry.BBox; import org.graphast.geometry.PoI; import org.graphast.geometry.Point; +import org.graphast.piecewise.Function; import org.graphast.util.FileUtils; public interface Graph { @@ -353,4 +354,6 @@ public interface Graph { public void setDirectory(String directory); public Set getPoiIds(); + + public void setFuntionEdge(long edgeId, Function function); } \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java new file mode 100644 index 0000000..2876a14 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceCSV.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + + +public class BuilderDatasourceCSV implements IBuilderDatasource { + + @Override + public double[][] getData() { + return null; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java new file mode 100644 index 0000000..2ca233a --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceDB.java @@ -0,0 +1,59 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.graphast.query.postgis.QueryPostgis; +import org.graphast.util.ConnectionJDBC; + +public class BuilderDatasourceDB implements IBuilderDatasource { + + private int FIELD_DATE_TIME = 1; + private int FIELD_DURACAO = 2; + + @Override + public double[][] getData() throws PiecewiseException { + + Connection connectionJDBC = null; + try { + connectionJDBC = ConnectionJDBC.getConnection(); + } catch (ClassNotFoundException | SQLException | IOException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); + } + + ResultSet resultSet = null; + double[][] matrixResult; + try { + Statement statement = connectionJDBC.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); + resultSet = statement.executeQuery(QueryPostgis.QUERY_ALL_DATE_TIME_WITH_DURATE); + + resultSet.last(); + int size = resultSet.getRow(); + resultSet.beforeFirst(); + + matrixResult = new double[size][2]; + + int i = 0; + while (resultSet.next()) { + + Double dateTime = resultSet.getDouble(FIELD_DATE_TIME); + Double duracao = resultSet.getDouble(FIELD_DURACAO); + double[] result = new double[2]; + result[0] = duracao; + result[1] = dateTime; + + matrixResult[i] = result; + i++; + } + + } catch (SQLException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu ao pegar as informações no DB."); + } + + + return matrixResult; + } +} \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java new file mode 100644 index 0000000..8b2a95d --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -0,0 +1,5 @@ +package org.graphast.piecewise; + +public class Function { + +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java new file mode 100644 index 0000000..d16777e --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -0,0 +1,124 @@ +package org.graphast.piecewise; + +import rcaller.RCaller; +import rcaller.RCode; + +public class GeneratorFunctionLoess implements IGeneratorFunctionPiecewise { + + private IManipulatorEngine engine; + + public GeneratorFunctionLoess() { + } + + public GeneratorFunctionLoess(IManipulatorEngine engine) { + this.engine = engine; + } + + private static final String RSCRIPT = "/usr/bin/Rscript"; + + public double getValue(double timestamp) throws PiecewiseException { + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] coleta = generatorFunctionPiecewise.getData(); + return getFuntionEdge(coleta, timestamp); + } + + @Override + public Function gerFuntionEdge(long idEdge, long timestamp) { + + engine.run(); + return null; + } + + private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + + PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); + PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + + double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); + double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); + + + double yFinal = 0; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { + + PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); + double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); + double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); + + yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + + coeficienteAngularAnterior = coeficienteAngularCurrent; + coeficienteLinearAnterior = coeficienteLinearCurrent; + } + + double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); + + PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], + pontosInflexao[pontosInflexao.length - 1]); + PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); + + double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); + double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); + + yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); + + return yFinal; + } + + private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { + double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); + return gama; + } + + private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { + double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); + return alfa; + } + + class PontoGeometrico { + + double x; + double y; + + public PontoGeometrico(double x, double y) { + this.x = x; + this.y= y; + } + + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } + } +} diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java new file mode 100644 index 0000000..64702a6 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -0,0 +1,9 @@ +package org.graphast.piecewise; + +public class GeneratorFunctionMatrix implements IGeneratorFunctionPiecewise { + + @Override + public double gerFuntionEdge(long idEdge, double timestamp) { + return 0; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java b/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java new file mode 100644 index 0000000..0cfbe64 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IBuilderDatasource.java @@ -0,0 +1,7 @@ +package org.graphast.piecewise; + + +public interface IBuilderDatasource { + + public double[][] getData() throws PiecewiseException; +} diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java new file mode 100644 index 0000000..d6e5066 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java @@ -0,0 +1,6 @@ +package org.graphast.piecewise; + +public interface IGeneratorFunctionPiecewise { + + Function gerFuntionEdge(long idEdge, long timestamp); +} diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java new file mode 100644 index 0000000..6208c65 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -0,0 +1,7 @@ +package org.graphast.piecewise; + +public interface IManipulatorEngine { + + Function run(); + +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java new file mode 100644 index 0000000..e9120b2 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + +public class ManipulatorR implements IManipulatorEngine { + + @Override + public Function run() { + + return null; + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java new file mode 100644 index 0000000..45f1b0b --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -0,0 +1,83 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; + +import org.graphast.util.ConnectionJDBC; +import org.junit.Assert; +import org.junit.Test; + +import rcaller.RCaller; +import rcaller.RCode; + +public class GeneratorFunctionPiecewiseDatabaseTest { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + + @Test + public void getConnectionTest() throws ClassNotFoundException, SQLException, IOException { + + Connection connection = ConnectionJDBC.getConnection(); + Assert.assertNotNull(connection); + } + + @Test + public void getDataTest() throws PiecewiseException { + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] data = generatorFunctionPiecewise.getData(); + int length = data.length; + + Assert.assertEquals(28, length); + } + + @Test + public void verifyPointsTest() throws IOException, PiecewiseException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] data = generatorFunctionPiecewise.getData(); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + System.out.println(caller.getParser().getXMLFileAsString().toString()); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + Assert.assertEquals(Double.valueOf("1201977368000"), Double.valueOf(pontoInicial[0])); + Assert.assertEquals(Double.valueOf("295522763826171"), Double.valueOf(pontoInicial[1])); + + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + int length = pontosInflexao.length; + Assert.assertEquals(6, length); + + Assert.assertEquals(Double.valueOf("1202063763696"), Double.valueOf(pontosInflexao[0])); + Assert.assertEquals(Double.valueOf("55328186.6718641"), Double.valueOf(pontosInflexao[length/2])); + } + + @Test + public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { + + GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); + double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); + + System.out.println(value); + Assert.assertNotNull(value); + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java new file mode 100644 index 0000000..8fc3eb7 --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java @@ -0,0 +1,34 @@ +package org.graphast.piecewise; + +import java.util.Calendar; + +import org.graphast.graphgenerator.GraphGenerator; +import org.graphast.model.Graph; +import org.junit.Test; + +public class GeneratorFunctionRefactore { + + // Com um graph já existente, basta setar a função. + @Test + public void definedFuntionEdgeTest() { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IManipulatorEngine engineR = new ManipulatorR(); + GeneratorFunctionLoess generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + //Criar o graph com as funções dos edges + @Test + public void createGraphWithFuntionEdgeTest() { + } +} diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java new file mode 100644 index 0000000..a03a6c2 --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/GeneratorGraphFunctionPiecewiseTest.java @@ -0,0 +1,23 @@ +package org.graphast.piecewise; + +import org.graphast.config.Configuration; +import org.graphast.model.Graph; +import org.graphast.model.GraphImpl; +import org.graphast.model.NodeImpl; +import org.junit.Test; + +public class GeneratorGraphFunctionPiecewiseTest { + + + @Test + public void createGraphTest() throws PiecewiseException { + + Graph graph = new GraphImpl(Configuration.USER_HOME + "/graphast/test/examplepiecewise"); + + NodeImpl node1 = new NodeImpl(1l, 10d, 10d, "label node 1"); + graph.addNode(node1); + + NodeImpl node2 = new NodeImpl(1l, 10d, 10d, "label node 1"); + graph.addNode(node2); + } +} From b31d87677f5cc15478704d554b8cbcb469b66cd5 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 18 Jan 2016 12:58:38 -0300 Subject: [PATCH 103/118] =?UTF-8?q?desacoplando=20implementa=C3=A7=C3=A3o?= =?UTF-8?q?=20de=20gerar=20a=20fun=C3=A7=C3=A3o=20para=20de=20uma=20aresta?= =?UTF-8?q?.=20Implementa=C3=A7=C3=A3o=20regressa=20linerar=20Loess.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/graphast/model/GraphImpl.java | 7 ++ .../piecewise/BuilderDatasourceStream.java | 10 ++ .../java/org/graphast/piecewise/Function.java | 5 +- .../piecewise/GeneratorFunctionLoess.java | 109 +----------------- .../piecewise/GeneratorFunctionMatrix.java | 13 ++- ...Piecewise.java => IGeneratorFunction.java} | 2 +- .../graphast/piecewise/ManipulatorJava.java | 9 ++ .../org/graphast/piecewise/ManipulatorR.java | 97 ++++++++++++++++ ...eneratorFunctionPiecewiseDatabaseTest.java | 2 +- .../piecewise/GeneratorFunctionRefactore.java | 52 ++++++++- 10 files changed, 192 insertions(+), 114 deletions(-) create mode 100644 core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java rename core/src/main/java/org/graphast/piecewise/{IGeneratorFunctionPiecewise.java => IGeneratorFunction.java} (66%) create mode 100644 core/src/main/java/org/graphast/piecewise/ManipulatorJava.java diff --git a/core/src/main/java/org/graphast/model/GraphImpl.java b/core/src/main/java/org/graphast/model/GraphImpl.java index 10ea8af..a5adffc 100644 --- a/core/src/main/java/org/graphast/model/GraphImpl.java +++ b/core/src/main/java/org/graphast/model/GraphImpl.java @@ -29,6 +29,7 @@ import org.graphast.geometry.PoI; import org.graphast.geometry.PoICategory; import org.graphast.geometry.Point; +import org.graphast.util.DistanceUtils; import org.graphast.util.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -1588,6 +1589,12 @@ public void setDirectory(String directory) { this.absoluteDirectory = FileUtils.getAbsolutePath(directory); this.directory = directory; } + + @Override + public void setFuntionEdge(long edgeId, Function function) { + // TODO Auto-generated method stub + + } /* * A partir daqui estão os métodos adicionais vindos da antiga classe GraphBoundsImpl: diff --git a/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java new file mode 100644 index 0000000..725aa55 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/BuilderDatasourceStream.java @@ -0,0 +1,10 @@ +package org.graphast.piecewise; + +public class BuilderDatasourceStream implements IBuilderDatasource { + + @Override + public double[][] getData() throws PiecewiseException { + return null; + } + +} diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java index 8b2a95d..bf15b67 100644 --- a/core/src/main/java/org/graphast/piecewise/Function.java +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -1,5 +1,8 @@ package org.graphast.piecewise; public class Function { - + + public double getValue(long timestamp) { + return 0; + } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index d16777e..445b61e 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -1,9 +1,7 @@ package org.graphast.piecewise; -import rcaller.RCaller; -import rcaller.RCode; -public class GeneratorFunctionLoess implements IGeneratorFunctionPiecewise { +public class GeneratorFunctionLoess implements IGeneratorFunction { private IManipulatorEngine engine; @@ -13,112 +11,13 @@ public GeneratorFunctionLoess() { public GeneratorFunctionLoess(IManipulatorEngine engine) { this.engine = engine; } - - private static final String RSCRIPT = "/usr/bin/Rscript"; - public double getValue(double timestamp) throws PiecewiseException { - - IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); - double[][] coleta = generatorFunctionPiecewise.getData(); - return getFuntionEdge(coleta, timestamp); + public double getValue(long timestamp) throws PiecewiseException { + return engine.run().getValue(timestamp); } @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - - engine.run(); - return null; - } - - private double getFuntionEdge(double[][] data, double timestamp) throws PiecewiseException { - - RCaller caller = new RCaller(); - RCode code = caller.getRCode(); - caller.setRscriptExecutable(RSCRIPT); - - code.addDoubleMatrix("matrix", data); - code.addRCode("dados.frame <- data.frame(matrix)"); - code.addRCode("dados.loess <- loess(dados.frame)"); - code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); - code.addRCode("y.predict <- predict(dados.loess, xl)"); - code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); - code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); - code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); - code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); - code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); - - caller.setRCode(code); - caller.runAndReturnResult("allObjects"); - - double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); - double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); - - PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); - PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); - - double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); - double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); - - - double yFinal = 0; - for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { - - PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); - PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); - double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); - double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); - - yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); - - coeficienteAngularAnterior = coeficienteAngularCurrent; - coeficienteLinearAnterior = coeficienteLinearCurrent; - } - - double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); - - PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], - pontosInflexao[pontosInflexao.length - 1]); - PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); - - double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); - double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - - yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); - - return yFinal; - } - - private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { - double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); - return gama; - } - - private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { - double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); - return alfa; - } - - class PontoGeometrico { - - double x; - double y; - - public PontoGeometrico(double x, double y) { - this.x = x; - this.y= y; - } - - public double getX() { - return x; - } - public void setX(double x) { - this.x = x; - } - public double getY() { - return y; - } - public void setY(double y) { - this.y = y; - } + return engine.run(); } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java index 64702a6..450f621 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -1,9 +1,16 @@ package org.graphast.piecewise; -public class GeneratorFunctionMatrix implements IGeneratorFunctionPiecewise { +public class GeneratorFunctionMatrix implements IGeneratorFunction { + + private IManipulatorEngine engine; + + public GeneratorFunctionMatrix(IManipulatorEngine engine) { + this.engine = engine; + } @Override - public double gerFuntionEdge(long idEdge, double timestamp) { - return 0; + public Function gerFuntionEdge(long idEdge, long timestamp) { + engine.run(); + return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java similarity index 66% rename from core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java rename to core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java index d6e5066..117a377 100644 --- a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunction.java @@ -1,6 +1,6 @@ package org.graphast.piecewise; -public interface IGeneratorFunctionPiecewise { +public interface IGeneratorFunction { Function gerFuntionEdge(long idEdge, long timestamp); } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java new file mode 100644 index 0000000..d93fb2e --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java @@ -0,0 +1,9 @@ +package org.graphast.piecewise; + +public class ManipulatorJava implements IManipulatorEngine { + + @Override + public Function run() { + return null; + } +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index e9120b2..9aecd9b 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -1,10 +1,107 @@ package org.graphast.piecewise; +import rcaller.RCaller; +import rcaller.RCode; + public class ManipulatorR implements IManipulatorEngine { + + private static final String RSCRIPT = "/usr/bin/Rscript"; + private double[][] data; + public ManipulatorR(double[][] coleta) { + this.data = coleta; + } + @Override public Function run() { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addDoubleMatrix("matrix", data); + code.addRCode("dados.frame <- data.frame(matrix)"); + code.addRCode("dados.loess <- loess(dados.frame)"); + code.addRCode("xl <- with(dados.loess, seq(min(x), max(x), (max(x) - min(x))/1000))"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("pontoInicial <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("pontosInflexao <- cbind(xl, y.predict)[infl,]"); + code.addRCode("pontoFinal <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects <- list(pontoInicial=pontoInicial, pontoFinal=pontoFinal, pontosInflexao=pontosInflexao)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + double[] pontoInicial = caller.getParser().getAsDoubleArray("pontoInicial"); + double[] pontosInflexao = caller.getParser().getAsDoubleArray("pontosInflexao"); + + PontoGeometrico pontoInicialGeo = new PontoGeometrico(pontoInicial[0], pontoInicial[1]); + PontoGeometrico pontoFinalGeo0 = new PontoGeometrico(pontosInflexao[0], pontosInflexao[pontosInflexao.length/2]); + + double coeficienteAngularAnterior = getCoeficienteAngular(pontoInicialGeo, pontoFinalGeo0); + double coeficienteLinearAnterior = getCoeficienteLinear(pontoInicialGeo, coeficienteAngularAnterior); + + + double yFinal = 0; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { + + PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); + double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); + double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); + + yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + + coeficienteAngularAnterior = coeficienteAngularCurrent; + coeficienteLinearAnterior = coeficienteLinearCurrent; + } + + double[] pontoFinal = caller.getParser().getAsDoubleArray("pontoFinal"); + + PontoGeometrico ponto1FinalGeo = new PontoGeometrico(pontosInflexao[(pontosInflexao.length/2)-1], + pontosInflexao[pontosInflexao.length - 1]); + PontoGeometrico ponto2FinalGeo = new PontoGeometrico(pontoFinal[0], pontoFinal[1]); + + double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); + double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); + + //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); return null; } + + private double getCoeficienteLinear(PontoGeometrico pontoInicialGeo, double coeficienteAngular) { + double gama = pontoInicialGeo.getY() - coeficienteAngular * pontoInicialGeo.getX(); + return gama; + } + + private double getCoeficienteAngular(PontoGeometrico pontoInicialGeo, PontoGeometrico pontoFinalGeo) { + double alfa = (pontoFinalGeo.getY() - pontoInicialGeo.getY()) / (pontoFinalGeo.getX() - pontoInicialGeo.getX()); + return alfa; + } + + class PontoGeometrico { + + double x; + double y; + + public PontoGeometrico(double x, double y) { + this.x = x; + this.y= y; + } + + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } + } } diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 45f1b0b..6eb47be 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -75,7 +75,7 @@ public void verifyPointsTest() throws IOException, PiecewiseException { public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); - double value = generatorFunctionPiecewise.getValue(Double.valueOf("1201977368000")); + double value = generatorFunctionPiecewise.getValue(Long.valueOf("1201977368000")); System.out.println(value); Assert.assertNotNull(value); diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java index 8fc3eb7..3b3675a 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionRefactore.java @@ -9,8 +9,9 @@ public class GeneratorFunctionRefactore { // Com um graph já existente, basta setar a função. + // Loess sendo implementado em R @Test - public void definedFuntionEdgeTest() { + public void definedFuntionLoessREdgeTest() throws PiecewiseException { //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 Calendar calendar = Calendar.getInstance(); @@ -20,8 +21,53 @@ public void definedFuntionEdgeTest() { GraphGenerator graphGenerator = new GraphGenerator(); Graph graph = graphGenerator.generateExample(); - IManipulatorEngine engineR = new ManipulatorR(); - GeneratorFunctionLoess generatorFunction = new GeneratorFunctionLoess(engineR); + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] coleta = generatorFunctionPiecewise.getData(); + IManipulatorEngine engineR = new ManipulatorR(coleta); + + IGeneratorFunction generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + // Com um graph já existente, basta setar a função. + // Loess sendo implementado em Java + @Test + public void definedFuntionLoessJavaEdgeTest() { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IManipulatorEngine engineR = new ManipulatorJava(); + IGeneratorFunction generatorFunction = new GeneratorFunctionLoess(engineR); + Function function = generatorFunction.gerFuntionEdge(1l, timestamp); + + graph.setFuntionEdge(1l, function); + } + + // Com um graph já existente, basta setar a função. + @Test + public void definedFuntionMatrixEdgeTest() throws PiecewiseException { + + //Hora do dia: Dia: 16/01/2016 Hora: 11:30:00 + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 01, 16, 11, 30, 00); + long timestamp = calendar.getTimeInMillis(); + + GraphGenerator graphGenerator = new GraphGenerator(); + Graph graph = graphGenerator.generateExample(); + + IBuilderDatasource generatorFunctionPiecewise = new BuilderDatasourceDB(); + double[][] coleta = generatorFunctionPiecewise.getData(); + IManipulatorEngine engineR = new ManipulatorR(coleta); + + IGeneratorFunction generatorFunction = new GeneratorFunctionMatrix(engineR); Function function = generatorFunction.gerFuntionEdge(1l, timestamp); graph.setFuntionEdge(1l, function); From 46f5b7a93cd0c2c9cf67b1de43ad882222ae8462 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:57:21 -0300 Subject: [PATCH 104/118] Parser json to DB com coordenadas configuradas para uma determinada Edge. --- .../piecewise/JsonTimeTravelToDB.java | 64 +++++++++++++++++++ .../org/graphast/piecewise/ManipulatorR.java | 4 +- core/src/main/resources/piecewise.sql | 15 +++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java create mode 100644 core/src/main/resources/piecewise.sql diff --git a/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java b/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java new file mode 100644 index 0000000..b98d843 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/JsonTimeTravelToDB.java @@ -0,0 +1,64 @@ +package org.graphast.piecewise; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.graphast.query.postgis.QueryPostgis; +import org.graphast.util.ConnectionJDBC; +import org.json.JSONArray; +import org.json.JSONObject; + +public class JsonTimeTravelToDB { + + private final String FILE_JSON_TIME_TRAVEL = "time_travel.json"; + + private String FIELD_JSON_COORDINATES = "coordinates"; + private String FIELD_JSON_TIME = "entry_time"; + private String FIELD_JSON_TOTAL_TRAVEL = "total_time"; + + public void jsonToDB() throws PiecewiseException, SQLException, ClassNotFoundException, IOException { + + JSONObject jsonObject = new JSONObject(FILE_JSON_TIME_TRAVEL); + + JSONArray coordinates = jsonObject.getJSONArray(FIELD_JSON_COORDINATES); + double timeDay = jsonObject.getDouble(FIELD_JSON_TIME); + double totalTime = jsonObject.getDouble(FIELD_JSON_TOTAL_TRAVEL); + + Object latitude = coordinates.get(0); + Object longitude = coordinates.get(1); + + Connection connectionJDBC = null; + try { + connectionJDBC = ConnectionJDBC.getConnection(); + } catch (ClassNotFoundException | SQLException | IOException e) { + throw new PiecewiseException("[ERRO] Um erro ocorreu quando estava sendo aberta a conexão com DB."); + } + + PreparedStatement statement = connectionJDBC.prepareStatement(QueryPostgis.QUERY_CLOSER_LINESTRING); + statement.setString(0, latitude.toString()); + statement.setString(0, longitude.toString()); + + ResultSet resultSet = statement.executeQuery(QueryPostgis.QUERY_CLOSER_LINESTRING); + + while (resultSet.next()) { + + int idEdge = resultSet.getInt(0); + persisteValue(idEdge, timeDay, totalTime); + } + } + + private void persisteValue(int idEdge, double timeDay, double totalTime) throws SQLException, ClassNotFoundException, IOException { + + String insertTableSQL = QueryPostgis.INSERT_PIECEWISE; + + PreparedStatement preparedStatement = ConnectionJDBC.getConnection().prepareStatement(insertTableSQL); + preparedStatement.setInt(1, idEdge); + preparedStatement.setDouble(2, timeDay); + preparedStatement.setDouble(3, totalTime); + preparedStatement .executeUpdate(); + + } +} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 9aecd9b..8811bb2 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -44,6 +44,8 @@ public Function run() { double yFinal = 0; + yFinal = coeficienteAngularAnterior; + for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); @@ -67,7 +69,7 @@ public Function run() { double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); - + return null; } diff --git a/core/src/main/resources/piecewise.sql b/core/src/main/resources/piecewise.sql new file mode 100644 index 0000000..abf1a20 --- /dev/null +++ b/core/src/main/resources/piecewise.sql @@ -0,0 +1,15 @@ +-- Table: public.tester + +-- DROP TABLE public.tester; + +CREATE TABLE public.piecewise +( + edgeId double precision, + timeDay double precision, + totalTime double precision +) +WITH ( + OIDS=FALSE +); +ALTER TABLE public.tester + OWNER TO postgres; \ No newline at end of file From a35b0dc64ddef5c5903964b204e90a02401fa2c6 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:58:15 -0300 Subject: [PATCH 105/118] Insert values para piecewise table. --- .../java/org/graphast/query/postgis/QueryPostgis.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 core/src/main/java/org/graphast/query/postgis/QueryPostgis.java diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java new file mode 100644 index 0000000..7cba9a3 --- /dev/null +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -0,0 +1,9 @@ +package org.graphast.query.postgis; + +public interface QueryPostgis { + + String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; + String QUERY_CLOSER_LINESTRING = null; + String INSERT_PIECEWISE = "INSERT INTO public.tester(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; + +} From ffb79cbf892282bbddc08c14db1e1f6cd7c7444f Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:58:52 -0300 Subject: [PATCH 106/118] Insert into para piecewise table. --- core/src/main/java/org/graphast/query/postgis/QueryPostgis.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java index 7cba9a3..2ae067b 100644 --- a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -4,6 +4,6 @@ public interface QueryPostgis { String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; String QUERY_CLOSER_LINESTRING = null; - String INSERT_PIECEWISE = "INSERT INTO public.tester(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; + String INSERT_PIECEWISE = "INSERT INTO public.piecewise(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; } From 082d7b37909c73a8dd459ed442653431790d3f3a Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:59:36 -0300 Subject: [PATCH 107/118] dependecy to json parser. --- core/pom.xml | 47 ++++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 94dbd3c..055958d 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -14,31 +14,30 @@ maven-repo http://arida.github.io/maven-repo/ - - + - - GNU Lesser General Public License (LGPL), Version 2.1 - http://www.fsf.org/licensing/licenses/lgpl.txt - repo - - + + GNU Lesser General Public License (LGPL), Version 2.1 + http://www.fsf.org/licensing/licenses/lgpl.txt + repo + + - scm:git:git://github.com/ARiDa/graphast.git - https://github.com/ARiDa/graphast.git - https://github.com/ARiDa/graphast.git + scm:git:git://github.com/ARiDa/graphast.git + https://github.com/ARiDa/graphast.git + https://github.com/ARiDa/graphast.git graphast Graphast - + - - + + UTF-8 @@ -72,14 +71,14 @@ osmpoispbf 1.1 - + com.github.davidmoten rtree 0.7.1 - + postgresql postgresql 9.1-901-1.jdbc4 @@ -144,21 +143,7 @@ - - + org.apache.maven.plugins maven-shade-plugin From e88ffe2f952fda483f3dabc65d6e3278d47440e4 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 28 May 2016 12:08:36 -0300 Subject: [PATCH 108/118] =?UTF-8?q?Atualiza=C3=A7=C3=A3o=20da=20fun=C3=A7?= =?UTF-8?q?=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/graphast/piecewise/Function.java | 29 ++++- .../piecewise/GeneratorFunctionLoess.java | 6 +- .../piecewise/GeneratorFunctionMatrix.java | 2 +- .../piecewise/IManipulatorEngine.java | 3 +- .../graphast/piecewise/ManipulatorJava.java | 2 +- .../org/graphast/piecewise/ManipulatorR.java | 10 +- ...eneratorFunctionPiecewiseDatabaseTest.java | 2 +- .../piecewise/RCallerEnvironmentsTest.java | 113 ++++++++++++++++++ 8 files changed, 152 insertions(+), 15 deletions(-) create mode 100644 core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java diff --git a/core/src/main/java/org/graphast/piecewise/Function.java b/core/src/main/java/org/graphast/piecewise/Function.java index bf15b67..04c6785 100644 --- a/core/src/main/java/org/graphast/piecewise/Function.java +++ b/core/src/main/java/org/graphast/piecewise/Function.java @@ -2,7 +2,34 @@ public class Function { - public double getValue(long timestamp) { + private long y; + + private long x; + + public Function(long y, long x) { + super(); + this.y = y; + this.x = x; + } + + public long getY() { + return y; + } + + public long getX() { + return x; + } + + public void setY(long y) { + this.y = y; + } + + public void setX(long x) { + this.x = x; + } + + + public long getValue(long timestamp) { return 0; } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index 445b61e..10be79b 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -12,12 +12,8 @@ public GeneratorFunctionLoess(IManipulatorEngine engine) { this.engine = engine; } - public double getValue(long timestamp) throws PiecewiseException { - return engine.run().getValue(timestamp); - } - @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - return engine.run(); + return engine.run(timestamp); } } diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java index 450f621..51c6fee 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionMatrix.java @@ -10,7 +10,7 @@ public GeneratorFunctionMatrix(IManipulatorEngine engine) { @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - engine.run(); + engine.run(timestamp); return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java index 6208c65..b9b1bf4 100644 --- a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -2,6 +2,5 @@ public interface IManipulatorEngine { - Function run(); - + Function run(long x); } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java index d93fb2e..69ee8d0 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorJava.java @@ -3,7 +3,7 @@ public class ManipulatorJava implements IManipulatorEngine { @Override - public Function run() { + public Function run(long x) { return null; } } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 8811bb2..66c25e4 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -13,7 +13,7 @@ public ManipulatorR(double[][] coleta) { } @Override - public Function run() { + public Function run(long x) { RCaller caller = new RCaller(); RCode code = caller.getRCode(); @@ -44,16 +44,17 @@ public Function run() { double yFinal = 0; + PontoGeometrico pontoGeo1 = null; yFinal = coeficienteAngularAnterior; for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { - PontoGeometrico pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); + pontoGeo1 = new PontoGeometrico(pontosInflexao[i], pontosInflexao[pontosInflexao.length/2+i]); PontoGeometrico pontoGeo2 = new PontoGeometrico(pontosInflexao[i+1], pontosInflexao[pontosInflexao.length/2+(i+1)]); double coeficienteAngularCurrent = getCoeficienteAngular(pontoGeo1, pontoGeo2); double coeficienteLinearCurrent = getCoeficienteLinear(pontoGeo1, coeficienteAngularCurrent); - yFinal = yFinal + (coeficienteAngularAnterior + pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); + yFinal = yFinal + (pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearCurrent)); coeficienteAngularAnterior = coeficienteAngularCurrent; coeficienteLinearAnterior = coeficienteLinearCurrent; @@ -68,7 +69,8 @@ public Function run() { double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - //yFinal = yFinal + (coeficienteAngularFinal * timestamp + coeficienteLinearFinal); + + yFinal = yFinal + (pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearFinal)) + coeficienteAngularFinal * x; return null; } diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 6eb47be..0e3bfd2 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -75,7 +75,7 @@ public void verifyPointsTest() throws IOException, PiecewiseException { public void generationFunctionEdgeTest() throws NumberFormatException, PiecewiseException { GeneratorFunctionLoess generatorFunctionPiecewise = new GeneratorFunctionLoess(); - double value = generatorFunctionPiecewise.getValue(Long.valueOf("1201977368000")); + Function value = generatorFunctionPiecewise.gerFuntionEdge(1l, Long.valueOf("1201977368000").longValue()); System.out.println(value); Assert.assertNotNull(value); diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java new file mode 100644 index 0000000..7639a61 --- /dev/null +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -0,0 +1,113 @@ +package org.graphast.piecewise; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +import rcaller.RCaller; +import rcaller.RCode; + +public class RCallerEnvironmentsTest { + + private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript"; + + @Test + public void createRscriptTest() { + + RCaller rcaller = new RCaller(); + rcaller.setRscriptExecutable(RSCRIPT); + Assert.assertNotNull(rcaller.getRCode()); + } + + @Test + public void baseLinearOutXMLTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("ols<-lm(y~x)"); + + caller.setRCode(code); + caller.runAndReturnResult("ols"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + + @Test + public void loessTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + + caller.setRCode(code); + caller.runAndReturnResult("dados.loess"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void loessWithRandomDataTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } + + @Test + public void loessWithSynteticDataTest() throws IOException { + + RCaller caller = new RCaller(); + RCode code = caller.getRCode(); + caller.setRscriptExecutable(RSCRIPT); + + code.addRCode("set.seed(123)"); + code.addRCode("x<-rnorm(10)"); + code.addRCode("y<-rnorm(10)"); + code.addRCode("dados.loess <- loess(y ~ x)"); + code.addRCode("xl <- seq(min(x), max(x), (max(x) - min(x))/1000)"); + code.addRCode("y.predict <- predict(dados.loess, xl)"); + code.addRCode("infl <- c(FALSE, diff(diff(y.predict)>0)!=0)"); + code.addRCode("objMin <- cbind(xl, y.predict)[xl == min(xl),]"); + code.addRCode("obj <- cbind(xl, y.predict)[infl,]"); + code.addRCode("objMax <- cbind(xl, y.predict)[xl == max(xl),]"); + code.addRCode("allObjects<-list(resultMin=objMin, resultMax=objMax, resultObj=obj)"); + + caller.setRCode(code); + caller.runAndReturnResult("allObjects"); + + System.out.println(caller.getParser().getXMLFileAsString()); + Assert.assertNotNull(caller.getParser().getXMLFileAsString()); + } +} From 04ea7f28ef3a2b1269d0dc6397d407ba195df4cf Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 31 May 2016 15:33:45 -0300 Subject: [PATCH 109/118] =?UTF-8?q?Altera=C3=A7=C3=A3o=20da=20lib=20do=20R?= =?UTF-8?q?caller=20para=20utilizar=20maven.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/pom.xml | 6 ++++++ .../src/main/java/org/graphast/piecewise/ManipulatorR.java | 4 ++-- .../piecewise/GeneratorFunctionPiecewiseDatabaseTest.java | 4 ++-- .../org/graphast/piecewise/RCallerEnvironmentsTest.java | 7 ++++--- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 055958d..bcf29a2 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -78,6 +78,12 @@ 0.7.1 + + com.github.jbytecode + RCaller + 2.8 + + postgresql postgresql diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 66c25e4..216172c 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -1,7 +1,7 @@ package org.graphast.piecewise; -import rcaller.RCaller; -import rcaller.RCode; +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; public class ManipulatorR implements IManipulatorEngine { diff --git a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java index 0e3bfd2..8e2e4a8 100644 --- a/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java +++ b/core/src/test/java/org/graphast/piecewise/GeneratorFunctionPiecewiseDatabaseTest.java @@ -8,8 +8,8 @@ import org.junit.Assert; import org.junit.Test; -import rcaller.RCaller; -import rcaller.RCode; +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; public class GeneratorFunctionPiecewiseDatabaseTest { diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java index 7639a61..c60791b 100644 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -5,12 +5,12 @@ import org.junit.Assert; import org.junit.Test; -import rcaller.RCaller; -import rcaller.RCode; +import com.github.rcaller.rStuff.RCaller; +import com.github.rcaller.rStuff.RCode; public class RCallerEnvironmentsTest { - private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript"; + private static final String RSCRIPT = "C:\\Program Files\\R\\R-3.2.2\\bin\\Rscript.exe"; @Test public void createRscriptTest() { @@ -24,6 +24,7 @@ public void createRscriptTest() { public void baseLinearOutXMLTest() throws IOException { RCaller caller = new RCaller(); + caller.setRscriptExecutable(RSCRIPT); RCode code = caller.getRCode(); code.addRCode("set.seed(123)"); From 8311e09f05240af61215a49b7d3a1c75f66a0fa6 Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 18 Jan 2016 12:07:06 -0300 Subject: [PATCH 110/118] =?UTF-8?q?descoplando=20gerador=20de=20fun=C3=A7?= =?UTF-8?q?=C3=A3o=20do=20edge=20do=20graph=20e=20o=20engine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/main/java/org/graphast/model/Graph.java | 8 ++++++++ .../graphast/piecewise/IGeneratorFunctionPiecewise.java | 6 ++++++ .../java/org/graphast/piecewise/IManipulatorEngine.java | 3 +++ .../main/java/org/graphast/piecewise/ManipulatorR.java | 1 + 4 files changed, 18 insertions(+) create mode 100644 core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java diff --git a/core/src/main/java/org/graphast/model/Graph.java b/core/src/main/java/org/graphast/model/Graph.java index 0280170..5be7463 100644 --- a/core/src/main/java/org/graphast/model/Graph.java +++ b/core/src/main/java/org/graphast/model/Graph.java @@ -352,8 +352,16 @@ public interface Graph { public String getAbsoluteDirectory(); public void setDirectory(String directory); +<<<<<<< HEAD public Set getPoiIds(); +<<<<<<< HEAD public void setFuntionEdge(long edgeId, Function function); +======= +======= + + public void setFuntionEdge(long edgeId, Function function); +>>>>>>> descoplando gerador de função do edge do graph e o engine +>>>>>>> descoplando gerador de função do edge do graph e o engine } \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java new file mode 100644 index 0000000..d6e5066 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java @@ -0,0 +1,6 @@ +package org.graphast.piecewise; + +public interface IGeneratorFunctionPiecewise { + + Function gerFuntionEdge(long idEdge, long timestamp); +} diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java index b9b1bf4..bdc2467 100644 --- a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -3,4 +3,7 @@ public interface IManipulatorEngine { Function run(long x); + + Function run(); + } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 216172c..ea85707 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -108,4 +108,5 @@ public void setY(double y) { this.y = y; } } + } From 00286d5a02ed61102fe240a0711c0cd7532bb01a Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 18 Jan 2016 12:58:38 -0300 Subject: [PATCH 111/118] =?UTF-8?q?desacoplando=20implementa=C3=A7=C3=A3o?= =?UTF-8?q?=20de=20gerar=20a=20fun=C3=A7=C3=A3o=20para=20de=20uma=20aresta?= =?UTF-8?q?.=20Implementa=C3=A7=C3=A3o=20regressa=20linerar=20Loess.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/main/java/org/graphast/model/GraphImpl.java | 1 + .../java/org/graphast/piecewise/GeneratorFunctionLoess.java | 4 ++-- .../org/graphast/piecewise/IGeneratorFunctionPiecewise.java | 6 ------ core/src/main/java/org/graphast/piecewise/ManipulatorR.java | 2 +- 4 files changed, 4 insertions(+), 9 deletions(-) delete mode 100644 core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java diff --git a/core/src/main/java/org/graphast/model/GraphImpl.java b/core/src/main/java/org/graphast/model/GraphImpl.java index a5adffc..053cdac 100644 --- a/core/src/main/java/org/graphast/model/GraphImpl.java +++ b/core/src/main/java/org/graphast/model/GraphImpl.java @@ -29,6 +29,7 @@ import org.graphast.geometry.PoI; import org.graphast.geometry.PoICategory; import org.graphast.geometry.Point; +import org.graphast.piecewise.Function; import org.graphast.util.DistanceUtils; import org.graphast.util.FileUtils; import org.slf4j.Logger; diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index 10be79b..e5b97ab 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -11,9 +11,9 @@ public GeneratorFunctionLoess() { public GeneratorFunctionLoess(IManipulatorEngine engine) { this.engine = engine; } - + @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - return engine.run(timestamp); + return engine.run(); } } diff --git a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java b/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java deleted file mode 100644 index d6e5066..0000000 --- a/core/src/main/java/org/graphast/piecewise/IGeneratorFunctionPiecewise.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.graphast.piecewise; - -public interface IGeneratorFunctionPiecewise { - - Function gerFuntionEdge(long idEdge, long timestamp); -} diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index ea85707..0554323 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -14,6 +14,7 @@ public ManipulatorR(double[][] coleta) { @Override public Function run(long x) { + RCaller caller = new RCaller(); RCode code = caller.getRCode(); @@ -108,5 +109,4 @@ public void setY(double y) { this.y = y; } } - } From e0649876d02b78cd1ade003e1443b4267191053c Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:57:21 -0300 Subject: [PATCH 112/118] Parser json to DB com coordenadas configuradas para uma determinada Edge. --- core/src/main/java/org/graphast/piecewise/ManipulatorR.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index 0554323..d95c660 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -46,6 +46,7 @@ public Function run(long x) { double yFinal = 0; PontoGeometrico pontoGeo1 = null; + yFinal = coeficienteAngularAnterior; for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { @@ -70,7 +71,6 @@ public Function run(long x) { double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - yFinal = yFinal + (pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearFinal)) + coeficienteAngularFinal * x; return null; From f15269403772becd0108fa13e56bb55332543a3d Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:58:15 -0300 Subject: [PATCH 113/118] Insert values para piecewise table. --- .../main/java/org/graphast/query/postgis/QueryPostgis.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java index 2ae067b..194090e 100644 --- a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -4,6 +4,10 @@ public interface QueryPostgis { String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; String QUERY_CLOSER_LINESTRING = null; +<<<<<<< HEAD String INSERT_PIECEWISE = "INSERT INTO public.piecewise(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; +======= + String INSERT_PIECEWISE = "INSERT INTO public.tester(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; +>>>>>>> Insert values para piecewise table. } From 91521daf43c334efe58583035df6a60b6106722c Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:58:52 -0300 Subject: [PATCH 114/118] Insert into para piecewise table. --- .../main/java/org/graphast/query/postgis/QueryPostgis.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java index 194090e..2ae067b 100644 --- a/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java +++ b/core/src/main/java/org/graphast/query/postgis/QueryPostgis.java @@ -4,10 +4,6 @@ public interface QueryPostgis { String QUERY_ALL_DATE_TIME_WITH_DURATE = "SELECT date_time, duracao FROM tester;"; String QUERY_CLOSER_LINESTRING = null; -<<<<<<< HEAD String INSERT_PIECEWISE = "INSERT INTO public.piecewise(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; -======= - String INSERT_PIECEWISE = "INSERT INTO public.tester(edgeId, timeDay, totalTime) VALUES (?, ?,?);"; ->>>>>>> Insert values para piecewise table. } From 6386ba7b4cab744249b51ff536977ed41975d96e Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Mon, 14 Mar 2016 12:59:36 -0300 Subject: [PATCH 115/118] dependecy to json parser. --- core/pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/pom.xml b/core/pom.xml index bcf29a2..3d48055 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -102,6 +102,12 @@ 3.4 + + org.json + json + 20160212 + + @@ -170,6 +176,7 @@ + From 40ec30589f25309d3a41af2d7a32937d5943faea Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 31 May 2016 17:14:09 -0300 Subject: [PATCH 116/118] =?UTF-8?q?Corre=C3=A7oes=20ap=C3=B3s=20pull=20req?= =?UTF-8?q?uest.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/pom.xml | 7 +++++++ core/src/main/java/org/graphast/model/Graph.java | 7 ------- .../graphast/piecewise/GeneratorFunctionLoess.java | 4 ++-- .../org/graphast/piecewise/IManipulatorEngine.java | 1 - .../java/org/graphast/piecewise/ManipulatorR.java | 4 +--- .../org/graphast/piecewise/PiecewiseException.java | 11 +++++++++++ .../graphast/piecewise/RCallerEnvironmentsTest.java | 1 + 7 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 core/src/main/java/org/graphast/piecewise/PiecewiseException.java diff --git a/core/pom.xml b/core/pom.xml index 3d48055..34c7ee5 100755 --- a/core/pom.xml +++ b/core/pom.xml @@ -107,6 +107,13 @@ json 20160212 + + + + com.github.jbytecode + RCaller + 2.8 + diff --git a/core/src/main/java/org/graphast/model/Graph.java b/core/src/main/java/org/graphast/model/Graph.java index 5be7463..927fd0f 100644 --- a/core/src/main/java/org/graphast/model/Graph.java +++ b/core/src/main/java/org/graphast/model/Graph.java @@ -352,16 +352,9 @@ public interface Graph { public String getAbsoluteDirectory(); public void setDirectory(String directory); -<<<<<<< HEAD public Set getPoiIds(); -<<<<<<< HEAD public void setFuntionEdge(long edgeId, Function function); -======= -======= - public void setFuntionEdge(long edgeId, Function function); ->>>>>>> descoplando gerador de função do edge do graph e o engine ->>>>>>> descoplando gerador de função do edge do graph e o engine } \ No newline at end of file diff --git a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java index e5b97ab..10be79b 100644 --- a/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java +++ b/core/src/main/java/org/graphast/piecewise/GeneratorFunctionLoess.java @@ -11,9 +11,9 @@ public GeneratorFunctionLoess() { public GeneratorFunctionLoess(IManipulatorEngine engine) { this.engine = engine; } - + @Override public Function gerFuntionEdge(long idEdge, long timestamp) { - return engine.run(); + return engine.run(timestamp); } } diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java index bdc2467..558fb74 100644 --- a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -5,5 +5,4 @@ public interface IManipulatorEngine { Function run(long x); Function run(); - } diff --git a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java index d95c660..f947be3 100644 --- a/core/src/main/java/org/graphast/piecewise/ManipulatorR.java +++ b/core/src/main/java/org/graphast/piecewise/ManipulatorR.java @@ -15,7 +15,6 @@ public ManipulatorR(double[][] coleta) { @Override public Function run(long x) { - RCaller caller = new RCaller(); RCode code = caller.getRCode(); caller.setRscriptExecutable(RSCRIPT); @@ -46,7 +45,6 @@ public Function run(long x) { double yFinal = 0; PontoGeometrico pontoGeo1 = null; - yFinal = coeficienteAngularAnterior; for (int i = 0; i < (pontosInflexao.length/2) - 1; i++) { @@ -70,7 +68,7 @@ public Function run(long x) { double coeficienteAngularFinal = getCoeficienteAngular(ponto1FinalGeo, ponto2FinalGeo); double coeficienteLinearFinal = getCoeficienteLinear(ponto1FinalGeo, coeficienteAngularFinal); - + yFinal = yFinal + (pontoGeo1.getX() * (coeficienteLinearAnterior - coeficienteLinearFinal)) + coeficienteAngularFinal * x; return null; diff --git a/core/src/main/java/org/graphast/piecewise/PiecewiseException.java b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java new file mode 100644 index 0000000..39e7c86 --- /dev/null +++ b/core/src/main/java/org/graphast/piecewise/PiecewiseException.java @@ -0,0 +1,11 @@ +package org.graphast.piecewise; + +public class PiecewiseException extends Exception { + + private static final long serialVersionUID = 1L; + + public PiecewiseException(String string) { + super(string); + } + +} diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java index c60791b..263652b 100644 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -25,6 +25,7 @@ public void baseLinearOutXMLTest() throws IOException { RCaller caller = new RCaller(); caller.setRscriptExecutable(RSCRIPT); + RCode code = caller.getRCode(); code.addRCode("set.seed(123)"); From d391ad8f08a63c20c44eeef1e33fed226cb17c8b Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Tue, 31 May 2016 17:22:18 -0300 Subject: [PATCH 117/118] =?UTF-8?q?corre=C3=A7=C3=B5es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/pom.xml | 6 ++++++ .../org/graphast/piecewise/RCallerEnvironmentsTest.java | 1 - 2 files changed, 6 insertions(+), 1 deletion(-) mode change 100755 => 100644 core/pom.xml diff --git a/core/pom.xml b/core/pom.xml old mode 100755 new mode 100644 index 34c7ee5..b4a85ec --- a/core/pom.xml +++ b/core/pom.xml @@ -84,6 +84,12 @@ 2.8 + + com.github.jbytecode + RCaller + 2.8 + + postgresql postgresql diff --git a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java index 263652b..c60791b 100644 --- a/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java +++ b/core/src/test/java/org/graphast/piecewise/RCallerEnvironmentsTest.java @@ -25,7 +25,6 @@ public void baseLinearOutXMLTest() throws IOException { RCaller caller = new RCaller(); caller.setRscriptExecutable(RSCRIPT); - RCode code = caller.getRCode(); code.addRCode("set.seed(123)"); From f15f99fec86c1eb53049419b238259f5856ced3b Mon Sep 17 00:00:00 2001 From: Mirla Braga Date: Sat, 4 Jun 2016 12:20:23 -0300 Subject: [PATCH 118/118] =?UTF-8?q?Corre=C3=A7=C3=B5es=20do=20merge.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/graphast/piecewise/IManipulatorEngine.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java index 558fb74..f8d88b6 100644 --- a/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java +++ b/core/src/main/java/org/graphast/piecewise/IManipulatorEngine.java @@ -1,8 +1,6 @@ -package org.graphast.piecewise; - -public interface IManipulatorEngine { - - Function run(long x); - - Function run(); -} +package org.graphast.piecewise; + +public interface IManipulatorEngine { + + Function run(long x); +}