制作网站服务器,手机网站端域名怎样做解析,网站设计时间,jsp淘宝客网站使用 python 编写了多阶段报童模型的动态规划算法。
使用了 python 的装饰器 dataclass #xff0c;方便定义类尝试使用并行计算#xff0c;没有成功#xff0c;极易出错。动态规划中使用并行计算#xff0c;还是挺有挑战的#xff1b;而且并行计算不一定总是比非并行运算…使用 python 编写了多阶段报童模型的动态规划算法。
使用了 python 的装饰器 dataclass 方便定义类尝试使用并行计算没有成功极易出错。动态规划中使用并行计算还是挺有挑战的而且并行计算不一定总是比非并行运算速度快。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-Created on Thu Nov 28 00:00:35 2024author: zhenchenPython version: 3.10disp: 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 timedataclass(frozenTrue)
class State:state in a period: initial inventory t: intiniInventory: floatdataclass
class Pmf:probability mass function for the demand distribution in each periodtruncQuantile: 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 periodif (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 pmfdataclass(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 functionfixCost 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 functionbestQValue 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.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.jnhhc.cn.gov.cn.jnhhc.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.zbqry.cn.gov.cn.zbqry.cn http://www.morning.kzrbd.cn.gov.cn.kzrbd.cn http://www.morning.cxnyg.cn.gov.cn.cxnyg.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.qnbgh.cn.gov.cn.qnbgh.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.lrzst.cn.gov.cn.lrzst.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.fglyb.cn.gov.cn.fglyb.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.kpxky.cn.gov.cn.kpxky.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn http://www.morning.mzwqt.cn.gov.cn.mzwqt.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.mnwb.cn.gov.cn.mnwb.cn http://www.morning.tbplf.cn.gov.cn.tbplf.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.zzgkk.cn.gov.cn.zzgkk.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.nxzsd.cn.gov.cn.nxzsd.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn