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

企业展示型网站 建站系统谷歌搜索引擎免费入口 台湾

企业展示型网站 建站系统,谷歌搜索引擎免费入口 台湾,请叫我鬼差大人王烨,手机注册使用 python 编写了多阶段报童模型的动态规划算法。 使用了 python 的装饰器 dataclass ,方便定义类尝试使用并行计算,没有成功,极易出错。动态规划中使用并行计算,还是挺有挑战的;而且并行计算不一定总是比非并行运算…

使用 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.shuangfujiaoyu.com/news/61180.html

相关文章:

  • 网站开发中网页之间的连接形式南京百度推广优化
  • 服务器色情网站专用北京网站建设运营
  • 做优化网站是什么意思上海最新发布最新
  • 网站关键词多少个字数 站长网注册域名后如何建立网站
  • 漳州建网站深圳知名网络优化公司
  • nodejs 做视频网站重庆seo全面优化
  • 建设银行兰州分行网站百度网站首页
  • 百度站长平台h5网站如何开发软件app
  • 门户网站申请百度百家号注册
  • 大学生实训网站建设心得北京网站优化服务
  • 个人介绍网页设计作品seo推广策划
  • 自己用模板做网站天天seo百度点击器
  • 小程序定义电商seo与sem是什么
  • 做youtube视频网站青岛网站制作公司
  • 怎么做属于自己的网站网络营销优化培训
  • 带做网站价位seo推广有哪些方式
  • 做西点网站苏州网站
  • 青岛seo百科北京百度推广排名优化
  • 网站建设公司业务人员岗位职责实体店营销方案
  • 北京服饰电商网站建设广州顶正餐饮培训学校
  • 备案要关闭网站吗爱网站
  • 点石嘉业北京网站建设公司无锡百度正规推广
  • 网站建设维护更新铜仁搜狗推广
  • 网站建设营销公司信息流广告接单平台
  • 某拍卖公司企业网站源码今日头条热搜榜前十名
  • 网页设计实验报告总结模板石家庄网站优化
  • 成功的营销网站的例子为企业策划一次网络营销活动
  • 什么类型的网站容易被百度抓取站长工具友链查询
  • 泰州建站价格长沙sem培训
  • 内网网站建设的必要性谷歌app官方下载