簡體   English   中英

我得到“數組必須都是相同的長度”。 有人可以幫我理解問題出在哪里以及如何解決嗎?

[英]I am getting "arrays must all be the same length". Can someone please help me understand where the problem lies and how to fix it?

我正在嘗試運行模擬並獲得利用率。

這是我的代碼。 plot 不幸地給出了錯誤,我不知道哪里出錯了。 有人可以解釋這意味着什么以及如何解決它嗎?

import gurobipy as gp
import numpy as np
import math
import seaborn as sn
import statistics as stat

roomPrice = 280
variablePrice = list(range(200, 350, 10))
dayAvailable = [1, 2, 3, 4, 5]
dayDemand = [[0], [1], [2], [3], [4], [0,1], [1,2], [2,3], [3,4], [0,1,2]]
customers = np.random.poisson(lam=250, size=1000)
customersDenied = []
utilization = []

def priceAccept(p): 
    value = 1/(1+math.exp(0.025*p-7.5))
    return value

for p in variablePrice: 
    bookings = 0 # to be able to calculate further, the bookings in this question cannot be a list and must be int
    for c in customers: # First we need to get customers booked or denied
        capacity = [50, 50, 50, 50, 50] # Allocate the max capacity per day
        customersHelped = 0 # Start the counting for customers that receive a booking
        deny = 0 # Start the counting for customer that are denied a booking
        probability = np.random.choice([0,1,2,3,4,5,6,7,8,9,], 
                                   size = c, 
                                   p = [0.15,0.05,0.1,
                                        0.05,0.1,0.2,
                                        0.1,0.1,0.05,0.1]) # The probability statistics for each (combination of) room
        for prob in probability: # We need to check whether max capacity has been reached
            capacityFull = 0 # variable for counting the bookings
            for i in dayDemand[prob]:
                if capacity[i] == 0:       # The next 3 lines indicate that capacity full and deny need to 
                    capacityFull += 1      # increase when capacity reaches 0. This is important for 
                    deny += 1              # the allocation of customers and the question later on.
            if capacityFull == 0: # If the room are empty
                priceAcceptCustomer = np.random.choice([1,0], size = 1, p=[priceAccept(roomPrice),1-priceAccept(roomPrice)])
                if priceAcceptCustomer == 1:
                    for d in dayDemand[prob]:
                        if capacity[d-1] > 0:
                            capacity[d-1] -= 1
                            customersHelped += 1
            bookings += customersHelped # bookings isnt appended but added to
        customersDenied.append(deny)
        utilization.append(bookings/1000/250*100)

plot = sn.lineplot(x = variablePrice,y = utilization)
plot.set(xlabel='Price per night', ylabel='Utilization [%]')

我沒有嘗試運行代碼,但這不是這一行的縮進錯誤嗎?

        utilization.append(bookings/1000/250*100)

我知道這應該只p in variablePrice發生一次,但您將它for c in customers

以下代碼適用於我:

import gurobipy as gp
import numpy as np
import math
import seaborn as sn
import statistics as stat
import tqdm

roomPrice = 280
variablePrice = list(range(200, 350, 10))
dayAvailable = [1, 2, 3, 4, 5]
dayDemand = [[0], [1], [2], [3], [4], [0,1], [1,2], [2,3], [3,4], [0,1,2]]
customers = np.random.poisson(lam=250, size=200)
customersDenied = []
utilization = []

def priceAccept(p): 
    value = 1/(1+math.exp(0.025*p-7.5))
    return value

p_pbar = tqdm.tqdm(variablePrice)
for p in p_pbar: 
    p_pbar.set_description(f"Processing p={p}")
    
    bookings = 0 # to be able to calculate further, the bookings in this question cannot be a list and must be int
    c_pbar = tqdm.tqdm(customers, leave=False)
    for c in c_pbar: # First we need to get customers booked or denied
        c_pbar.set_description(f"Processing c={c}")
        capacity = [50, 50, 50, 50, 50] # Allocate the max capacity per day
        customersHelped = 0 # Start the counting for customers that receive a booking
        deny = 0 # Start the counting for customer that are denied a booking
        probability = np.random.choice([0,1,2,3,4,5,6,7,8,9,], 
                                   size = c, 
                                   p = [0.15,0.05,0.1,
                                        0.05,0.1,0.2,
                                        0.1,0.1,0.05,0.1]) # The probability statistics for each (combination of) room
        for prob in probability: # We need to check whether max capacity has been reached
            capacityFull = 0 # variable for counting the bookings
            for i in dayDemand[prob]:
                if capacity[i] == 0:       # The next 3 lines indicate that capacity full and deny need to 
                    capacityFull += 1      # increase when capacity reaches 0. This is important for 
                    deny += 1              # the allocation of customers and the question later on.
            if capacityFull == 0: # If the room are empty
                priceAcceptCustomer = np.random.choice([1,0], size = 1, p=[priceAccept(roomPrice),1-priceAccept(roomPrice)])
                if priceAcceptCustomer == 1:
                    for d in dayDemand[prob]:
                        if capacity[d-1] > 0:
                            capacity[d-1] -= 1
                            customersHelped += 1
            bookings += customersHelped # bookings isnt appended but added to
        customersDenied.append(deny)
    utilization.append(bookings/1000/250*100)

plot = sn.lineplot(x = variablePrice,y = utilization)
plot.set(xlabel='Price per night', ylabel='Utilization [%]')

如您所見,我使用tqdm添加了一些進度條。 我推薦這個庫,它非常易於使用,並為您的腳本和工具帶來了不錯的優勢!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM