根据Python神秘主义的条件最大化和

时间:2018-01-04 04:05:43

标签: python numpy scipy mystic

我正在尝试在Dynamic room pricing model for hotel revenue management systems上构建一份白皮书的实现。如果此链接将来死亡,我会在相关部分粘贴: enter image description here

到目前为止,我目前的实际情况已经大大破坏了,因为我真的不完全理解如何解决非线性最大化方程。

# magical lookup table that returns demand based on those inputs
# this will eventually be a db lookup against past years rental activity and not hardcoded to a specific value
def demand(dateFirstNight, duration):
    return 1

# magical function that fetches the price we have allocated for a room on this date to existing customers
# this should be a db lookup against previous stays, and not hardcoded to a specific value
def getPrice(date):
    return 75

# Typical room base price
# Defined as: Nominal price of the hotel (usually the average historical price)
nominalPrice = 89

# from the white paper, but perhaps needs to be adjusted in the future using the methods they explain
priceElasticity = 2

# this is an adjustable constant it depends how far forward we want to look into the future when optimizing the prices
# likely this will effect how long this will take to run, so it will be a balancing game with regards to accuracy vs runtime
numberOfDays = 30

def roomsAlocated(dateFirstNight, duration)
    roomPriceSum = 0.0
    for date in range(dateFirstNight, dateFirstNight+duration-1):
        roomPriceSum += getPrice(date)

    return demand(dateFirstNight, duration) * (roomPriceSum/(nominalPrice*duration))**priceElasticity


def roomsReserved(date):
    # find all stays that contain this date, this 


def maximizeRevenue(dateFirstNight):
    # we are inverting the price sum which is to be maximized because mystic only does minimization
    # and when you minimize the inverse you are maximizing!
    return (sum([getPrice(date)*roomsReserved(date) for date in range(dateFirstNight, dateFirstNight+numberOfDays)]))**-1


def constraint(x): # Ol - totalNumberOfRoomsInHotel <= 0
    return roomsReserved(date) - totalNumberOfRoomsInHotel

from mystic.penalty import quadratic_inequality
@quadratic_inequality(constraint, k=1e4)
def penalty(x):
  return 0.0

from mystic.solvers import diffev2
from mystic.monitors import VerboseMonitor
mon = VerboseMonitor(10)

bounds = [0,1e4]*numberOfDays
result = diffev2(maximizeRevenue, x0=bounds, penalty=penalty, npop=10, gtol=200, disp=False, full_output=True, itermon=mon, maxiter=M*N*100)

任何熟悉mystic工作的人都可以就如何实现这一点给我一些建议吗?

2 个答案:

答案 0 :(得分:3)

当您要求使用库mystic时,在开始使用非线性优化时,您可能不需要这种细粒度控制。模块scipy应该足够了。接下来是一个或多或少的完整解决方案,纠正我认为在原始白皮书中关于定价界限的错字:

import numpy as np
from scipy.optimize import minimize

P_nom = 89
P_max = None
price_elasticity = 2
number_of_days = 7
demand = lambda a, L: 1./L
total_rooms = [5]*number_of_days

def objective(P, *args):
    return -np.dot(P, O(P, *args))

def worst_leftover(P, C, *args):
    return min(np.subtract(C, O(P, *args)))

def X(P, a, L, d, e, P_nom):
    return d(a, L)*(sum(P[a:a+L])/P_nom/L)**e

def d(a, L):
    return 1.

def O_l(P, l, l_max, *args):
    return sum([X(P, a, L, *args)
                for a in xrange(0, l)
                for L in xrange(l-a+1, l_max+1)])

def O(P, *args):
    return [O_l(P, l, *args) for l in xrange(len(P))]

result = minimize(
    objective,
    [P_nom]*number_of_days,
    args=(number_of_days-1, demand, price_elasticity, P_nom),
    method='SLSQP',
    bounds=[(0, P_max)]*number_of_days,
    constraints={
        'type': 'ineq',
        'fun': worst_leftover,
        'args': (total_rooms, number_of_days-1, demand, price_elasticity, P_nom)
    },
    tol=1e-1,
    options={'maxiter': 10**3}
)

