当前位置: 首页 > news >正文

多阶段报童问题动态规划求解,Python 实现

使用 python 编写了多阶段报童模型的动态规划算法。

  • 使用了 python 的装饰器 @dataclass ,方便定义类
  • 尝试使用并行计算,没有成功,极易出错。动态规划中使用并行计算,还是挺有挑战的;而且并行计算不一定总是比非并行运算速度快。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 28 00:00:35 2024@author: zhenchen@Python version: 3.10@disp:  stochastic dynamic programming to compute multi-period newsvendor problems;use @dataclass for ease of defining classes;parallel computing unsucessful, highly prone to make mistakes;
"""import scipy.stats as sp
from dataclasses import dataclass
from functools import lru_cache
import time@dataclass(frozen=True) 
class State:"""state in  a period: initial inventory """t: intiniInventory: float@dataclass
class Pmf:"""probability mass function for the demand distribution in each period"""truncQuantile: floatdistribution_type: str def get_pmf(self, distribution_parameters):"""Parameters----------distribution_parameters: list, may be multi dimensionalDESCRIPTION. parameter values of the distributionReturns-------pmf : 3-D listDESCRIPTION. probability mass function for the demand in each period"""if (self.distribution_type == 'poisson'):  mean_demands = distribution_parametersmax_demands = [sp.poisson.ppf(self.truncQuantile, d).astype(int) for d in mean_demands]T = len(mean_demands)pmf = [[[k, sp.poisson.pmf(k, mean_demands[t])/self.truncQuantile] for k in range(max_demands[t])] for t in range(T)]return pmf@dataclass(eq = False) 
class StochasticInventory:"""multi period stochastic inventory model class"""    T: int          capacity: float # maximum ordering quantityfixOrderCost: floatvariOrderCost: floatholdCost: floatpenaCost: floattruncationQ: floatmax_inventory: floatmin_inventory: floatpmf: [[[]]]cache_actions = {}def get_feasible_action(self, state:State):"""feasible actions for a certain state"""      return range(self.capacity + 1)def state_tran(self, state:State, action, demand):"""state transition function"""       nextInventory = state.iniInventory + action - demandnextInventory = self.max_inventory if self.max_inventory < nextInventory else nextInventorynextInventory = self.min_inventory if self.min_inventory > nextInventory else nextInventoryreturn State(state.t + 1, nextInventory)def imme_value(self, state:State, action, demand):"""immediate value function"""fixCost = self.fixOrderCost if action > 0 else 0variCost = self.variOrderCost * actionnextInventory = state.iniInventory + action - demandnextInventory = self.max_inventory if nextInventory > self.max_inventory else nextInventorynextInventory = self.min_inventory if nextInventory < self.min_inventory else nextInventoryholdingCost = self.holdCost * max(0, nextInventory)penaltyCost = self.penaCost * max(0, -nextInventory)return fixCost + variCost + holdingCost + penaltyCost# recursion@ lru_cache(maxsize = None)def f(self, state:State):"""recursive function"""bestQValue = float('inf')bestQ = 0for action in self.get_feasible_action(state):thisQValue = 0for randDandP in self.pmf[state.t - 1]:thisQValue += randDandP[1] * self.imme_value(state, action, randDandP[0])if state.t < T:thisQValue += randDandP[1] * self.f(self.state_tran(state, action, randDandP[0]))if thisQValue < bestQValue:bestQValue = thisQValuebestQ = actionself.cache_actions[str(state)] = bestQreturn bestQValuedemands = [10, 20, 10, 20]
distribution_type = 'poisson'
capacity = 100 # maximum ordering quantity
fixOrderCost = 0
variOderCost = 1
holdCost = 2
penaCost = 10
truncQuantile = 0.9999 # trancated quantile for the demand distribution
maxI = 500 # maximum possible inventory
minI = -300 # minimum possible inventorypmf = Pmf(truncQuantile, distribution_type).get_pmf(demands)
T = len(demands)if __name__ == '__main__': start = time.process_time()model = StochasticInventory(T,capacity, fixOrderCost, variOderCost,holdCost, penaCost, truncQuantile,maxI, minI,pmf)ini_state = State(1, 0)expect_total_cost = model.f(ini_state)print('****************************************')print('final expected total cost is %.2f' % expect_total_cost)optQ = model.cache_actions[str(State(1, 0))]print('optimal Q_1 is %.2f' % optQ)end = time.process_time()cpu_time = end - startprint('cpu time is %.4f s' % cpu_time)
http://www.lryc.cn/news/493345.html

相关文章:

  • 【C++进阶篇】像传承家族宝藏一样理解C++继承
  • Java基础面试题09:Java异常处理完成以后,Exception对象会发生什么变化?
  • mysql sql语句 between and 是否边界值
  • Java接收LocalDateTime、LocalDatee参数
  • 方差分析、相关分析、回归分析
  • SQLModel入门
  • 单片机蓝牙手机 APP
  • PostgreSQL在Linux环境下的常用命令总结
  • Unity shaderlab 实现LineSDF
  • Ubuntu中的apt update 和 apt upgrade
  • Android 中 Swipe、Scroll 和 Fling 的区别
  • linux基础2
  • 如何通过智能生成PPT,让演示文稿更高效、更精彩?
  • 执法记录仪数据自动备份光盘刻录归档系统
  • 启动SpringBoot
  • 重定向操作和不同脚本的互相调用
  • 51单片机教程(九)- 数码管的动态显示
  • golang支持线程安全和自动过期map
  • 机器学习之RLHF(人类反馈强化学习)
  • 泷羽sec---shell作业
  • 华为海思2025届校招笔试面试经验分享
  • 摆脱复杂配置!使用MusicGPT部署你的私人AI音乐生成环境
  • 嵌入式Linux中的GPIO编程
  • js:函数
  • 低代码平台审批流程设计
  • OpenCV相机标定与3D重建(8)相机标定函数calibrateCamera()的使用
  • Linux信号量的编程
  • “Yaker,你可以全局配置插件环境变量!“
  • SAAS美容美发系统架构解析
  • 如何为 ext2/ext3/ext4 文件系统的 /dev/centos/root 增加 800G 空间