diff --git a/src/main/java/com/peteralbus/controller/FireCenterController.java b/src/main/java/com/peteralbus/controller/FireCenterController.java index c7b0c70..7640b78 100644 --- a/src/main/java/com/peteralbus/controller/FireCenterController.java +++ b/src/main/java/com/peteralbus/controller/FireCenterController.java @@ -4,6 +4,7 @@ import com.peteralbus.entity.*; import com.peteralbus.service.EarthquakeInfoService; import com.peteralbus.service.EstimateService; import com.peteralbus.service.FireCenterService; +import com.peteralbus.util.EstimateUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; @@ -12,159 +13,186 @@ import org.springframework.web.bind.annotation.RestController; import java.util.*; +/** + * The type Fire center controller. + */ @CrossOrigin @RestController public class FireCenterController { + private FireCenterService fireCenterService; + private EstimateController estimateController; + private EstimateService estimateService; + private EarthquakeInfoService earthquakeInfoService; + private EstimateUtil estimateUtil; + /** + * The Max fire center count. + */ + static Integer MAX_FIRE_CENTER_COUNT = 20; + + /** + * Sets fire center service. + * + * @param fireCenterService the fire center service + */ @Autowired - FireCenterService fireCenterService; + public void setFireCenterService(FireCenterService fireCenterService) { + this.fireCenterService = fireCenterService; + } + + /** + * Sets estimate controller. + * + * @param estimateController the estimate controller + */ @Autowired - EstimateController estimateController; + public void setEstimateController(EstimateController estimateController) { + this.estimateController = estimateController; + } + + /** + * Sets estimate service. + * + * @param estimateService the estimate service + */ @Autowired - EstimateService estimateService; + public void setEstimateService(EstimateService estimateService) { + this.estimateService = estimateService; + } + + /** + * Sets earthquake info service. + * + * @param earthquakeInfoService the earthquake info service + */ + @Autowired + public void setEarthquakeInfoService(EarthquakeInfoService earthquakeInfoService) { + this.earthquakeInfoService = earthquakeInfoService; + } + + /** + * Sets estimate util. + * + * @param estimateUtil the estimate util + */ @Autowired - EarthquakeInfoService earthquakeInfoService; + public void setEstimateUtil(EstimateUtil estimateUtil) { + this.estimateUtil = estimateUtil; + } + + /** + * Gets all fire center. + * + * @return the all fire center + */ @RequestMapping("/getAllFireCenter") - public List getAllFireCenter(){ + public List getAllFireCenter() { return fireCenterService.getAllFireCenter(); } - //获取一个地震区域的消防站位置 - public List getOnePlaceAllFireCenter(Long earthquakeId){ - List fireCenters=getAllFireCenter(); - List fireCenterList=new ArrayList<>(); - MapmapParameter=new HashMap<>(); - mapParameter.put("earthquakeId",earthquakeId); - EarthquakeInfo earthquakeInfo=earthquakeInfoService.queryInfoWithLine(mapParameter).get(0); - for(FireCenter fireCenter:fireCenters){ - double distanceTwoPlaces=getDistance(fireCenter.getFireLon(),fireCenter.getFireLat(),earthquakeInfo.getLongitude(),earthquakeInfo.getLatitude()); - List intensityLineList=earthquakeInfo.getIntensityLineList(); - if(distanceTwoPlaces getOnePlaceAllFireCenter(Long earthquakeId) { + List fireCenters = fireCenterService.getAllFireCenter(); + List fireCenterList = new ArrayList<>(); + Map mapParameter = new HashMap<>(); + mapParameter.put("earthquakeId", earthquakeId); + EarthquakeInfo earthquakeInfo = earthquakeInfoService.queryInfoWithLine(mapParameter).get(0); + for (FireCenter fireCenter : fireCenters) { + double distanceTwoPlaces = getDistance(fireCenter.getFireLon(), fireCenter.getFireLat(), earthquakeInfo.getLongitude(), earthquakeInfo.getLatitude()); + List intensityLineList = earthquakeInfo.getIntensityLineList(); + if (distanceTwoPlaces < intensityLineList.get(intensityLineList.size() - 1).getLongRadius()) { fireCenterList.add(fireCenter); } } return fireCenterList; } + + /** + * Gets one fire center. + * + * @param id the id + * @return the one fire center + */ @RequestMapping("/getOneFireCenter") - public FireCenter getOneFireCenter(int id){ + public FireCenter getOneFireCenter(int id) { return fireCenterService.getAllFireCenter().get(id); } + + /** + * 根据地震ID,寻找附近的消防站,并随机选取fireCenterCount个进行物资分配计算. + * + * @param earthquakeId 地震ID + * @param fireCenterCount 物资分配的救援点数量 + * @return FireWeight,救援点权重,包含救援点信息 + */ @RequestMapping("/findFireCenterNearby") - public List getFireCenterWeight(Long earthquakeId){ - List OnePlaceAllFireCenter=getOnePlaceAllFireCenter(earthquakeId); - List FireCenterIntensityArr=new ArrayList<>(); - for(FireCenter fireCenter:OnePlaceAllFireCenter){ - FireCenterIntensityArr.add(estimateController.getPointIntensity(earthquakeId, fireCenter.getFireLon(), fireCenter.getFireLat())); - } - int FireCenterIntensityArSize=FireCenterIntensityArr.size(); - System.out.print("FireCenterIntensityArSize长度为"+FireCenterIntensityArSize); - if(FireCenterIntensityArSize==0){ + public List getFireCenterWeight(Long earthquakeId, Integer fireCenterCount) { + if (fireCenterCount > MAX_FIRE_CENTER_COUNT) { + // 自行修改封装后的返回值,错误:资助点数量不能过多 return null; } - Mapmap = new HashMap(); - for(int i=0;imax) { - max=arr[j][i]; - } - } - for(int i=0;i fireCenterList = getOnePlaceAllFireCenter(earthquakeId); + List randomFireCenterList = new ArrayList<>(); + Random random = new Random(); + // 随机选取 fireCenterCount 个消防站 + for (int i = 0; i < fireCenterCount; i++) { + if(fireCenterList.size()==0){ + break; } - e[j]=(-k)*sumArrNewHang; - g[j]=1-e[j]; + int index = random.nextInt(fireCenterList.size()); + randomFireCenterList.add(fireCenterList.get(index)); + fireCenterList.remove(index); } - double[] a=new double[10];//每个属性所占的权数 - double SumG=0.0; - for(int i=0;i arrayW1=new ArrayList<>(); - Map topTen=new LinkedHashMap(); - List resultFireWeightLists=new ArrayList<>(); - for(int i=0;i> entrys=new ArrayList<>(arrayStrict.entrySet()); - Collections.sort(entrys, new MyComparator()); - //输出排序后的键值对 - int count=0; - for(Map.Entry entry:entrys){ - topTen.put(entry.getKey(),entry.getValue()); - FireCenter resultFireCenter=getOneFireCenter(map.get(entry.getKey())); - FireWeight fireWeight=new FireWeight(); - fireWeight.setFireId(resultFireCenter.getFireId()); - fireWeight.setFireLat(resultFireCenter.getFireLat()); - fireWeight.setFireLon(resultFireCenter.getFireLon()); - fireWeight.setFireName(resultFireCenter.getFireName()); - fireWeight.setFireAddress(resultFireCenter.getFireAddress()); - fireWeight.setFireCenterWeight(entry.getValue()); + Double[] weights = estimateUtil.entropyMethod(arr,randomFireCenterListSize,3,new ArrayList(Arrays.asList(0,1,2))); + List resultFireWeightLists = new ArrayList<>(); + for (int i=0;igetAllFireCenter(); + List getAllFireCenter(); } diff --git a/src/main/java/com/peteralbus/service/impl/FireCenterServiceImpl.java b/src/main/java/com/peteralbus/service/impl/FireCenterServiceImpl.java index 69a6f43..342f261 100644 --- a/src/main/java/com/peteralbus/service/impl/FireCenterServiceImpl.java +++ b/src/main/java/com/peteralbus/service/impl/FireCenterServiceImpl.java @@ -8,11 +8,23 @@ import org.springframework.stereotype.Service; import java.util.List; +/** + * The type Fire center service. + * @author wuhon + */ @Service public class FireCenterServiceImpl implements FireCenterService { + /** + * The Fire center mapper. + */ FireCenterMapper fireCenterMapper; + /** + * Sets fire center mapper. + * + * @param fireCenterMapper the fire center mapper + */ @Autowired public void setFireCenterMapper(FireCenterMapper fireCenterMapper) { this.fireCenterMapper = fireCenterMapper; diff --git a/src/main/java/com/peteralbus/util/EstimateUtil.java b/src/main/java/com/peteralbus/util/EstimateUtil.java index 57bef95..54ccde8 100644 --- a/src/main/java/com/peteralbus/util/EstimateUtil.java +++ b/src/main/java/com/peteralbus/util/EstimateUtil.java @@ -1,12 +1,17 @@ package com.peteralbus.util; +import com.peteralbus.entity.FireWeight; import org.springframework.stereotype.Component; import java.time.LocalDateTime; +import java.util.Arrays; import java.util.Date; +import java.util.List; +import java.util.Map; /** * The type Estimate util. + * * @author PeterAlbus */ @Component @@ -15,14 +20,16 @@ public class EstimateUtil /** * death predict int. * - * @param population the population - * @param magnitude the magnitude + * @param earthquakeId the earthquake id + * @param population the population + * @param magnitude the magnitude + * @param intensity the intensity * @param earthquakeTime the time of earthquake - * @param longitude the longitude - * @param latitude the latitude + * @param longitude the longitude + * @param latitude the latitude * @return the double */ - public double deathPredict(Long earthquake_id,int population, double magnitude, double intensity, LocalDateTime earthquakeTime,Double longitude,Double latitude) + public double deathPredict(Long earthquakeId,int population, double magnitude, double intensity, LocalDateTime earthquakeTime,Double longitude,Double latitude) { double deathPredict = 0; /*M为震级 I为震中强度*/ @@ -67,7 +74,82 @@ public class EstimateUtil * @return the double */ public double economyPredict(double highIntensity) - {return Math.pow(10,0.84444*highIntensity-1.831)/10000; + { + return Math.pow(10,0.84444*highIntensity-1.831)/10000; + } + + /** + * 使用熵值法计算每个点的权重 + * + * @param data 二维数组,pointCount个点的parameterCount个参考指标 + * @param pointCount 救援点数量 + * @param parameterCount 参考指标数量 + * @param positiveIndicatorsIndex 参考指标中为正向指标的索引 + * @return 权重列表Double[pointCount], 依次对应data中的每个点 + */ + public Double[] entropyMethod(double[][] data, int pointCount, int parameterCount, List positiveIndicatorsIndex) { + Double[] max = new Double[parameterCount]; + Double[] min = new Double[parameterCount]; + Double[] sum = new Double[parameterCount]; + // 求出每个属性的最大值、最小值 + for(int i = 0; i < parameterCount; i++) { + max[i] = Double.MIN_VALUE; + min[i] = Double.MAX_VALUE; + sum[i] = 0.0; + for(int j=0;j max[i]) { + max[i] = data[j][i]; + } + if(data[j][i] < min[i]) { + min[i] = data[j][i]; + } + } + } + // 归一化 + Double[][] normalizedData = new Double[pointCount][parameterCount]; + for(int i = 0; i < parameterCount; i++) { + for(int j=0;j < pointCount;j++) { + if(positiveIndicatorsIndex.contains(i)) { + normalizedData[j][i] = (data[j][i] - min[i]) / (max[i] - min[i]); + } else { + normalizedData[j][i] = (max[i] - data[j][i]) / (max[i] - min[i]); + } + sum[i] += normalizedData[j][i]; + } + } + // 每个属性指标值在该属性所有数值中的比例 + Double[][] percentage = new Double[pointCount][parameterCount]; + for(int i = 0; i < parameterCount; i++) { + for(int j=0;j mapParameter = new HashMap(); -// mapParameter.put("earthquakeId",16); -// EarthquakeInfo earthquakeInfo=earthquakeInfoService.queryInfoWithLine(mapParameter).get(0); -// double magnitude = earthquakeInfo.getMagnitude(), -// highIntensity = earthquakeInfo.getHighIntensity(), -// longitude = earthquakeInfo.getLongitude(), -// latitude = earthquakeInfo.getLatitude(), -// longRadius = earthquakeInfo.getIntensityLineList().get(2).getLongRadius(), -// shortRadius = earthquakeInfo.getIntensityLineList().get(2).getShortRadius(); -// System.out.println(longRadius); -// LocalDateTime earthquakeTime = earthquakeInfo.getEarthquakeTime(); -// //将角度转换为弧度。 -// double radians = Math.toRadians(latitude); -// double minLongitude = longitude-shortRadius/(111-Math.cos(radians)), -// maxLongitude = longitude+shortRadius/(111-Math.cos(radians)), -// minLatitude = latitude-longRadius/111, -// maxLatitude = latitude+longRadius/111; -// int population=(int)estimateService.getPopulation(minLongitude,maxLongitude,minLatitude,maxLatitude); -// double death=estimateUtil.deathPredict(earthquakeInfo.getEarthquakeId(),population,magnitude,highIntensity,earthquakeTime,longitude,latitude); -// long endTime = System.currentTimeMillis(); -// long usedTime = (endTime-startTime)/1000; -// System.out.println(death + " " + usedTime); -// getPopulation(100.7342,25.5736); -// getPopulation(99.9586,25.6753); -// getPopulation(100.1926,25.9121); -// getPopulation(100.1885,25.9252); -// getPopulation(99.7768,25.3342); -// getPopulation(99.9586,25.6752); -// getPopulation(100.3105,25.6775); -// getPopulation(100.1209,25.7921); -// getPopulation(100.4937,25.3366); + System.out.println(fireCenterController.getFireCenterWeight(16L, 12)); +// double[][] data = {{6.962645,67101.0,730.3979,1.3}, +// {5.643831,50732.0,170.8517,1.7}, +// {7.775079,39755.0,89.2054,1.0}, +// {6.568763,67101.0,294.3379,1.0}, +// {6.552100,67101.0,281.1741,1.9}, +// {6.669364,43274.0,41.5144,1.2}, +// {7.774892,39755.0,89.2054,1.4}, +// {6.511145,67101.0,518.4596,1.4}, +// {6.966898,67101.0,382.3772,1.7}, +// {5.894564,35641.0,374.3219,1.2}}; +// List list = new ArrayList<>(); +// list.add(0); +// list.add(2); +// list.add(3); +// System.out.println(Arrays.toString(estimateUtil.entropyMethod(data, 10, 4, list))); } void getPopulation(double longitude,double latitude)