這段代碼算是我寫的認真的代碼,跑出來的結果也比較滿意。用的算法是神經網絡,採用隨機數據模擬現實進行訓練,得出一個均衡結果。web
import numpy as np import pandas as pd import matplotlib.pyplot as plt import random from sympy import * # 夏天時溫度隨時間的擬合曲線 summEnviromentTemperature = [26, 26, 25, 25, 24, 25, 26, 27, 29, 30, 31, 31, 31, 32, 32, 32, 32, 31, 30, 29, 29, 28, 28, 27, ] x = np.linspace(0, 23, 1000) plt.plot(x, 5.569913178608847e-04*pow(x, 4)-0.029478827667234*pow(x, 3)+0.467882701034875*pow(x, 2)-1.920380411322440*x+26.720370370370368,label="夏天溫度隨時間變換") plt.show() # 冬天時溫度隨時間的擬合曲線 enviromentTemperature = [6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 9, 9, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4, ] x = np.linspace(0, 23, 1000) plt.plot(x, -2.597040640518996e-05*pow(x, 4)-0.002883489296533*pow(x, 3)+0.095365654876524*pow(x, 2)-0.482645715797885*x+6.361324786324780,label="冬天溫度隨時間變換") plt.show() # 獲取當前季節的室外溫度 def getSummerCorrentOutsideTemperature(time): # 擬合函數1號 correntOutsideTemp = 5.569913178608847e-04*pow(time%24, 4)-0.029478827667234*pow(time%24, 3)+0.467882701034875*pow(time%24, 2)-1.920380411322440*(time%24)+26.720370370370368 return correntOutsideTemp def getWinterCorrentOutsideTemperature(time): # 擬合函數2號 correntOutsideTemp = -2.597040640518996e-05*pow(time%24, 4)-0.002883489296533*pow(time%24, 3)+0.095365654876524*pow(time%24, 2)-0.482645715797885*(time%24)+6.361324786324780 return correntOutsideTemp # 模擬洗一次澡後加水 # return 加熱時間 單位秒 def afterUploadWater(setedTemp, time, season): # setedTemp : 熱水器內部溫度 # time : 時間mod24 # season : 季節 (1爲夏天,2爲冬天) if season == 1: correntOutsideTemp = getSummerCorrentOutsideTemperature(time) usedWater = (72.001*abs(37-correntOutsideTemp))/abs(setedTemp - correntOutsideTemp) else: correntOutsideTemp = getWinterCorrentOutsideTemperature(time) usedWater = (72.001*abs(42-correntOutsideTemp))/abs(setedTemp - correntOutsideTemp) unusedWater = 60-usedWater if unusedWater < 0: unusedWater = 0 fixTemp = (unusedWater*setedTemp + correntOutsideTemp*usedWater)/72.00001 x = symbols('x') heatNeededTime = integrate(60*4200*(1/(1500-1.08*0.879*(x-correntOutsideTemp))), (x, fixTemp, setedTemp)) return heatNeededTime # 模擬加熱5度 # return 返回時間 單位秒 def keepTemp(setedTemp, time, season): # setedTemp : 熱水器內部溫度 # time : 時間mod24 # season : 季節 (1爲夏天,2爲冬天) if season == 1: correntOutsideTemp = getSummerCorrentOutsideTemperature(time) else: correntOutsideTemp = getWinterCorrentOutsideTemperature(time) x = Symbol('x') heatNeededTime = integrate(60*4200*(1/(1500-1.08*0.879*(x-correntOutsideTemp))), (x, setedTemp-5, setedTemp)) return heatNeededTime # 以7天爲一週期的主函數,進行100次迭代,並模擬在一我的任意時刻洗澡 def main(season): # season : 季節 (1爲夏天,2爲冬天) ansDict = {} ansResult = [] if season == 1: start = 40 end = 75 else: start = 50 end = 75 # 開始100次迭代 for i in range(100): for temperature in range(start, end): if season == 1: # 擬合函數3號 startPow = -5.855191256830656e-05*temperature**3 + 0.010656525851198*temperature**2 -0.575437053383782*temperature + 10.713433165195610 else: # 擬合函數4號 startPow = 2.666666666665736e-06*temperature**3 -4.799999999998103e-04*temperature**2 +0.093533333333321*temperature -1.179999999999723 # 表示天天兩次以上降溫5度,被打斷一次 if (startPow >= 2): startPow -= 1 # 模擬每一個溫度下一週加熱時間 totalTime = 0 randomHourWash = random.choice([i for i in range(0,25)]) randomHourHeat = random.choice([i for i in range(0,25)]) for hour in range(7*24): # 天天模擬一個時間點洗澡,一個時間點加熱 if hour/24 != (hour-1)/24: randomHourWash = random.choice([i for i in range(0,25)]) randomHourHeat = random.choice([i for i in range(0,25)]) if (hour%24 == randomHourWash): totalTime += afterUploadWater(temperature, hour, season) if (hour%24 == randomHourHeat): totalTime += startPow*keepTemp(temperature, hour, season) # 下面爲了防止數字溢出,作一個等比例減少處理 if temperature in ansDict: ansDict[temperature] += totalTime/100 else: ansDict[temperature] = totalTime/100 totalTime = 0 finalAnwser = 10000000 bestTemp = 0 for item in ansDict: if ansDict[item] < finalAnwser: finalAnwser = ansDict[item] bestTemp = item ansResult.append(bestTemp) print("Iteration "+str(i+1)+" : "+ str(bestTemp)) # 找出100次迭代中出現最多的溫度 tempDict = {} for flag in ansResult: if flag not in tempDict: tempDict[flag] = 1 else: tempDict[flag] += 1 showMaxNum = 0 finalNum = 0 for item in tempDict: if tempDict[item] > showMaxNum: showMaxNum = tempDict[item] finalNum = item print("final anwser is " + str(finalNum)) main(2) main(1)