Guest User

Untitled

a guest
Aug 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.57 KB | None | 0 0
  1. /* вычисление координат станций с помощью force-based алгоритма
  2.  *
  3.  * @author nastia.malysheva
  4.  */
  5.    
  6. import java.sql.*;
  7. import java.util.*;
  8.  
  9. class Station {
  10.  
  11.     private int esr;
  12.     private boolean fixed;
  13.     private double x, y;
  14.     private double fx, fy;
  15.  
  16.     Station(int esr, double x, double y, boolean fixed) {
  17.         this.esr = esr;
  18.         this.x = x;
  19.         this.y = y;
  20.         this.fixed = fixed;
  21.         System.out.println(this);
  22.     }
  23.  
  24.     boolean isFixed() {
  25.         return fixed;
  26.     }
  27.  
  28.     int getEsr() {
  29.         return esr;
  30.     }
  31.  
  32.     public double getFx() {
  33.         return fx;
  34.     }
  35.  
  36.     public double getFy() {
  37.         return fy;
  38.     }
  39.  
  40.     public double getX() {
  41.         return x;
  42.     }
  43.  
  44.     public double getY() {
  45.         return y;
  46.     }
  47.  
  48.     public void addF(double fx, double fy) {
  49.         this.fx += fx;
  50.         this.fy += fy;
  51.     }
  52.  
  53.     public void addXY(double x, double y) {
  54.         this.x += x;
  55.         this.y += y;
  56.     }
  57.  
  58.     public void zeroF() {
  59.         fx = 0;
  60.         fy = 0;
  61.     }
  62.  
  63.     public String toString() {
  64.         return "id: " + esr + ", x: " + x + " y: " + y;
  65.     }
  66.  
  67. }
  68.  
  69. //список станций
  70. class Stations extends ArrayList<Station> {
  71.  
  72.     void add(int esr, double x, double y, boolean fixed) {
  73.         super.add(new Station(esr, x, y, fixed));
  74.     }
  75.    
  76.     void zeroForces() {
  77.         for (Station s : this) {
  78.             s.zeroF();
  79.         }
  80.     }
  81. }
  82.  
  83. //граф связей станций
  84. class Connections extends HashMap<Integer, List<Integer>> {
  85.  
  86.     public void put(int esrFrom, int esrTo) {
  87.         if (containsKey(esrFrom)) {
  88.             get(esrFrom).add(esrTo);
  89.         } else {
  90.             List<Integer> list = new ArrayList<Integer>();
  91.             list.add(esrTo);
  92.             put(esrFrom, list);
  93.         }
  94.     }
  95.  
  96.     public boolean isConnected(int esrFrom, int esrTo) {
  97.         if (containsKey(esrFrom)) {
  98.             if (get(esrFrom).contains(esrTo)) {
  99.                 return true;
  100.             }
  101.         }
  102.  
  103.         if (containsKey(esrTo)) {
  104.             if (get(esrTo).contains(esrFrom)) {
  105.                 return true;
  106.             }
  107.         }
  108.         return false;
  109.     }
  110. }
  111.  
  112. public class StationsCoord {
  113.  
  114.     private static Stations stations = new Stations();
  115.     private static Connections connections = new Connections();
  116.     public static final int XMIN = 6400000;
  117.     public static final int YMIN = 5600000;
  118.     public static final int DX = 900000;
  119.     public static final int DY = 500000;
  120.  
  121.     //iterationsNum - количество итераций force-based алгоритма
  122.     //чем больше, тем выше качество, но ниже производительность
  123.     public static void calculate(int iterationsNum) {
  124.         try {      
  125.             Class.forName("com.mysql.jdbc.Driver");
  126.             String connectionUrl = "jdbc:mysql://localhost/skat?user=root&password=0Y2uiPCt2ARj";
  127.             Connection dbConn = DriverManager.getConnection(connectionUrl);
  128.             String sql = "select esr, fixed, latitude, longitude  from  stations";          
  129.             ResultSet rs = dbConn.createStatement().executeQuery(sql);
  130.                        
  131.             //координаты  станций должны быть уникальны
  132.             Set<Long> setUniq = new HashSet<Long>();
  133.             Random rnd = new Random();
  134.             int esr;
  135.             double x, y;
  136.             boolean fixed;
  137.             long key;
  138.  
  139.             while (rs.next()) {
  140.                 esr = Integer.parseInt(rs.getString("esr"));
  141.                
  142.                 //fixed - у таких станций координаты заданы вручную
  143.                 if (rs.getString("fixed") != null) {
  144.                     fixed = true;
  145.                 } else {
  146.                     fixed = false;
  147.                 }
  148.                
  149.                 //если в БД нет координат, они задаются случайно                
  150.                 if (rs.getString("latitude") == null || rs.getString("longitude") == null) {
  151.                     while (true) {
  152.                         x = rnd.nextInt(DX) + XMIN;
  153.                         y = rnd.nextInt(DY) + YMIN;
  154.                         key = (long) (x * 10000000L + y);
  155.                         if (!setUniq.contains(key)) {
  156.                             setUniq.add(key);
  157.                             break;
  158.                         }
  159.                         x /= 100000;
  160.                         y /= 100000;
  161.                     }
  162.                     if (fixed) {
  163.                         fixed = false;
  164.                     }
  165.                 } else {
  166.                     x = (int) (Double.parseDouble(rs.getString("latitude")) * 100000);  
  167.                     y = (int) (Double.parseDouble(rs.getString("longitude")) * 100000);
  168.                     key = (long) (x * 10000000L + y);
  169.                     setUniq.add(key);
  170.                 }
  171.                 stations.add(esr, x, y, fixed);
  172.             }
  173.  
  174.             sql = "select esrfrom, esrto from nsi_run_list";
  175.             rs = dbConn.createStatement().executeQuery(sql);
  176.  
  177.             while (rs.next()) {
  178.                 int esrFrom = Integer.parseInt(rs.getString("esrfrom"));
  179.                 int esrTo = Integer.parseInt(rs.getString("esrto"));
  180.                 connections.put(esrFrom, esrTo);
  181.             }
  182.  
  183.             //начало работы алгоритма
  184.             double kCoulomb = 0.00005;
  185.             double kHooke = 0.1;
  186.            
  187.             for (int step = 0; step < iterationsNum; step++) {
  188.                 stations.zeroForces();
  189.                 for (Station stationFrom : stations) {
  190.                     for (Station stationTo : stations) {
  191.                         if (stationFrom.getEsr() == stationTo.getEsr() || stationTo.isFixed()) {
  192.                             continue;
  193.                         }
  194.                         double dx = stationTo.getX() - stationFrom.getX();
  195.                         double dy = stationTo.getY() - stationFrom.getY();
  196.                         double dist = Math.sqrt(dx * dx + dy * dy);                        
  197.  
  198.                         double force = kCoulomb / (dist * dist);
  199.  
  200.                         if (connections.isConnected(stationFrom.getEsr(), stationTo.getEsr())) {
  201.                             force += -kHooke * (dist - 0.01);                            
  202.                         }
  203.  
  204.                         stationTo.addF(force * dx / dist, force * dy / dist);
  205.                     }
  206.                 }
  207.                 for (Station s : stations) {
  208.                     s.addXY(s.getFx(), s.getFy());
  209.                 }
  210.             }
  211.  
  212.             for (Station s : stations) {  
  213.                 sql = "update stations set coord=GeomFromWKB(Point(" +  
  214.                       s.getX() + "," + s.getY() + "),4326)," +
  215.                       " latitude=" + s.getX() + ", longitude=" + s.getY() +
  216.                       " where esr=" + s.getEsr();
  217.                 dbConn.createStatement().executeUpdate(sql);
  218.             }
  219.         } catch (SQLException e) {
  220.             System.out.println("SQL Exception: " + e.toString());
  221.         } catch (ClassNotFoundException cE) {
  222.             System.out.println("Class Not Found Exception: " + cE.toString());
  223.         }
  224.     }
  225.    
  226.     static public void String toString() {
  227.         return stations.toString();
  228.     }
  229.  
  230.     public static void main(String[] args) {
  231.         StationsCoord.calculate(10000);
  232.         System.out.println(StationsCoord);
  233.     }
  234. }
Add Comment
Please, Sign In to add comment