| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.core.tsp; |
| | | |
| | | /** |
| | | * èç±»åç» |
| | | */ |
| | | import com.doumee.core.utils.Constants; |
| | | import com.doumee.dao.admin.request.SketchCateModel; |
| | | import com.doumee.dao.business.model.JkSketchCustomer; |
| | | import com.doumee.service.business.impl.JkSketchServiceImpl; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | public class Clustering { |
| | | public static List<SketchCateModel> clusterPoints(List<JkSketchCustomer> points, double threshold) { |
| | | |
| | | List<SketchCateModel> clusters = new ArrayList<>(); |
| | | boolean[] visited = new boolean[points.size()]; |
| | | int index =0; |
| | | for (int i = 0; i < points.size(); i++) { |
| | | if (!visited[i]) { |
| | | List<JkSketchCustomer> cluster = new ArrayList<>(); |
| | | dfs(points, visited, cluster, i, threshold); |
| | | SketchCateModel sketchCateModel = new SketchCateModel(); |
| | | sketchCateModel.setCustomerList(cluster); |
| | | sketchCateModel.setId(index); |
| | | sketchCateModel.setStartPoint(cluster.get(0)); |
| | | for (JkSketchCustomer c : cluster){ |
| | | sketchCateModel.setTotalNum(Constants.formatBigdecimal(sketchCateModel.getTotalNum()).add(Constants.formatBigdecimal(c.getTotalNum()))); |
| | | } |
| | | sketchCateModel.setTotalCustomer(cluster.size()); |
| | | clusters.add(sketchCateModel); |
| | | } |
| | | } |
| | | // æå°æ¯ä¸ªèç±»çç¹ |
| | | for (int i = 0; i < clusters.size(); i++) { |
| | | System.out.println("Cluster " + (i + 1) + ": " + clusters.get(i).getStartPoint().getName()+ ": " + clusters.get(i).getCustomerList().size()); |
| | | } |
| | | return clusters; |
| | | } |
| | | public static double distanceTo(JkSketchCustomer self, JkSketchCustomer other) { |
| | | // ====æ è®°==忽ç¥äº¤éè§åè·ç¦»===== |
| | | /*List<DistanceMapParam> distanceMapParamList =JkSketchServiceImpl.getListFromJsonStr(self.getDistanceJson()); |
| | | DistanceMapParam param = JkSketchServiceImpl.getParamByCustomerIds( other.getId(),distanceMapParamList); |
| | | if(param!=null && param.getDistance()!=0){//妿ä¹åå·²ç»è·åè¿ |
| | | return (param.getDistance()); |
| | | }*/ |
| | | return DistanceCalculator.calculateDistance(Constants.formatBigdecimal(self.getLatitude()).doubleValue() |
| | | ,Constants.formatBigdecimal(self.getLongitude()).doubleValue() |
| | | ,Constants.formatBigdecimal(other.getLatitude()).doubleValue() |
| | | ,Constants.formatBigdecimal(other.getLongitude()).doubleValue()); |
| | | } |
| | | private static void dfs(List<JkSketchCustomer> points, boolean[] visited, List<JkSketchCustomer> cluster, int startIndex, double threshold) { |
| | | visited[startIndex] = true; |
| | | cluster.add(points.get(startIndex)); |
| | | JkSketchCustomer startPoint = points.get(startIndex); |
| | | |
| | | for (int i = 0; i < points.size(); i++) { |
| | | if (!visited[i]) { |
| | | double distance = distanceTo(startPoint,points.get(i)); |
| | | if (distance <= threshold) { |
| | | dfs(points, visited, cluster, i, threshold); // é彿·»å å°èç±»ä¸ |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 117°40â²ï½118°44â²ãå纬30°19â²ï½31°34â² |
| | | * @param args |
| | | */ |
| | | public static void main(String[] args) { |
| | | List<JkSketchCustomer> points = new ArrayList<>(); |
| | | for (int i = 0; i <3000; i++) { |
| | | JkSketchCustomer a = new JkSketchCustomer(); |
| | | a.setLatitude(new BigDecimal(30.19d+(30.54d-30.19d)*Math.random())); |
| | | a.setLongitude(new BigDecimal(117.40+(117.74d-117.40d)*Math.random())); |
| | | a.setName("客æ·"+i); |
| | | points.add(a); |
| | | } |
| | | |
| | | double threshold = 1000; // 设置è·ç¦»éå¼ï¼è¶
è¿è¿ä¸ªè·ç¦»å°±ä¸å±äºåä¸èç±»ã |
| | | clusterPoints(points, threshold); |
| | | } |
| | | } |