2019杭电数模竞赛A题第三题代码

这段代码算是我写的认真的代码,跑出来的结果也比较满意。用的算法是神经网络,采用随机数据模拟现实进行训练,得出一个均衡结果。

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)

你可能感兴趣的:(python)