私は web2py を初めて使用し、アプリケーションを試しています。controller
関連する機能とそれに対応する関連する については、以下を参照してくださいviews
。まず、ブラウザで をポイントしてhttp://127.0.0.1:8000/test/default/index
から、アプリケーションに従います。私には奇妙に思えることがあります:価格が最初に入力された後http://127.0.0.1:8000/test/default/inandout
、ページに表示される MC と maxWTP の値が変わることがあります (常にではありませんが、5 ~ 6 回に 1 回程度です) 。私がやろうとしているのは、MC と maxWTP を に行くときにランダムに選択する必要があり、これらを使用して marketobject を作成することです。しかしその後ではありません。ページがリダイレクトされたらhttp://127.0.0.1:8000/test/default/index
http://127.0.0.1:8000/test/default/inandout
価格が入力された後に MC と maxWTP が変更される理由がわかりません。また、最初に価格が入力された後にのみ発生するようです。なぜこれが起こっているのか、そしてそれを修正する方法を誰かが私に説明してもらえますか? Chrome ブラウザとバージョン 1.99.7 (2012-03-04 22:12:08) の web2py と python 2.7 を使用しています。
その理由は市場モジュールにあるとは思いません。しかし、市場モジュールとそれが依存する他のモジュールを見たい場合は、私に知らせてください。
コントローラー機能
import random
import market
def index():
'''Creates a new market object and resets the price, quantity
and profit histories'''
session.marketobject = None
mc = random.choice([1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2])
maxWTP = random.choice([4,5,6,7])
session.marketobject = market.Market(mc,maxWTP)
session.priceHistory = []
session.profitHistory = []
session.quantityHistory = []
redirect(URL('inandout'), 303)
def second():
if request.vars.price:
price = float(request.vars.price)
# use the price to calculate profit and qsold
currentprice, qsold, profit = session.marketobject.calculate_qsold_profit(price)
# add the result to the history of prices, profits and qsold
session.priceHistory.append(currentprice)
session.profitHistory.append(profit)
session.quantityHistory.append(qsold)
redirect(URL('inandout'), 303)
def inandout():
capacity = session.marketobject.K
populationsize = session.marketobject.N
mc = session.marketobject.MC
maxWTP = session.marketobject.maxWTP
return dict(capacity=capacity, populationsize=populationsize,
mc=mc, maxWTP=maxWTP,
priceHistory=session.priceHistory,
profitHistory=session.profitHistory,
quantityHistory=session.quantityHistory)
意見:inandout.html
<html>
<head>
<title>First page</title>
<link rel="stylesheet" href="{{=URL('static', 'css/testapp.css')}}">
</head>
<body>
<h2>Seller capacity: {{=capacity}}</h2>
<h2>Population size: {{=populationsize}}</h2>
<h2>MC: {{=mc}}</h2>
<h2>Maximum WTP: {{=maxWTP}}</h2>
<form action="second">
Enter price: <input type="text" name="price">
<input type="submit">
</form>
<table>
{{for count in range(len(priceHistory)):}}
<tr>
<td>{{=priceHistory[count]}}</td>
<td>{{=quantityHistory[count]}}</td>
<td>{{=profitHistory[count]}}</td>
</tr>
{{pass}}
</table>
</body>
</html>
市場、消費者、およびビジネス モジュール
#!/usr/bin/env python
# coding: utf8
import consumers
import business
class Market(object):
def __init__(self, MC, maxWTP, N=30000, minWTP=0,
FC=0, K=10000, numDecisions=3):
self.numDecisions = numDecisions
self.K = K
self.N = N
self.MC = MC
self.maxWTP = maxWTP
self.consumerClass = consumers.AllConsumers(N, minWTP, maxWTP)
self.seller = business.Seller(FC, MC, K)
def calculate_qsold_profit(self, price):
currentprice = price
qtydemanded = self.consumerClass.qtyDemanded(currentprice)
calculate_qsold_profit = \
self.seller.calculate_qsold_profit(currentprice,
qtydemanded)
qSold = calculate_qsold_profit[0]
profit = calculate_qsold_profit[1]
return (currentprice, qSold,profit,)
消費者.py
#!/usr/bin/env python
# coding: utf8
import random
##=====================================================================
# Create the class that creates all consumers
##=====================================================================
class AllConsumers(object):
def __init__(self, N, minWTP, maxWTP):
self.N = N
self.minWTP = minWTP
self.maxWTP = maxWTP
self.listOfWTPs = sorted([random.uniform(minWTP,maxWTP) \
for x in range(N)], reverse=True)
def qtyDemanded(self, price):
count = 0
for wtp in self.listOfWTPs:
if wtp >= price:
count += 1
else:
break
return count
ビジネス.py
#!/usr/bin/env python
# coding: utf8
##=====================================================================
# Create a seller class
##=====================================================================
class Seller(object):
def __init__(self, FC, MC, K):
self.FC = FC
self.MC = MC
self.K = K
def calculate_qsold_profit(self, price, qtydemanded):
# choose minimum of quantity demanded and capacity
# create a vector of monthly capacity (same element repeated 12 times)
qsold = min(qtydemanded,self.K)
# Calculate the profit vector
profit = ((price - self.MC) * qsold) - self.FC
return (qsold, profit)