コード例 #1
0
def getTrainData():
    """获取训练数据"""
    dataFilePath = "../data/samples-data.data"
    labelsFilePath = "../data/samples-data-labels.data"
    X = io.getData(dataFilePath)
    Y = io.getData(labelsFilePath)

    return X, Y
コード例 #2
0
def excute():
    """针对训练好的模型进行寻优"""
    logger.info("{}-sarsaOptimization-excute-{}".format('*' * 25, '*' * 25))
    # Env基本参数定义
    dim = 5  # 决策变量维数
    actionDim = 3  # 动作策略维度
    lowBoundary = [-300, -300, -300, -300, -200]  # 变量下界值
    upBoundary = [300, 300, 100, 300, 200]  # 变量上界值

    # 获得代理模型
    modelName = getModelName()[1]
    modelPath = modelPathFormat.format(modelName)  # quadraticRegression
    agent = io.getData(modelPath)

    MAX_STEP = 10000
    checkLens = 1000  # 检测最优最大步数

    initPos = [[300, 300, 300, 300, 200]]  # 最原始的模型尺寸

    # # 结果最优的代理模型路径
    # QSavingPath = "../data/Q.model"

    # 执行动作
    Q = io.getData(QSavingPath)
    for pos in initPos:  # 选择不同的起点进行
        logger.info("using initial position: {}".format(pos))
        curPos = pos[:]  # 深度复制
        e = EnvExcutor(agent,
                       dim,
                       lowBoundary,
                       upBoundary,
                       initPost=curPos,
                       checkLens=checkLens)
        action = QExcutor.epsilonGreedyExcutor(Q, e.presentState)
        while (e.isEnd is False) and (e.step < MAX_STEP):
            e.interact(action)  # 计算当前动作的奖赏
            state = e.presentState  # 获得当前状态
            newAction = QExcutor.epsilonGreedyExcutor(
                Q, state)  # 根据累积奖赏获得当前状态的下一个动作
            action = newAction

        var, value = e.getOptimalValue()

        # 按日期为目录保存绘制的结果曲线
        sarsaResultsSavingDir = "sarsaModelExcuteResults/{}".format(TODAY)
        tool.mkdirs(sarsaResultsSavingDir)
        e.saveValueSnapShot(
            '{}/optimalValueRecord.jpg'.format(sarsaResultsSavingDir))
        e.saveVarSnapShot('{}/varRecord.jpg'.format(sarsaResultsSavingDir))

        # 打印结果
        logger.info(
            "end step: {}, best result appears in step: {}, variabl: {}, optimal value: {}"
            .format(e.step, e.optimalStep, var, value))
コード例 #3
0
def testModelMAE(X, y):
    """测试模型对已有训练数据的拟合能力"""
    logger.info("{}-testModelMAE-{}".format('*'*25, '*'*25))
    curveFit = io.getData(curveFitModelSavingPath)
    pdt = curveFit.predict(X)
    rmae = tool.computeMAE(y, pdt)
    logger.info("quadratic regression, predict mae : {}".format(rmae))
コード例 #4
0
ファイル: GATest.py プロジェクト: Janson404/Lookoop
def testMultiVar():
    modelName = "ElasticNet"
    modelPath = "../data/{}.model".format(modelName)
    func = io.getData(modelPath)
    featureSize = 18
    populationSize = 500
    upperLimit = [100 for _ in range(featureSize)]
    lowerLimit = [-100 for _ in range(featureSize)]
    chromosomeLength = 10
    maxIter = 1000
    pc = 0.6
    pm = 0.01
    threshedValue = 10
    isMax = False
    ga = GA.GA(func,
               featureSize,
               populationSize,
               upperLimit,
               lowerLimit,
               chromosomeLength,
               maxIter=maxIter,
               pc=pc,
               pm=pm,
               isMax=isMax,
               threshedValue=threshedValue)
    ga.fit()
コード例 #5
0
def testModelPdtRMAE():
    """使用所有数据进行训练,然后对训练数据进行预测"""
    logger.info("{}-testModelPdtRMAE-{}".format('*' * 25, '*' * 25))
    stackModel = io.getData(stackModelSavingPath)
    X, Y = getTrainData()
    pdtValue = stackModel.predict(X)
    retMSE = tool.computeRMAE(Y, pdtValue)
    logger.info("stacking model using all data, RMAE : {}".format(retMSE))
