package com.rongyi.platform.game.web.data;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 参考:https://blog.csdn.net/weixin_73733267/article/details/135144512
* https://cloud.tencent.com/developer/article/1399322
*/
public class Compare {
// 初始化数据
public static List<Double> init(List<Double> data){
// int[] arr = {100};
// int targetLength = findClosestValue(arr,data.size());
return initLength(data,100);
}
// 找最接近的数
public static int findClosestValue(int[] arr, int target) {
int[] temp = new int[arr.length];
for (int i = 0;i < arr.length;i++) {
temp[i] = Math.abs(arr[i] - target);
}
int finalTemp = temp[0];
int index = 0;
for (int i = 1;i < temp.length;i++) {
if(temp[i] < finalTemp){
finalTemp = temp[i];
index = i;
}
}
return arr[index];
}
// 标准模型的初始化 TODO
public static List<Double> initLength(List<Double> data1,int targetLength){
// 扩展
data1 = extendGtLength(data1,targetLength);
// 裁剪
data1 = extendEqLength(data1,targetLength);
System.out.println("扩展后的数据:" + data1);
if(data1.size() != targetLength){
throw new RuntimeException("扩展裁剪有问题");
}
// 归一化
MinMaxScaler minMaxScaler = new MinMaxScaler(data1);
return minMaxScaler.scale();
}
/**
* 把数组data扩展到data的长度大于length。
*/
private static List<Double> extendGtLength(List<Double> data,int length){
if(data.size() >= length){
return minuteBetweenLength(data,length);
}
// 数据扩充
List<Double> temp = new ArrayList<>();
// if(data.size() * 2 <= length){
// 扩1倍
for (int i = 0; i < data.size(); i++) {
temp.add(data.get(i));
if(i < data.size() - 1){
temp.add((data.get(i + 1) + data.get(i)) / 2);
}
}
// }else if((data.size() * 1.5 <= length)){
// // 扩0.5倍
// for (int i = 0; i < data.size(); i++) {
// temp.add(data.get(i));
// if (i != 0 && i % 2 == 0) {
// temp.add((data.get(i + 1) + data.get(i)) / 2);
// }
// }
// }else{
// // 扩0.25倍
// for (int i = 0; i < data.size(); i++) {
// temp.add(data.get(i));
// if (i != 0 && i % 4 == 0) {
// temp.add((data.get(i + 1) + data.get(i)) / 2);
// }
// }
// }
if(temp.size() >= length){
return temp;
}
System.out.println("扩" + temp);
return extendGtLength(temp,length);
}
// 缩减到length长度。
private static List<Double> minuteBetweenLength(List<Double> data,int length){
if(data.size() < length){
throw new RuntimeException("不符合缩减条件");
}
if(data.size() >= length && data.size() < length * 2){
return data;
}
int x = data.size() / length;
// 缩减
List<Double> temp = new ArrayList<>();
for(int i = 0; i < data.size();i = i + x){
temp.add(data.get(i));
}
return minuteBetweenLength(temp, temp.size());
}
// 缩减到length长度。
private static List<Double> extendEqLength(List<Double> data,int length){
if(data.size() >= length && data.size() * 2 < length){
throw new RuntimeException("不符合extendEqLength条件");
}
if(data.size() == length){
return data;
}
// 需要减少的数量
int elementsToRemove = data.size() -length;
// 每间隔x,忽略此数据
int x = data.size() / elementsToRemove;
List<Double> temp = new ArrayList<>();
temp.add(data.get(0));
int removeNum = 0;
for (int i = 1; i < data.size();i++){
if(!(i % x == 0)){
temp.add(data.get(i));
}else if(removeNum<elementsToRemove){
removeNum++;
}else{
temp.add(data.get(i));
}
}
if(temp.size() < length){
temp.add(data.get(data.size() - 1));
}
if(temp.size() > length){
temp.remove(data.get(data.size() - 1));
}
return temp;
}
public static void main(String[] args) {
List<Double> data1 = Arrays.asList(10.1,10.5,11.2,10.8,12.0,12.5,11.5,12.5,13.5,13.1,14.5,14.3,15.8,16.0,17.0,16.5,16.5,17.1,16.5,15.2,14.8,14.0,14.5,13.5,12.5,11.1,11.5,11.2,11.8,11.0,10.5,10.5);
List<Double> list1 = init(data1);
System.out.println("归一化1后:" + JSON.toJSONString(list1));
// List<Double> data2 = Arrays.asList(33.3,35.6,38.4,37.3,38.4,39.5,40.5,42.6,41.6,43.6,44.6,50.0,48.5,46.5,44.5,42.5,42.5,42.0,41.0,38.5,37.5,36.5,36.5,34.5,36.5,37.5,34.5,33.5,33.5,31.5,30.5);
List<Double> data2 = Arrays.asList(50.0,48.5,46.5,44.5,42.5,42.5,42.0,41.0,38.5,37.5,36.5,36.5,34.5,36.5,37.5,34.5,33.5,33.5,31.5,30.5,33.3,35.6,38.4,37.3,38.4,39.5,40.5,42.6,41.6,43.6,44.6);
List<Double> list2 = init(data2);
System.out.println("归一化2后:" + JSON.toJSONString(list2));
System.out.println("长度:" + data2.size());
double distance = EuclideanDistance.calculateEuclideanDistance(list1, list2);
System.out.println("欧式距离: " + distance);
System.out.println("欧式距离相似度: " + (100- distance));
}
// public static void main1(String[] args) {
// List<Double> data1 = Arrays.asList(10.1,10.5,11.2,10.8,12.0,12.5,11.5,10.5,10.5,10.1,10.5,10.5,10.8,13.0,13.0,11.5,10.5,10.1,10.5,11.2,10.8,12.0,11.5,10.5,10.5,10.1,10.5,11.2,10.8,13.0,12.5,11.5,10.5,10.5);
// List<Double> list1 = init(data1);
// System.out.println("归一化1后:" + JSON.toJSONString(list1));
// List<Double> data2 = Arrays.asList(20.8,22.2,25.0,27.5,30.0,33.2,27.4,25.0,23.0,22.2,20.8,22.2,25.0,27.5,27.5,30.0,33.2,26.4,23.3,22.2,22.0,22.2,25.0,27.5,30.0,33.2,27.4,23.3,22.2,20.8,22.2,25.0,27.5,30.0);
// // List<Double> data2 = Arrays.asList(33.2,26.4,23.3,22.2,22.01,22.2,25.01,27.5,30.01,33.2,27.4,23.3,22.2,20.8,22.2,25.01,27.5,30.01,28.4,27.8,24.6,23.5,22.1,26.5,24.6,26.5,27.5,28.5,29.4,30.2,30.6,30.5,30.5,30.5);
// List<Double> list2 = init(data2);
// System.out.println("归一化2后:" + JSON.toJSONString(list2));
// System.out.println("长度:" + data2.size());
// double distance = EuclideanDistance.calculateEuclideanDistance(list1, list2);
// System.out.println("欧式距离: " + distance);
// System.out.println("欧式距离相似度: " + (10- distance));
//
// }
// public static void main22(String[] args) {
// List<Double> data1 = Arrays.asList(10.1,10.5,11.2,10.8,12.0,12.5,11.5,10.5,10.5,10.1,10.5,10.5,10.8,13.0,13.0,11.5,10.5,10.1,10.5,11.2,10.8,12.0,11.5,10.5,10.5,10.1,10.5,11.2,10.8,13.0,12.5,11.5,10.5,10.5,10.1,10.5,11.2,10.8,12.0,12.5,11.5,10.5,10.5,10.1,10.5,10.5,10.8,13.0,13.0,11.5);
// List<Double> list1 = init(data1);
// System.out.println("归一化1后:" + JSON.toJSONString(list1));
// // List<Double> data2 = Arrays.asList(20.8,22.2,25.0,27.5,30.0,33.2,27.4,25.0,23.0,22.2,20.8,22.2,25.0,27.5,27.5,30.0,33.2,26.4,23.3,22.2,22.0,22.2,25.0,27.5,30.0,33.2,27.4,23.3,22.2,20.8,22.2,25.0,27.5,30.0,20.8,22.2,25.0,27.5,30.0,33.2,27.4,25.0,23.0,22.2,20.8,22.2,25.0,27.5,27.5,30.0);
// // List<Double> data2 = Arrays.asList(33.2,26.4,23.3,22.2,22.01,22.2,25.01,27.5,30.01,33.2,27.4,23.3,22.2,21.2,22.2,25.01,27.5,30.01,28.4,27.8,24.6,23.5,22.1,26.5,24.6,26.5,27.5,28.5,29.4,30.2,30.6,30.5,30.5,30.5,33.2,26.4,23.3,22.2,22.01,22.2,25.01,27.5,30.01,33.2,27.4,23.3,22.2,21.2,22.2,25.01);
// List<Double> data2 = Arrays.asList(33.2,26.4,23.3,22.2,15.01,22.2,25.01,26.5,30.01,33.2,27.4,14.3,22.2,21.2,22.2,25.01,27.5,30.01,35.4,28.8,24.6,23.5,22.1,26.5,24.6,26.5,27.5,28.5,29.4,25.2,30.6,30.5,30.5,39.5,33.2,26.4,23.3,22.2,22.01,22.2,25.01,27.5,30.01,33.2,30.4,23.3,22.2,21.2,30.2,25.01);
// List<Double> list2 = init(data2);
// System.out.println("归一化2后:" + JSON.toJSONString(list2));
// System.out.println("长度:" + data2.size());
// double distance = EuclideanDistance.calculateEuclideanDistance(list1, list2);
// System.out.println("欧式距离: " + distance);
// System.out.println("欧式距离相似度: " + (100- distance));
//
// }
private static double process(double distance){
double sim = 100 - distance;
if(sim < 10){
return 0.0;
}
if(sim > 90){
return 100;
}
return 10 * (sim-10) / 8;
}
public List<Double> pre(List<Double> data1,int length){
return null;
}
public static void main2(String[] args) {
String str = "33.2 26.4 23.3 22.2 22.01 22.2 25.01 27.5 30.01 33.2 27.4 23.3 22.2 20.8 22.2 25.01 27.5 30.01 28.4 27.8 24.6 23.5 22.1 26.5 24.6 26.5 27.5 28.5 29.4 30.2 30.6 30.5 30.5 30.5";
String replace = str.replace(" ", ",");
System.out.println("结构:" + replace);
}
public static void main22(String[] args) {
String str= "0.0,0.0,0.0,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.379310344827586,0.379310344827586,0.379310344827586,0.24137931034482793,0.24137931034482793,0.6551724137931035,0.6551724137931035,0.6551724137931035,0.8275862068965517,0.8275862068965517,0.8275862068965517,0.48275862068965525,0.48275862068965525,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.0,0.0,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.24137931034482793,0.24137931034482793,1.0,1.0,1.0,1.0,1.0,1.0,0.48275862068965525,0.48275862068965525,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.0,0.0,0.0,0.13793103448275873,0.13793103448275873,0.379310344827586,0.379310344827586,0.379310344827586,0.24137931034482793,0.24137931034482793,0.24137931034482793,0.6551724137931035,0.6551724137931035,0.48275862068965525,0.48275862068965525,0.48275862068965525,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.0,0.0,0.0,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.379310344827586,0.379310344827586,0.379310344827586,0.24137931034482793,0.24137931034482793,0.24137931034482793,0.24137931034482793,1.0,1.0,1.0,1.0,0.8275862068965517,0.8275862068965517,0.8275862068965517,0.8275862068965517,0.48275862068965525,0.48275862068965525,0.48275862068965525,0.48275862068965525,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873";
String replace = str.replace(",", " ");
System.out.println("结构:" + replace);
}
}