Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion configs/round_configs/demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ trade_box_scale: 0.2
# this calculation is done after scaling, so x and y are fractional values
# this function needs to be both valid javascript and python code which means available operations are
# pretty much just limited to arithmetic
utility_function: "(x ** 0.5) * (y ** 0.5)"

# if the parameter 'piecewise_utility' is True, the syntax should be a string like this one
#"(piece 1, domain for piece 1);(piece 2, domain for piece 2)...". e.g: “(11 + 12*(x+(11.2*y - 0.5*(y**2))), y<11.2);(11 + 12*(x + 0.5*(11.2**2)), y>=11.2)”
# else, the syntax is a string with the "function". e.g: "(x ** 0.5) * (y ** 0.5)"
piecewise_utility: true
utility_function: “(11 + 12*(x+(11.2*y - 0.5*(y**2))), y<11.2);(11 + 12*(x + 0.5*(11.2**2)), y>=11.2)”
# the maximum utility value which can be reached by a player
# this is used to scale displayed utility values when generating the heatmap
max_utility: 50
Expand Down
22 changes: 21 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,27 @@ def config(self):
return MarketConfig.get(config_name, self.round_number, self.id_in_group)

def utility_function(self, x, y):
return eval(self.config.utility_function, {'x': x, 'y': y})
"""
Evaluates the inputted parameters on a continuous/piecewise function

Input: parameter values for x and y (float)
Output: value of function after evaluating the values (float)
"""

if not self.config.piecewise_utility:
return eval(self.config.utility_function, {'x': x, 'y': y})
else:
pieces = self.config.utility_function.split(";")

for piece in pieces: # iterating through pieces of function
# extracting value under current piece and condition
value_and_cond = eval(piece, {'x': x, 'y': y})
piece_value = value_and_cond[0]
condition_is_met = value_and_cond[1]

if condition_is_met:
return piece_value


def set_payoff(self):
config = self.config
Expand Down