print result.x

值得一提的几点:

  1. 目标函数添加了减号,用于scipy的minimize()例程,与原始白皮书中引用的最大化形成对比。这会导致result.fun为负值,而非指示总收入。

  2. 公式似乎对参数有点敏感。最小化是正确的(至少,当它说它正确执行时它是正确的 - 检查result.success),但如果输入离现实太远,那么你可能会发现价格远高于预期。您可能还想使用比以下示例中更多的天数。似乎有一些类似于白皮书引起的周期性影响。

  3. 我不是白皮书命名方案的粉丝,因为它与可读代码有关。我改变了一些东西,但是有些东西确实是残暴的,应该被替换,比如小写的l,很容易混淆1号。

  4. 我确实设定了界限,使价格为正而非负。根据您的专业知识,您应该验证这是正确的决定。

  5. 您可能更喜欢比我指定的更严格的公差。这在某种程度上取决于你想要的运行时间。随意使用tol参数。此外,如果容差更严格,您可能会发现'maxiter'参数中的options必须增加minimize()才能正确收敛。

  6. 我很确定total_rooms应该是酒店尚未预订的房间数量,因为白皮书上的字母是l而不是像你在原始代码。我将其设置为用于测试目的的常量列表。

  7. 该方法必须是'SLSQP'来处理价格的界限和房间数量的界限。注意不要改变这一点。

  8. 计算O_l()的方式效率低下。如果运行时成为问题,我将采取的第一步是确定如何缓存/记忆对X()的调用。所有这些只是第一次传递,概念验证。它应该是合理的无错误和正确的,但它几乎直接从白皮书中提取,并且可以做一些重新分解。

  9. Anywho,玩得开心,随时可以评论/ PM /等任何其他问题。

答案 1 :(得分:3)

抱歉,我迟到了回答这个问题,但我认为接受的答案并不是解决完整的问题,而是进一步解决问题。请注意,在局部最小化中,求解接近名义价格并不能提供最佳解决方案。

让我们首先构建一个hotel类:

"""
This file is 'hotel.py'
"""
import math

class hotel(object):
    def __init__(self, rooms, price_ave, price_elastic):
        self.rooms = rooms
        self.price_ave = price_ave
        self.price_elastic = price_elastic

    def profit(self, P):
        # assert len(P) == len(self.rooms)
        return sum(j * self._reserved(P, i) for i,j in enumerate(P))

    def remaining(self, P): # >= 0
        C = self.rooms
        # assert len(P) == C
        return [C[i] - self._reserved(P, i) for i,j in enumerate(P)]

    def _reserved(self, P, day):
        max_days = len(self.rooms)
        As = range(0, day)
        return sum(self._allocated(P, a, L) for a in As
                   for L in range(day-a+1, max_days+1))

    def _allocated(self, P, a, L):
        P_nom = self.price_ave
        e = self.price_elastic
        return math.ceil(self._demand(a, L)*(sum(P[a:a+L])/(P_nom*L))**e)

    def _demand(self, a,L): #XXX: fictional non-constant demand function
        return abs(1-a)/L + 2*(a**2)/L**2

以下是使用mystic解决问题的一种方法:

"""
This file is 'local.py'
"""
n_days = 7
n_rooms = 50
P_nom = 85
P_bounds = 0,None
P_elastic = 2

import hotel
h = hotel.hotel([n_rooms]*n_days, P_nom, P_elastic)

def objective(price, hotel):
    return -hotel.profit(price)

def constraint(price, hotel): # <= 0
    return -min(hotel.remaining(price))

bounds = [P_bounds]*n_days
guess = [P_nom]*n_days

import mystic as my

@my.penalty.quadratic_inequality(constraint, kwds=dict(hotel=h))
def penalty(x):
    return 0.0

