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

server/2024/11/29 8:54:27/

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

  • 使用了 python 的装饰器 @dataclass ,方便定义类
  • 尝试使用并行计算,没有成功,极易出错。动态规划中使用并行计算,还是挺有挑战的;而且并行计算不一定总是比非并行运算速度快。
python">#!/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.ppmy.cn/server/145862.html

相关文章

mfc110u.dll是什么意思,mfc110u.dll丢失解决方法大全详解

mfc110u.dll是Microsoft Foundation Classes (MFC)库的一个特定版本&#xff08;版本11.0&#xff09;的Unicode动态链接库文件。MFC是Microsoft为C开发者设计的一个应用程序框架&#xff0c;主要用于简化Windows应用程序的开发工作。这个框架封装了很多Windows API函数&#x…

Laravel8.5+微信小程序实现京东商城秒杀方案

一、商品秒杀涉及的知识点 鉴权策略封装掊口访问频次限制小程序设计页面防抖接口调用订单创建事务使用超卖防御 二、订单库存系统方案&#xff08;3种&#xff09; 下单减库存 优点是库存和订单的强一致性&#xff0c;商品不会卖超&#xff0c;但是可能导致恶意下单&#xff…

【技术支持】vscode不使用插件,两种方式重命名html标签对

1. 使用 VS Code 内置功能 VS Code 内置支持 HTML/XML 标签对的重命名功能。步骤如下&#xff1a; 将光标放置在标签名上&#xff08;如 <div> 或</div>&#xff09;。按下快捷键 F2&#xff08;重命名符号&#xff09;。输入新的标签名&#xff0c;按 Enter&…

动态内存管理的知识点笔记总结

开始之前&#xff0c;我们解释一为什么存在动态内存分配&#xff1f; 在我们之前写的&#xff1a; int arr[10]{0}; 连续开辟40个字节的空间 int a10; 在内存开辟4个字节 但是&#xff0c; 1.这种大小是固定死的&#xff0c;我们是无法改变的。 2. 数组在申明的时候&a…

C++和C中的volatile 关键字

在 C/C 中volatile 关键字的作用 1.防止编译器优化 编译器在编译程序时&#xff0c;为了提高程序的执行效率&#xff0c;会对代码进行优化。例如&#xff0c;当编译器发现一个变量的值在一段代码中没有被显式地改变时&#xff0c;它可能会将这个变量的值缓存到寄存器中&#…

AWS海外注册域名是否需要实名认证?

在全球化的互联网环境中&#xff0c;注册域名已成为企业和个人建立在线存在的重要步骤。亚马逊网络服务&#xff08;AWS&#xff09;作为全球领先的云服务提供商&#xff0c;其域名注册服务也备受关注。然而&#xff0c;对于在AWS上注册海外域名是否需要实名认证&#xff0c;许…

javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version

查了一上午&#xff0c;这个错误的原因好像是 发送端和接收端采用的 TLS 协议版本不一致导致的&#xff1a; 解决步骤 确认双方使用的 TLS 协议版本更改一方的 TLS 使两方相同 1.确认双方使用的 TLS 协议版本 捣鼓了半天&#xff0c;终于发现一个简单靠谱的方法来确认双方的…

深入理解Go语言中的`sync.Pool`与常规内存分配

在Go语言的并发编程中&#xff0c;内存管理是一个不可忽视的话题。sync.Pool作为Go标准库中的一个特殊工具&#xff0c;提供了一种对象池化机制&#xff0c;以优化内存分配和垃圾回收&#xff08;GC&#xff09;。本文将深入探讨sync.Pool与常规内存分配的主要区别&#xff0c;…