コード例 #6
0
def testSingleModelRMAE():
    """使用所有数据进行训练,然后对训练数据进行预测"""
    logger.info("{}-testSingleModelRMAE-{}".format('*' * 25, '*' * 25))
    X, Y = getTrainData()
    names, models = model.getModel()

    for n in names:
        m = io.getData(singleModelSaving.format(n))
        pdtValue = m.predict(X)
        retMSE = tool.computeRMAE(Y, pdtValue)
        logger.info("model: {}, using all data, RMAE : {}".format(n, retMSE))
コード例 #7
0
def getTrainData():
    """获取训练数据"""
    X = io.getData(dataFilePath)
    Y = io.getData(labelsFilePath)

    return X, Y
コード例 #8
0
ファイル: GAMain.py プロジェクト: Janson404/Lookoop
    def loadModel(self):
        """加载模型"""
        modelPath = modelPathFormat.format(self.modelName)
        model = io.getData(modelPath)

        return model
コード例 #9
0
def testRes():
    X = np.array([[299.99, 299.99, 99.99, 299.99, -199.99]])
    curveFit = io.getData(curveFitModelSavingPath)
    pdt = curveFit.predict(X)
    print(pdt)
コード例 #10
0
def train():
    """单目标,多约束寻优问题"""
    logger.info("{}-sarsaOptimization-train-{}".format('*'*25, '*'*25))
    global globalOptimalValue, MAX_STEP, EPSILON

    # Env基本参数定义
    dim = 5         # 决策变量维数
    actionDim = 3   # 动作策略维度
    lowBoundary = [-300, -300, -300, -300, -200]    # 变量下界值
    upBoundary = [300, 300, 100, 300, 200]          # 变量上界值

    # 获得代理模型
    modelName = getModelName()[1]
    modelPath = modelPathFormat.format(modelName)   # quadraticRegression
    agent = io.getData(modelPath)   

    MAX_STEP = 5000
    maxIter = 1000        # 最大迭代数
    checkLens = 5000     # 检测最优最大步数
    lmb = 1     # 奖赏值学习率
    elta = 2
    gamma = 0.8 # 检查最优值的最大步数学习率
    preOptimalValueDistance = 0 # 上一次迭代中前后两次最优解之间的步数间隔

    # EPSILON衰减率
    preEPSILON = EPSILON    # 保存EPSILON值
    eDescend = 0.001        # EPSILON衰减率,1000步后EPSILON衰减到0.0135

    # 初始化起点位置
    splitPointCount = 2
    initPos = gridMeshPoints(lowBoundary, upBoundary, splitPointCount) 
    initPos.append([300, 300, 300, 300, 200]) # 最原始的模型尺寸

    # 打印当前运行参数信息
    # logger.info("The global optimalValue is not enabled, without EPSILON change")
    logger.info("The global optimalValue is enabled")
    logger.info("using model: {}".format(modelName))
    logger.info("dim: {}, actionDim: {}, lowBoundary: {}, upBoundary: {},\n\
                maxIter: {}, init checkLens: {}, lmb: {}, splitPointCount: {}, pointsSize:{},\n\
                EPSILON: {}, MAX_STEP: {}"
                .format(dim, actionDim, lowBoundary, upBoundary, 
                    maxIter, checkLens, lmb, splitPointCount, len(initPos),
                    EPSILON, MAX_STEP))

    totalIter = len(initPos) * maxIter  # 总迭代数
    count = 1   # 记录迭代数

    Q = QFunction(dim, actionDim, lowBoundary, upBoundary)  # 初始化Q函数
    logger.info("use a new QFunction")

    # # 选择上次最佳模型进行初始化
    # preOptimalModelPath = "../data/sarsaModel/p-0-iter-830-sarsa-2020-01-06.model"
    # Q = io.getData(preOptimalModelPath)
    # logger.info("initial QFunction is backup from: {}".format(preOptimalModelPath))

    # 开始训练
    globalBestValue = []
    for index, pos in enumerate(initPos): # 选择不同的起点进行
        logger.info("start training, using initial position: {}".format(pos))
        resultForEachStep = []  # 记录每一步的结果
        bestValue = []  # 记录最优结果
        checkLensStore = [checkLens]    # 记录收敛步长数
        preCheckLens = checkLens
        EPSILON = preEPSILON
        for it in range(maxIter):
            curPos = pos[:] # 深度复制
            e = Env(agent, dim, lowBoundary, upBoundary, initPost=curPos, checkLens=checkLens, lmb=lmb)
            action = QExcutor.epsilonGreedy(Q, e.presentState)
            while (e.isEnd is False) and (e.step < MAX_STEP):
                # logger.info("action: {}".format(action))
                state = e.presentState
                reward = e.interact(action) # 计算当前动作的奖赏
                newState = e.presentState   # 获得当前状态
                newAction = QExcutor.epsilonGreedy(Q, newState)  # 根据累积奖赏获得当前状态的下一个动作
                Q.updateStateAndAction(state, action, newState, newAction, reward)  # 更新状态和动作
                action = newAction[:]

            # # 更新收敛检查步数
            # # checkLens *= elta
            # checkLens = abs(int(preCheckLens + elta * checkLens * (e.optimalStep / e.step - gamma)))
            # checkLensStore.append(checkLens)
            preOptimalValueGapDistance = e.getOptimalValueGapDistance()

            var, value = e.getOptimalValue()

            if globalOptimalValue and value < globalOptimalValue:
                globalOptimalValue = value

            # 记录每个迭代的最佳值
            if var and value:
                resultForEachStep.append([it, var, value])
            if not bestValue or value < bestValue[2]:
                bestValue = [it, var, value, pos, index]
                curOptimalModelPath = os.path.join(os.path.dirname(QSavingPath), "p-{}-iter-{}-{}".format(index, it, os.path.basename(QSavingPath)))
                io.saveData(Q, curOptimalModelPath)

            # 按日期为目录保存绘制的结果曲线
            e.saveValueSnapShot('{}/optimalValueRecord-P-{}-iter-{}.jpg'.format(sarsaResultsSavingDir, index, it))
            e.saveVarSnapShot('{}/varRecord-P-{}-iter-{}.jpg'.format(sarsaResultsSavingDir, index, it))
            e.saveRewardSnapShot('{}/rewardRecord-P-{}-iter-{}.jpg'.format(sarsaResultsSavingDir, index, it))

            # 预估剩余运行时间
            curTime = datetime.datetime.now() 
            curRunTime = curTime - startTime
            meanIterRunTime = curRunTime / count 
            estimateTime = (totalIter - count) * meanIterRunTime

            count += 1  # 迭代计数加1

            # 更新EPSILON值
            # EPSILON = (1 - eDescend) * EPSILON

            # 打印当前变动参数,当前最佳值,预估仍需要运行时间的时间
            logger.info("iter: {}, end step: {}, best value appears in step: {},\n\
                        optimal var: {}, optimal value: {},\n\
                        next checkLens: {}, preOptimalValueGapDistance: {},\n\
                        cur best value appears in iter: {},\n\
                        cur best optimal var:{}, cur best optimal value: {},\n\
                        cur EPSILON: {},\n\
                        still need to run: {}"
                        .format(it, e.step, e.optimalStep, 
                                var, value, 
                                checkLens, preOptimalValueGapDistance, 
                                bestValue[0], 
                                bestValue[1], bestValue[2], 
                                EPSILON,
                                str(estimateTime)))
            
        # 保存收敛步数曲线图
        # saveProfileOfCheckLens(checkLensStore, '{}/checkLens-{}.jpg'.format(sarsaResultsSavingDir, index))
        # 保存每个迭代结果曲线图以及当前最优解曲线图
        saveProfileOfResults(resultForEachStep, '{}/result-{}.jpg'.format(sarsaResultsSavingDir, index))

        # 打印使用当前初始起点的最优解
        logger.info("p: {}, based on cur initial position, best value appears in iter: {}, var: {},  bestValue : {}"\
                    .format(index, bestValue[0], bestValue[1], bestValue[2]))

        if not globalBestValue or globalBestValue[2] > bestValue[2]:
            globalBestValue = bestValue

    io.saveData(Q, QSavingPath) # 保存最终模型文件
    logger.info("global best value: {}".format(globalBestValue))
    endTime = datetime.datetime.now() 
    logger.info("run time: {}".format(str(endTime - startTime)))
コード例 #11
0
def test():
    dataSavePath = "../../data/samples-data.data"
    X = io.getData(dataSavePath)
    print(X)