interactive_cost_tuning.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import os, sys
3 from time import time
4 
5 if sys.version_info >= (3, 0):
6  import tkinter as tk
7 else:
8  import Tkinter as tk
9 
10 # import rospkg as rp
11 
12 __all__ = ['InteractiveCostTuning']
13 
14 class InteractiveCostTuning(object):
15 
16  def __init__(self, problem):
17 
18  # Initial setup
19  self.master = tk.Tk()
20  self.problem = problem
21 
22  # Set title
23  self.master.winfo_toplevel().title("Interactive Cost Tuning")
24 
25  # Set icon
26  # icon = rp.RosStack().get_path('exotica') + '/doc/images/EXOTica_icon.png'
27  # img = tk.PhotoImage(file=icon)
28  # self.master.tk.call('wm', 'iconphoto', self.master._w, img)
29 
30  # Grab current rhos and cost task map names
31  self.rho = {}
32  self.original_rho = {}
34  for k in list(problem.get_task_maps().keys()):
35  try:
36  r = problem.get_rho(k)
37  self.rho[k] = r
38  self.original_rho[k] = r
39  self.cost_task_map_names.append(k)
40  except:
41  continue
42 
43  # Setup labels and entries
44  self.entries = {}
45  for i, k in enumerate(self.cost_task_map_names):
46  tk.Label(self.master, text=k).grid(row=i, column=0)
47  self.entries[k] = tk.Entry(self.master)
48  self.entries[k].grid(row=i, column=1, pady=4)
49  self.entries[k].insert(0, self.rho[k])
50 
51  n_cost_task_maps = len(self.cost_task_map_names)
52  tk.Label(self.master, text='Filename').grid(row=n_cost_task_maps, column=0, pady=4)
53  self.entries['filename'] = tk.Entry(self.master)
54  self.entries['filename'].grid(row=n_cost_task_maps, column=1, pady=4)
55  self.entries['filename'].insert(0, 'FilenameHere')
56 
57  # Setup buttons
58  tk.Button(self.master, text="Set", command=self.set_button).grid(row=n_cost_task_maps+1, column=0, pady=4)
59  tk.Button(self.master, text="Save", command=self.save_button).grid(row=n_cost_task_maps+1, column=1, pady=4)
60  tk.Button(self.master, text="Reset", command=self.reset_button).grid(row=n_cost_task_maps+2, column=0, pady=4)
61  tk.Button(self.master, text="Quit", command=self.quit_button).grid(row=n_cost_task_maps+2, column=1, pady=4)
62 
63  def set_button(self):
64  """Sets rho parameters in entries into Exotica problem."""
65  print("Setting cost parameters:")
66  for k in self.cost_task_map_names:
67  userin = self.entries[k].get() # is a str
68  rho = float(eval(userin))
69  self.entries[k].delete(0, 'end')
70  self.entries[k].insert(0, rho)
71  self.problem.set_rho(k, rho)
72  print(" {}\t{}".format(k, rho))
73 
74  def save_button(self):
75  """Saves current rho parameters in entries to file in home dir."""
76 
77  # Generate filename, filename structure is FILENAMEINENTRY_TIMEINMS.costparams
78  # Use time to avoid overwriting errors
79  t = int(round(time() * 1000)) # time in ms (int)
80  filename = "%s/%s_%d.costparams" % (os.environ['HOME'], self.entries['filename'].get(), t)
81 
82  # Save parameters
83  with open(filename, 'w') as fout:
84  fout.write('<Cost>\n')
85  for k in self.cost_task_map_names:
86  fout.write(' <Task Task="{}" Rho="{}"/>\n'.format(k, float(eval(self.entries[k].get()))))
87  fout.write('</Cost>\n')
88  print("Saved cost parameters to %s" % filename)
89 
90  def reset_button(self):
91  """Resets entries/exotica to original cost terms as specified in xml."""
92  print("Resetting cost parameters:")
93  for k in self.cost_task_map_names:
94  rho = self.original_rho[k]
95 
96  # Reset entries
97  self.entries[k].delete(0, 'end')
98  self.entries[k].insert(0, rho)
99 
100  # Reset exotica problem
101  self.problem.set_rho(k, rho)
102 
103  print(" {}\t{}".format(k, rho))
104 
105  def quit_button(self):
106  """Quits interactive cost tuning."""
107  print("Quitting interactive cost tuning...")
108  self.master.quit()
109 
110  def mainloop(self):
111  """Starts tk mainloop."""
112  tk.mainloop()


exotica_python
Author(s):
autogenerated on Sat Apr 10 2021 02:35:59