Mais conteúdo relacionado
Semelhante a 【更新版】20190628玉川大学脳トレ社会科学oTree演習編 (20)
【更新版】20190628玉川大学脳トレ社会科学oTree演習編
- 26. ③models.pyの定義:
class Player(BasePlayer):
name = models.StringField(verbose_name='あなたの名前を教えてください.')
age = models.IntegerField(verbose_name='あなたの年齢を教えてください.')
sex = models.CharField(initial=None,
choices=['男性', '女性'],
verbose_name='あなたの性別を教えてください.',
widget=widgets.Select())
!26
❖simple_surveyフォルダ内のmodels.pyを開く.
❖“class Player(BasePlayer):”に以下を書き足してください.
❖models.pyではデータベースに登録するデータを指定します.
❖Pythonでは大文字と小文字を区別します.気をつけましょう.
- 29. ③models.pyの定義:models.pyの全体
from otree.api import (
models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer,
Currency as c, currency_range
)
author = 'みなさんの名前を入れましょう.'
doc = """
簡単な調査アプリです.
"""
class Constants(BaseConstants):
name_in_url = 'simple_survey'
players_per_group = None
num_rounds = 1
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
pass
class Player(BasePlayer):
name = models.StringField(verbose_name='あなたの名前を教えてください.')
age = models.IntegerField(verbose_name='あなたの年齢を教えてください.')
sex = models.CharField(initial=None,
choices=['男性', '女性'],
verbose_name='あなたの性別を教えてください.',
widget=widgets.Select())
!29
ただし,適宜改行を入れてあげると読みやすいです.
- 33. ④templateの定義:Page2の作成
{% extends "global/Page.html" %}
{% load otree %}
{% block title %}
あなたの情報
{% endblock %}
{% block content %}
あなたが入力した内容は以下のとおりです.<br>
<p>
あなたの名前:{{ player.name }}<br>
あなたの年齢:{{ player.age }}<br>
あなたの性別:{{ player.sex }}<br>
</p>
{% next_button %}
{% endblock %}
!33
- 39. ⑤pages.pyの定義:全部合わせるとこんな感じ !39
from otree.api import Currency as c, currency_range
from ._builtin import Page, WaitPage
from .models import Constants
class Page1(Page):
form_model = 'player'
form_fields = ['name', 'age', 'sex']
class Page2(Page):
pass
page_sequence = [
Page1,
Page2
]
- 59. ③models.pyの定義:Groupを定義する.
❖Groupクラスの中で計算プロセスを定義する.
– Group内で定義されるfieldは同一グループの全員に影響する.
– 全員の貢献額を合計する
– 人数で割って,1人1人へ戻される金額を計算する.
!59
class Group(BaseGroup):
total_contribution = models.CurrencyField()
individual_share = models.CurrencyField()
def compute(self):
contributions = [p.contribution for p in self.get_players()]
self.total_contribution = sum(contributions)
self.individual_share = self.total_contribution * Constants.multiplier /
Constants.players_per_group
for p in self.get_players():
p.payoff = Constants.endowment - p.contribution + self.individual_share
defは関数を定義する.
ここではcomputeという関数を定義している.
- 60. ③models.pyの定義:models.pyの全体
from otree.api import (
models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer,
Currency as c, currency_range
)
author = 'あなたの名前を入力してください.'
doc = """
公共財ゲームです.
"""
class Constants(BaseConstants):
name_in_url = 'public_goods_trial'
players_per_group = 3
num_rounds = 1
endowment = c(100)
multiplier = 2
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
total_contribution = models.CurrencyField()
individual_share = models.CurrencyField()
def compute(self):
contributions = [p.contribution for p in self.get_players()]
self.total_contribution = sum(contributions)
self.individual_share = self.total_contribution * Constants.multiplier / Constants.players_per_group
for p in self.get_players():
p.payoff = Constants.endowment - p.contribution + self.individual_share
class Player(BasePlayer):
contribution = models.CurrencyField(
choices=currency_range(c(0), c(Constants.endowment), c(1)),
label="あなたはいくら貢献しますか?",
widget=widgets.Slider())
!60
ただし,適宜改行を入れてあげると読みやすいです.
- 62. ④templateの定義:Page1の作成
{% block title %}
説明&貢献額の決定
{% endblock %}
{% block content %}
<p>
これは{{ Constants.players_per_group }} 人プレイヤーによる公共財ゲームです.<br>
初期保有額は{{ Constants.endowment }}であり,係数は{{ Constants.multiplier }}で
す.<br>
{{ Constants.players_per_group }} 人の貢献額の合計を{{ Constants.multiplier }}
倍したポイントを{{ Constants.players_per_group }}で割ったものが一人ひとりに返されま
す.
</p>
{% formfields %}
{% next_button %}
{% endblock %}
!62
- 63. ④templateの定義:Page1の作成
{% extends "global/Page.html" %}
{% load otree %}
{% block title %}
説明&貢献額の決定
{% endblock %}
{% block content %}
<p>
これは{{ Constants.players_per_group }} 人プレイヤーによる公共財ゲームです.<br>
初期保有額は{{ Constants.endowment }}であり,係数は{{ Constants.multiplier }}です.<br>
{{ Constants.players_per_group }} 人の貢献額の合計を{{ Constants.multiplier }}倍したポイントを{{
Constants.players_per_group }}で割ったものが一人ひとりに返されます.
</p>
{% formfields %}
{% next_button %}
{% endblock %}
!63
- 65. ④templateの定義:Page3の作成
{% block title %}
結果の確認
{% endblock %}
{% block content %}
<p>
あなたは初期保有額として<strong> {{ Constants.endowment }}</strong>を持っていました.<br>
その中から<strong>{{ player.contribution }}</strong>を貢献しました.<br>
あなたのグループでは全員で<strong>{{ group.total_contribution }}</strong>を貢献していました.<br>
その結果,一人あたりには<strong>{{ group.individual_share }}</strong>が渡されました.<br>
</p>
<p>
<strong>{{ Constants.endowment }}-{{ player.contribution }}+{{ group.individual_share }}
={{ player.payoff }}</strong>
</p>
<p>
したがって,あなたは<strong>{{ player.payoff }}</strong>を受け取りました.
</p>
{% next_button %}
{% endblock %}
!65
- 66. ④templateの定義:Page3の作成
{% extends "global/Page.html" %}
{% load otree %}
{% block title %}
結果の確認
{% endblock %}
{% block content %}
<p>
あなたは初期保有額として<strong> {{ Constants.endowment }}</strong>を持っていました.<br>
その中から<strong>{{ player.contribution }}</strong>を貢献しました.<br>
あなたのグループでは全員で<strong>{{ group.total_contribution }}</strong>を貢献していました.<br>
その結果,一人あたりには<strong>{{ group.individual_share }}</strong>が渡されました.<br>
</p>
<p>
<strong>{{ Constants.endowment }}-{{ player.contribution }}+{{ group.individual_share }}={{ player.payoff }}</strong>
</p>
<p>
したがって,あなたは<strong>{{ player.payoff }}</strong>を受け取りました.
</p>
{% next_button %}
{% endblock %}
!66
- 72. ⑤pages.pyの定義:全部合わせるとこんな感じ !72
from otree.api import Currency as c, currency_range
from ._builtin import Page, WaitPage
from .models import Constants
class Page1(Page):
form_model = 'player'
form_fields = ['contribution']
class Page2(WaitPage):
def after_all_players_arrive(self):
self.group.compute()
class Page3(Page):
pass
page_sequence = [
Page1,
Page2,
Page3
]
- 92. 自分のパソコンで作業するには:
❖Python 3のインストール
– https://www.python.org/
– もしくはanacondaでインストールしても良い.
– 3.7シリーズで良いが,場合によっては3.6が良いことも
• 一部のoTree拡張パッケージを入れると,3.7シリーズだと言うことを聞かないこ
とがある.
❖PostgreSQL(必須ではない)
– https://www.postgresql.jp/
– oTreeではSQLiteが用意されているが,データをしっかり扱うためにはその他のSQLを
使うことが推奨されている.
• 上書きのタイミングなどの関係,大規模実験ならばPostgreSQLなどを使うべき.
❖エディタ
– Pycharmがオススメな感じだが,Atomでも十分使いやすい.
– ココらへんはお好きなものを.
!92