# using a local optimizer, starting from the nominal price
solver = my.solvers.fmin
mon = my.monitors.VerboseMonitor(100)

kwds = dict(disp=True, full_output=True, itermon=mon,
            args=(h,),  xtol=1e-8, ftol=1e-8, maxfun=10000, maxiter=2000)
result = solver(objective, guess, bounds=bounds, penalty=penalty, **kwds)

print([round(i,2) for i in result[0]])

结果:

>$ python local.py 
Generation 0 has Chi-Squared: -4930.000000
Generation 100 has Chi-Squared: -22353.444547
Generation 200 has Chi-Squared: -22410.402420
Generation 300 has Chi-Squared: -22411.780268
Generation 400 has Chi-Squared: -22413.908944
Generation 500 has Chi-Squared: -22477.869093
Generation 600 has Chi-Squared: -22480.144244
Generation 700 has Chi-Squared: -22480.280379
Generation 800 has Chi-Squared: -22485.563188
Generation 900 has Chi-Squared: -22485.564265
Generation 1000 has Chi-Squared: -22489.341602
Generation 1100 has Chi-Squared: -22489.345912
Generation 1200 has Chi-Squared: -22489.351219
Generation 1300 has Chi-Squared: -22491.994305
Generation 1400 has Chi-Squared: -22491.994518
Generation 1500 has Chi-Squared: -22492.061127
Generation 1600 has Chi-Squared: -22492.573672
Generation 1700 has Chi-Squared: -22492.573690
Generation 1800 has Chi-Squared: -22492.622064
Generation 1900 has Chi-Squared: -22492.622230
Optimization terminated successfully.
         Current function value: -22492.622230
         Iterations: 1926
         Function evaluations: 3346
STOP("CandidateRelativeTolerance with {'xtol': 1e-08, 'ftol': 1e-08}")
[1.15, 20.42, 20.7, 248.1, 220.56, 41.4, 160.09]

这里再次使用全局优化器:

"""
This file is 'global.py'
"""
n_days = 7
n_rooms = 50
P_nom = 85
P_bounds = 0,None
P_elastic = 2

import hotel
h = hotel.hotel([n_rooms]*n_days, P_nom, P_elastic)

def objective(price, hotel):
    return -hotel.profit(price)

def constraint(price, hotel): # <= 0
    return -min(hotel.remaining(price))

bounds = [P_bounds]*n_days
guess = [P_nom]*n_days

import mystic as my

@my.penalty.quadratic_inequality(constraint, kwds=dict(hotel=h))
def penalty(x):
    return 0.0

# try again using a global optimizer
solver = my.solvers.diffev
mon = my.monitors.VerboseMonitor(100)

kwds = dict(disp=True, full_output=True, itermon=mon, npop=40,
            args=(h,),  gtol=250, ftol=1e-8, maxfun=30000, maxiter=2000)
result = solver(objective, bounds, bounds=bounds, penalty=penalty, **kwds)

print([round(i,2) for i in result[0]])

结果:

>$ python global.py 
Generation 0 has Chi-Squared: 3684702.124765
Generation 100 has Chi-Squared: -36493.709890
Generation 200 has Chi-Squared: -36650.498677
Generation 300 has Chi-Squared: -36651.722841
Generation 400 has Chi-Squared: -36651.733274
Generation 500 has Chi-Squared: -36651.733322
Generation 600 has Chi-Squared: -36651.733361
Generation 700 has Chi-Squared: -36651.733361
Generation 800 has Chi-Squared: -36651.733361
STOP("ChangeOverGeneration with {'tolerance': 1e-08, 'generations': 250}")
Optimization terminated successfully.
         Current function value: -36651.733361
         Iterations: 896
         Function evaluations: 24237
[861.07, 893.88, 398.68, 471.4, 9.44, 0.0, 244.67]

我认为为了产生更合理的定价,我会将P_bounds值更改为更合理的值。