Skip to content

Commit

Permalink
Add method to ConfigurationManipulator to output parameter tree as a …
Browse files Browse the repository at this point in the history
…string
  • Loading branch information
Sierangho committed Feb 27, 2015
1 parent 07daac0 commit 185665b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/tsp/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, args):
data = args.data
m = open(data).readlines()
self.distance = [[int(i) for i in l.split()] for l in m]

def run(self, desired_result, input, limit):
cfg = desired_result.configuration.data
p = cfg[0] # cheating: should use manipulator function
Expand All @@ -50,7 +50,7 @@ def manipulator(self):
def solution(self):
p = [1,13,2,15,9,5,7,3,12,14,10,8,6,4,11]
return self.eval_path(p)



if __name__ == '__main__':
Expand Down
48 changes: 48 additions & 0 deletions opentuner/search/manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def add_parameter(self, p):
p.set_parent(self)
self.params.append(p)

#TODO sub parameters should be recursed on
# not currently an issue since no doubly-nested sub-parameters
sub_params = p.sub_parameters()
for sp in sub_params:
sp.set_parent(p)
Expand Down Expand Up @@ -175,6 +177,52 @@ def parameters(self, config):
raise TypeError()
return self.params

def serialize_parameters(self):
"""
output the parameters in this manipulator in format:
[ConfigurationManipulator,{pinfo:count,pinfo:count ...}]
where pinfo has a similar form to describe the parameter's sub-parameters:
[param_name,{pinfo:count,pinfo:count ...}]
"""
def serialize_parameter(param, sub_parameters):
"""
recursively output param + subparameters in a json-like format:
[param_name,{subparam:count,subparam:count,...}]
or if no subparams
[paramname,{}]
where subparam are sorted alphabetically
"""
sub_parameter_counts = {}
# build the string
if isinstance(param, str):
param_name = param
else:
param_name = param.__class__.__name__
out = ['[', param_name, ',{']

if len(sub_parameters) > 0:
# count sub params
for sp in sub_parameters:
spout = serialize_parameter(sp, sp.sub_parameters())
sub_parameter_counts[spout] = sub_parameter_counts.get(spout, 0) + 1
# add the count map in sorted order
for sp in sorted(sub_parameter_counts):
out.append(sp)
out.append(':')
out.append(str(sub_parameter_counts[sp]))
out.append(',')
out.pop() # remove trailing comma

out.append('}]')
return ''.join(out)

# filter out subparameters to avoid double counting
params = [p for p in self.params if p.parent is self]
return serialize_parameter(self, params)

def hash_config(self, config):
"""produce unique hash value for the given config"""
m = hashlib.sha256()
Expand Down

0 comments on commit 185665b

Please sign in to comment.