package Histogram;
/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import java.util.Vector;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
/**
* A chart that displays rectangular bars with heights indicating data values
* for categories. Used for displaying information when at least one axis has
* discontinuous or discrete data.
*
* @see javafx.scene.chart.BarChart
* @see javafx.scene.chart.Chart
* @see javafx.scene.chart.Axis
* @see javafx.scene.chart.CategoryAxis
* @see javafx.scene.chart.NumberAxis
*
*/
public class V_Optimal extends Application {
private static int[] DataSet={1,3,4,7,2,8,3,6,3,6,8,2,1,6,3,5,3,4,7,2,6,7,2};
private static int BucketNum=3;
//定义变量,保存相应的分段数值
private static int a_sec;
private static int b_sec;
private static int c_sec;
private static int minValue;
private static int maxValue;
//定义变量,保存每段的频数
private static int a_num=0;
private static int b_num=0;
private static int c_num=0;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
//x轴
String aname="["+ minValue +","+ (minValue + a_sec) + "]";
String bname="["+ (minValue + a_sec + 1)+ "," + (minValue + a_sec + b_sec) + "]";
String cname="["+ (minValue + a_sec + b_sec + 1) + "," + maxValue + "]";
//System.out.println(aname);
String[] section = {aname,bname,cname};
CategoryAxis xAxis = new CategoryAxis();
xAxis.setCategories(FXCollections.<String>observableArrayList(section));
//y轴
NumberAxis yAxis = new NumberAxis("频数", 0, 10, 1);
@SuppressWarnings({ "unchecked", "rawtypes" })
ObservableList<BarChart.Series> barChartData = FXCollections.observableArrayList(
new BarChart.Series("V_Optimal Histogram", FXCollections.observableArrayList(
new BarChart.Data(section[0], a_num),
new BarChart.Data(section[1], b_num),
new BarChart.Data(section[2], c_num)
))
);
@SuppressWarnings({ "rawtypes", "unchecked" })
BarChart chart = new BarChart(xAxis, yAxis, barChartData,0);
chart.setTitle("V最优化直方图");
root.getChildren().add(chart);
}
@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
//将数据从小到大排序
DataSet = bubleSort(DataSet);
print_array(bubleSort(DataSet));
// 求取数据区间段
minValue = min(DataSet);
maxValue = max(DataSet);
int section = maxValue - minValue;
// 划分每个桶的区间范围
Vector<Integer> aList = new Vector<Integer>();
Vector<Integer> bList = new Vector<Integer>();
Vector<Integer> cList = new Vector<Integer>();
Vector<Double> VarianceValueList = new Vector<Double>();
if (BucketNum > section + 1)
System.out.println("桶数目过多!");
else if (BucketNum < 1)
System.out.println("桶数目必须为正整数!");
else {
for (int a = 0; a <= section - 2; a++) {
for (int b = 1; b <= section - 1 - a; b++) {
Vector<Integer> First = new Vector<Integer>();
Vector<Integer> Second = new Vector<Integer>();
Vector<Integer> Third = new Vector<Integer>();
// 获取每次循环的3个区间集
for (int i = 0; i < DataSet.length; i++) {
if (DataSet[i] >= minValue
&& DataSet[i] <= minValue + a)
First.add(DataSet[i]);
else if (DataSet[i] > minValue + a
&& DataSet[i] <= minValue + a + b)
Second.add(DataSet[i]);
else
Third.add(DataSet[i]);
}
// 转换成3个定长数组
int FirstSection[] = TransferVectorToInt(First);
int SecondSection[] = TransferVectorToInt(Second);
int ThirdSection[] = TransferVectorToInt(Third);
// 求取此时分组的总带权方差
double valueVariance = Cal_Variance(FirstSection)
+ Cal_Variance(SecondSection)
+ Cal_Variance(ThirdSection);
// 保存此时的总带权方差和各分组a,b,c
VarianceValueList.add(valueVariance);
aList.add(a);
bList.add(b);
cList.add(section - a - b);
}
}
}
System.out.println("循环次数为:" + VarianceValueList.size());
// 寻找总带权方差的最小值,和那个分组的a,b,c
int loc = MinLocation(VarianceValueList);
a_sec = aList.get(loc);
b_sec = bList.get(loc);
c_sec = cList.get(loc);
//求出每个分组的频数
for(int ii=0;ii<DataSet.length;ii++){
if(DataSet[ii]>=minValue&&DataSet[ii]<=minValue+a_sec)
a_num++;
else if(DataSet[ii]>minValue+a_sec&&DataSet[ii]<=minValue + a_sec + b_sec)
b_num++;
else
c_num++;
}
// 结果输出:
print_array(TransferVectorToDouble(VarianceValueList));
System.out.println("最小带权方差为:" + VarianceValueList.get(loc));
System.out.println("最佳方案为第" + loc + "次循环!");
System.out.println("a_sec=" + a_sec + " b_sec=" + b_sec + " c_sec="
+ c_sec);
System.out.println("V优化直方图法最佳方案:" + "[" + minValue + ","
+ (minValue + a_sec) + "]" + " [" + (minValue + a_sec + 1)
+ "," + (minValue + a_sec + b_sec) + "]" + " ["
+ (minValue + a_sec + b_sec + 1) + "," + maxValue + "]");
// 进入绘图
launch(args);
}
/**
* 将一组数值中的最小值的位置找到*/
private static int MinLocation(Vector<Double> list){
int loc=0;
//double min=list.get(loc);
for(int i=1;i<list.size();i++){
if(list.get(i)<list.get(loc))
loc=i;
}
return loc;
}
/**
* 将Vector数组转换成常规定长数组*/
private static int[] TransferVectorToInt(Vector<Integer> list){
int[] array=new int[list.size()];
for(int i=0;i<list.size();i++){
array[i]=(int) list.elementAt(i);
}
return array;
}
private static double[] TransferVectorToDouble(Vector<Double> list){
double[] array=new double[list.size()];
for(int i=0;i<list.size();i++){
array[i]=(double) list.elementAt(i);
}
return array;
}
/**
* 计算一组数据的加权方差*/
@SuppressWarnings("unused")
private static double Cal_Variance(double array[]){
double sum=0;
for(int i=0;i<array.length;i++){
sum+=array[i];
}
double avg=sum/array.length;
double nss=0;//ss表示方差,nss表示带权方差
for(int j=0;j<array.length;j++){
nss+=(avg-array[j])*(avg-array[j]);
}
return nss;
}
private static double Cal_Variance(int array[]){
double sum=0;
for(int i=0;i<array.length;i++){
sum+=array[i];
}
double avg=sum/array.length;
double nss=0;//ss表示方差,nss表示带权方差
for(int j=0;j<array.length;j++){
nss+=(avg-array[j])*(avg-array[j]);
}
return nss;
}
/**
* 冒泡排序,从小到大*/
@SuppressWarnings("unused")
private static double[] bubleSort(double array[]){
int n=array.length;
double temp;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
return array;
}
private static int[] bubleSort(int array[]){
int n=array.length;
int temp;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
return array;
}
/**求取最小值*/
private static int min(int[] data){
int size=data.length;
int minValue=data[0];
for(int i=1;i<size;i++){
if(minValue>data[i])
minValue=data[i];
}
return minValue;
}
/**求取最大值*/
private static int max(int[] data){
int size=data.length;
int maxValue=data[0];
for(int i=1;i<size;i++){
if(maxValue<data[i])
maxValue=data[i];
}
return maxValue;
}
/**输出数组*/
private static void print_array(double arr[]){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
private static void print_array(int arr[]){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}