|
@@ -3,22 +3,26 @@ from sklearn.model_selection import train_test_split
|
|
|
from sklearn.ensemble import RandomForestRegressor
|
|
|
from sklearn.metrics import mean_squared_error
|
|
|
from scipy.optimize import minimize
|
|
|
+from flask import Flask, request, jsonify
|
|
|
+
|
|
|
+app = Flask(__name__)
|
|
|
|
|
|
|
|
|
# 解冻时间计算函数
|
|
|
-def calculate_defrost_time(w, T_initial):
|
|
|
+def calculate_defrost_time(w, data):
|
|
|
# 物理参数1
|
|
|
- rho_coal = 1400 # 煤的密度
|
|
|
- rho_ice = 917 # 冰的密度
|
|
|
- C_coal = 1000 # 煤的比热容
|
|
|
- C_ice = 2100
|
|
|
- L = 334000 # 物体长度(米)
|
|
|
- k_coal = 0.3 # 煤的导热系数
|
|
|
- k_ice = 2.2
|
|
|
- h = 10 # 近似导热系数(W/m²·K)
|
|
|
- T_air = 20 # 空气温度(摄氏度)
|
|
|
- T_m = 0 # 熔点温度(摄氏度)
|
|
|
- a, b, c = 13, 2.72, 1.6 # 物体的尺寸(长、宽、高)
|
|
|
+ rho_ice = data['rho_ice'] # 冰的密度
|
|
|
+ C_ice = data['C_ice'] # 冰的比热容
|
|
|
+ L = data['L'] # 冰的潜热
|
|
|
+ k_ice = data['k_coal'] # 冰的导热系数
|
|
|
+ T_m = data['T_m'] # 熔点温度(摄氏度)
|
|
|
+ rho_coal = data['rho_coal'] # 物料的密度
|
|
|
+ C_coal = data['C_coal'] # 物料的比热容
|
|
|
+ k_coal = data['k_coal'] # 物料的导热系数
|
|
|
+ h = data['h'] # 对流换热系数
|
|
|
+ T_air = data['T_air'] # 空气温度(摄氏度)
|
|
|
+ T_initial = data['T_initial'] # 物料初始温度
|
|
|
+ a, b, c = data['a'], data['b'], data['c'] # 车厢的尺寸(长、宽、高)
|
|
|
# 计算体积和表面积
|
|
|
V = a * b * c
|
|
|
A = 2 * (a * b) + 2 * (b * c) + 2 * (a * c)
|
|
@@ -45,68 +49,43 @@ def calculate_defrost_time(w, T_initial):
|
|
|
return t_total
|
|
|
|
|
|
|
|
|
-# 模拟生成数据
|
|
|
-np.random.seed(42)
|
|
|
-n_samples = 500
|
|
|
-# 含水量,范围 10% - 15%
|
|
|
-w = np.random.uniform(0.1, 0.15, n_samples)
|
|
|
-# 初始温度,范围 -10 到 20 摄氏度
|
|
|
-T_initial = np.random.uniform(-10, 20, n_samples)
|
|
|
-# 计算解冻时间
|
|
|
-original_times = []
|
|
|
-for i in range(n_samples):
|
|
|
- original_times.append(calculate_defrost_time(w[i], T_initial[i]))
|
|
|
-
|
|
|
-# 模拟实际作业时长(实际时长和计算时长有一定随机误差)
|
|
|
-y = np.array(original_times) + np.random.normal(0, 0.2, n_samples)
|
|
|
-
|
|
|
-# 准备特征
|
|
|
-X = np.column_stack((w, T_initial, original_times))
|
|
|
-
|
|
|
-# 划分训练集和测试集
|
|
|
-X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
|
-
|
|
|
-# 选择模型并训练
|
|
|
-model = RandomForestRegressor(n_estimators=100, random_state=42)
|
|
|
-model.fit(X_train, y_train)
|
|
|
-
|
|
|
-# 模型评估
|
|
|
-y_pred = model.predict(X_test)
|
|
|
-mse = mean_squared_error(y_test, y_pred)
|
|
|
-print(f"Mean Squared Error: {mse}")
|
|
|
-
|
|
|
-
|
|
|
# 反推函数
|
|
|
-def reverse_calculate(time):
|
|
|
+def reverse_calculate(time, data):
|
|
|
def objective_function(vars):
|
|
|
- w, T_initial = vars
|
|
|
- calculated_time = calculate_defrost_time(w, T_initial)
|
|
|
+ w = vars
|
|
|
+ calculated_time = calculate_defrost_time(w, data)
|
|
|
return abs(calculated_time - time)
|
|
|
|
|
|
- bounds = [(0.1, 0.15), (-10, 20)] # 含水量和初始温度的范围
|
|
|
- num_trials = 5 # 尝试的次数
|
|
|
+ bounds = [(0.1, 0.5)] # 含水量和初始温度的范围
|
|
|
+ num_trials = 10 # 尝试的次数
|
|
|
best_result = None
|
|
|
best_error = np.inf
|
|
|
for _ in range(num_trials):
|
|
|
# 随机生成初始猜测值
|
|
|
- initial_guess = [np.random.uniform(0.1, 0.15), np.random.uniform(-10, 20)]
|
|
|
+ initial_guess = [np.random.uniform(0.1, 0.15)]
|
|
|
result = minimize(objective_function, initial_guess, bounds=bounds)
|
|
|
error = result.fun
|
|
|
if error < best_error:
|
|
|
best_error = error
|
|
|
best_result = result
|
|
|
- return best_result.x[0], best_result.x[1]
|
|
|
+ return best_result.x[0]
|
|
|
+
|
|
|
|
|
|
+# API 入口
|
|
|
+@app.route('/reverse_calculate', methods=['POST'])
|
|
|
+def handle_request():
|
|
|
+ try:
|
|
|
+ data = request.get_json()
|
|
|
+ time = float(data['realThawTime'])
|
|
|
+ w_result = reverse_calculate(time, data)
|
|
|
+ print(f"实际用时{time}")
|
|
|
+ print(f"含水量{round(w_result, 4) * 100}")
|
|
|
+ return jsonify({
|
|
|
+ "water_content": round(w_result, 4) * 100
|
|
|
+ })
|
|
|
+ except Exception as e:
|
|
|
+ return jsonify({"error": str(e)}), 400
|
|
|
|
|
|
-# 输入实际解冻时间
|
|
|
-try:
|
|
|
- test_time = float(input("请输入实际解冻时间(小时): "))
|
|
|
- w_result, T_result = reverse_calculate(test_time)
|
|
|
- print(f"反推得到的含水量: {w_result}")
|
|
|
- print(f"反推得到的初始温度: {T_result}")
|
|
|
- recalculated_time = calculate_defrost_time(w_result, T_result)
|
|
|
- print(f"重新计算的解冻时间: {recalculated_time}")
|
|
|
-except ValueError:
|
|
|
- print("输入无效,请输入有效的数字。")
|
|
|
|
|
|
-
|
|
|
+if __name__ == '__main__':
|
|
|
+ app.run(host='127.0.0.1', port=9993, debug=True)
|