package main

import (
	"fmt"
	"math"
	"testing"
)

// 测试函数
func TestBeego(t *testing.T) {
	//制冷机组温控设定最低值_, _ := lib.StringToFloat32("-25")
	//制冷机组温控设定最高值_, _ := lib.StringToFloat32("-25")
	//
	//T := (lib.To_float32(制冷机组温控设定最低值_) + lib.To_float32(制冷机组温控设定最高值_)) / 2
	//fmt.Printf("转换后的 float32 值: %s\n", lib.To2(T))
	// 测试用例1
	//A1, B1 := 3.5, 6.5
	//a1s, b1s := findValidValues(A1, B1)
	//fmt.Printf("当A=%.1f, B=%.1f时:\n", A1, B1)
	//for i := range a1s {
	//	fmt.Printf("A1=%.1f, B1=%.1f\n", a1s[i], b1s[i])
	//}
	//
	//// 测试用例2
	//A2, B2 := 4.0, 6.0
	//a1s, b1s = findValidValues(A2, B2)
	//fmt.Printf("\n当A=%.1f, B=%.1f时:\n", A2, B2)
	//for i := range a1s {
	//	fmt.Printf("A1=%.1f, B1=%.1f\n", a1s[i], b1s[i])
	//}

	//A1, B1 := 3.5, 6.5
	//C1, D1 := 6.5, 3.5
	//a1s1, b1s1 := findOptimalAdjustment(A1, B1, C1, D1)
	//fmt.Printf("当A=%.1f, B=%.1f时:\n", A1, B1)
	//fmt.Printf("最优解 A1=%.1f, B1=%.1f\n", a1s1, b1s1)
	//
	//// 测试用例2
	//A2, B2 := 4.0, 6.0
	//C2, D2 := 3.5, 6.5
	//a1s2, b1s2 := findOptimalAdjustment(A2, B2, C2, D2)
	//fmt.Printf("\n当A=%.1f, B=%.1f时:\n", A2, B2)
	//fmt.Printf("最优解 A1=%.1f, B1=%.1f\n", a1s2, b1s2)

	a, b, _ := findBindingPointsOptimalAdjustment(5.0, 5.0, 4.9, 6.5, 0.5)
	fmt.Printf("最优解 A1=%.1f, B1=%.1f\n", a, b)
}
func TestAverageOptimalAdjustment(t *testing.T) {
	// 输入参数
	A1 := 4.0
	B1 := 5.0
	C := 6.0

	// 计算有效区间
	minA := A1 - 0.5
	maxA := A1 + 0.5
	minB := B1 - 0.5
	maxB := B1 + 0.5

	// 求交集范围
	lowerBound := math.Max(minA, minB)
	upperBound := math.Min(maxA, maxB)

	// 确定最优调整值
	var optimalC float64
	if C < lowerBound {
		optimalC = lowerBound // 取区间下限
	} else if C > upperBound {
		optimalC = upperBound // 取区间上限
	} else {
		optimalC = C // 已在区间内无需调整
	}

	// 输出结果
	fmt.Printf("调整前C值: %.1f\n", C)
	fmt.Printf("有效区间: [%.1f, %.1f]\n", lowerBound, upperBound)
	fmt.Printf("最优调整值: %.1f\n", optimalC)
	fmt.Printf("调整幅度: %.1f\n", math.Abs(C-optimalC))

}

func findOptimalAdjustment(A, B, C, D float64) (float64, float64) {

	type DataPair struct {
		A1 float64
		B1 float64
	}
	var dataPairs []DataPair

	var A1, B1 float64 = -100, -100

	// 生成A1和B1的可能值
	for a1 := A - 1; a1 <= A+1; a1 += 0.5 {
		for b1 := B - 1; b1 <= B+1; b1 += 0.5 {
			// 检查是否存在满足条件的C值
			minC := math.Max(a1-0.5, b1-0.5)
			maxC := math.Min(a1+0.5, b1+0.5)

			if minC <= maxC {
				dataPairs = append(dataPairs, DataPair{A1: a1, B1: b1})
			}
		}
	}

	if len(dataPairs) == 1 {
		A1 = dataPairs[0].A1
		B1 = dataPairs[0].B1
	}
	if len(dataPairs) > 1 {

		// 寻找最优解
		minAdjustment := math.MaxFloat64
		var optimalPair DataPair

		for _, pair := range dataPairs {
			adjustment := math.Abs(pair.A1-C) + math.Abs(pair.B1-D)
			if adjustment < minAdjustment {
				minAdjustment = adjustment
				optimalPair = pair
			}
		}
		A1 = optimalPair.A1
		B1 = optimalPair.B1
	}

	return A1, B1
}

func findBindingPointsOptimalAdjustment(A, B, C, D, T_deviation float64) (float64, float64, bool) {

	type DataPair struct {
		A1 float64
		B1 float64
	}
	T_deviation_half := T_deviation / 2
	var dataPairs []DataPair

	var A1, B1 float64 = -100, -100

	// 生成A1和B1的可能值
	for a1 := A - T_deviation; a1 <= A+T_deviation; a1 += 0.1 {
		for b1 := B - T_deviation; b1 <= B+T_deviation; b1 += 0.1 {
			// 检查是否存在满足条件的C值
			minC := math.Max(a1-T_deviation_half, b1-T_deviation_half)
			maxC := math.Min(a1+T_deviation_half, b1+T_deviation_half)

			if minC <= maxC {
				dataPairs = append(dataPairs, DataPair{A1: a1, B1: b1})
			}
		}
	}

	if len(dataPairs) == 1 {
		A1 = dataPairs[0].A1
		B1 = dataPairs[0].B1
	}
	if len(dataPairs) > 1 {
		// 寻找最优解
		minAdjustment := math.MaxFloat64
		var optimalPair DataPair

		for _, pair := range dataPairs {
			adjustment := math.Abs(pair.A1-C) + math.Abs(pair.B1-D)
			if adjustment < minAdjustment {
				minAdjustment = adjustment
				optimalPair = pair
			}
		}
		A1 = optimalPair.A1
		B1 = optimalPair.B1
	}
	if A1 == -100 || B1 == -100 {
		return A1, B1, false
	}
	return A1, B1, true
}
