Skip to content

Commit 317e800

Browse files
committed
Added indexes to SQLite3 Database
* There are several queries that use ORDER BY statements. In order to let them work properly, indexes must be created
1 parent 85e2482 commit 317e800

6 files changed

Lines changed: 65 additions & 41 deletions

File tree

MLC/GUI/Experiment/ExperimentWindow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ def on_draw_as_tree_button_clicked(self):
471471
experiment_name=self._experiment_name,
472472
gen_data=gen_data,
473473
current_gen=self._current_gen)
474+
tree_window.setAttribute(Qt.WA_DeleteOnClose)
474475
tree_window.show()
475476

476477
def on_board_config_button_clicked(self):

MLC/GUI/mlc_gui.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import os
2323
import sys
24-
# reload(sys)
25-
# sys.setdefaultencoding("utf-8")
2624

2725
mlc_gui_dir = os.path.dirname(os.path.abspath(__file__))
2826
root_dir = os.path.join(*[mlc_gui_dir, "..", ".."])

MLC/api/Experiment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ def reload_configuration(self):
7979
"""
8080
Load again the experiment configuration, reading the config file from disk
8181
"""
82-
self._configuration = ConfigParser.ConfigParser()
82+
self._configuration = configparser.ConfigParser()
8383
self._configuration.read(self._config_file)
8484
Config.get_instance().read(self._config_file)
8585
return Config.to_dictionary(self._configuration)
8686

8787
def set_configuration(self, new_configuration):
88-
self._configuration = Config.from_dictionary(new_configuration, config_type=ConfigParser.ConfigParser)
88+
self._configuration = Config.from_dictionary(new_configuration, config_type=configparser.ConfigParser)
8989
self._configuration.write(open(self._config_file, "wt"))
9090

9191
# Reload the configuration

MLC/db/sqlite/sql_statements.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@
1919
# You should have received a copy of the GNU General Public License
2020
# along with this program. If not, see <http://www.gnu.org/licenses/>
2121

22+
2223
def stmt_create_table_individuals():
23-
return ''' CREATE TABLE individual(indiv_id INTEGER PRIMARY KEY,
24-
value text,
25-
formal text,
26-
complexity INTEGER)'''
24+
return '''
25+
CREATE TABLE individual(indiv_id INTEGER PRIMARY KEY,
26+
value text,
27+
formal text,
28+
complexity INTEGER)'''
29+
30+
31+
def stmt_create_table_index_individuals():
32+
return '''
33+
CREATE INDEX index_indiv_id ON individual(indiv_id)'''
2734

2835

2936
def stmt_create_table_population():
@@ -38,6 +45,16 @@ def stmt_create_table_population():
3845
FOREIGN KEY(indiv_id) REFERENCES individual(indiv_id))'''
3946

4047

48+
def stmt_create_id_index_on_population():
49+
return '''
50+
CREATE INDEX index_pop_id ON population(id)'''
51+
52+
53+
def stmt_create_indiv_id_index_on_population():
54+
return '''
55+
CREATE INDEX index_pop_indiv_id ON population(indiv_id)'''
56+
57+
4158
def stmt_delete_generation(generation):
4259
return """DELETE FROM population
4360
WHERE gen = %s""" % (generation,)
@@ -47,10 +64,12 @@ def stmt_delete_from_generations(from_generation):
4764
return """DELETE FROM population
4865
WHERE gen >= %s""" % (from_generation,)
4966

67+
5068
def stmt_delete_to_generations(to_generation):
5169
return """DELETE FROM population
5270
WHERE gen <= %s""" % (to_generation,)
5371

72+
5473
def stmt_delete_unused_individuals():
5574
return '''DELETE FROM individual
5675
WHERE indiv_id NOT IN (SELECT DISTINCT indiv_id FROM population)'''
@@ -72,19 +91,19 @@ def stmt_get_generations():
7291

7392
def stmt_insert_individual_in_population(generation, indiv_id, cost, evaluation_time, gen_method, parents):
7493
return '''INSERT INTO population (gen, cost, evaluation_time, gen_method, parents, indiv_id)
75-
VALUES (%s, "%s", %s, %s, "%s", %s)''' % (generation,
76-
cost,
77-
evaluation_time,
78-
gen_method,
79-
parents,
80-
indiv_id)
94+
VALUES ( % s, "%s", % s, % s, "%s", % s)''' % (generation,
95+
cost,
96+
evaluation_time,
97+
gen_method,
98+
parents,
99+
indiv_id)
81100

82101

83102
def stmt_get_individuals_from_population(generation):
84-
return '''SELECT indiv_id, cost, evaluation_time, gen_method, parents, ID
103+
return '''SELECT indiv_id, cost, evaluation_time, gen_method, parents, id
85104
FROM population
86105
WHERE gen = %s
87-
ORDER BY ID''' % generation
106+
ORDER BY id''' % generation
88107

89108

90109
class SQLSaveFormal:
@@ -102,10 +121,10 @@ def from_sql(indiv_formal_column):
102121

103122

104123
def stmt_insert_individual(individual_id, individual):
105-
return '''INSERT INTO individual VALUES (%s, "%s", "%s", %s)''' % (individual_id,
106-
individual.get_value(),
107-
SQLSaveFormal.to_sql(individual.get_formal()),
108-
individual.get_complexity())
124+
return '''INSERT INTO individual VALUES ( % s, "%s", "%s", % s)''' % (individual_id,
125+
individual.get_value(),
126+
SQLSaveFormal.to_sql(individual.get_formal()),
127+
individual.get_complexity())
109128

110129

111130
def stmt_get_all_individuals():
@@ -140,16 +159,19 @@ def stmt_update_cost(individual_id, cost, evaluation_time, generation):
140159
individual_id,
141160
generation)
142161
"""
143-
The individual with the least cost in the last population
162+
The individual with the least cost in the last population
144163
is considered to be the best individual
145164
"""
165+
166+
146167
def stmt_get_individual_with_min_cost_in_last_pop():
147-
return '''SELECT indiv_id,
148-
cost
168+
return '''SELECT indiv_id,
169+
cost
149170
FROM population
150171
ORDER BY gen DESC,
151172
cost ASC
152173
LIMIT 1'''
153174

175+
154176
def stmt_enable_foreign_key():
155177
return '''PRAGMA foreign_keys = ON'''

MLC/db/sqlite/sqlite_repository.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ def __initialize_db(self):
7575

7676
# MLC Population tables
7777
cursor.execute(stmt_create_table_individuals())
78+
cursor.execute(stmt_create_table_index_individuals())
7879
cursor.execute(stmt_create_table_population())
80+
cursor.execute(stmt_create_id_index_on_population())
81+
cursor.execute(stmt_create_indiv_id_index_on_population())
7982

8083
# Board configuration tables
8184
cursor.execute(stmt_create_table_board())
@@ -331,6 +334,7 @@ def __load_individuals(self):
331334
individuals = {}
332335
conn = self.__get_db_connection()
333336
cursor = conn.execute(stmt_get_all_individuals())
337+
# data = cursor.fetchall()
334338

335339
for row in cursor:
336340
new_individual = Individual(str(row[1]), SQLSaveFormal.from_sql(row[2]), row[3])

MLC/individual/Individual.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ class MutationType:
9898
_maxdepthfirst = None
9999

100100
def __init__(self, value, formal=None, complexity=None):
101-
self._config = Config.get_instance()
102-
103101
# Tree expression initialized using lazing initialization through
104102
# _tree property. Use self._tree instead of self._lazy_tree to
105103
# obtain the tree expression.
@@ -114,8 +112,8 @@ def __init__(self, value, formal=None, complexity=None):
114112
self._complexity = self._tree.complexity()
115113
self._value = self._tree.get_expanded_tree_as_string()
116114

117-
self._range = self._config.getint("POPULATION", "range")
118-
self._precision = self._config.getint("POPULATION", "precision")
115+
def get_config():
116+
return Config.get_instance()
119117

120118
@property
121119
def _tree(self):
@@ -188,9 +186,9 @@ def __crossover_tree(self, other_individual):
188186
189187
:return: (first new tree, second new tree) as strings
190188
"""
191-
maxtries = self._config.getint("GP", "maxtries")
192-
mutmindepth = self._config.getint("GP", "mutmindepth")
193-
maxdepth = self._config.getint("GP", "maxdepth")
189+
maxtries = Config.get_instance().getint("GP", "maxtries")
190+
mutmindepth = Config.get_instance().getint("GP", "mutmindepth")
191+
maxdepth = Config.get_instance().getint("GP", "maxdepth")
194192

195193
correct = False
196194
count = 0
@@ -218,11 +216,11 @@ def __crossover_tree(self, other_individual):
218216
return value_1, value_2
219217

220218
def __mutate_tree(self, mutation_type):
221-
mutmindepth = self._config.getint("GP", "mutmindepth")
222-
maxdepth = self._config.getint("GP", "maxdepth")
223-
sensor_spec = self._config.getboolean("POPULATION", "sensor_spec")
224-
sensors = self._config.getint("POPULATION", 'sensors')
225-
mutation_types = self._config.get_list("GP", 'mutation_types')
219+
mutmindepth = Config.get_instance().getint("GP", "mutmindepth")
220+
maxdepth = Config.get_instance().getint("GP", "maxdepth")
221+
sensor_spec = Config.get_instance().getboolean("POPULATION", "sensor_spec")
222+
sensors = Config.get_instance().getint("POPULATION", 'sensors')
223+
mutation_types = Config.get_instance().get_list("GP", 'mutation_types')
226224

227225
# equi probability for each mutation type selected.
228226
if mutation_type == Individual.MutationType.ANY:
@@ -241,11 +239,12 @@ def __mutate_tree(self, mutation_type):
241239
else:
242240
next_individual_type = 4
243241

244-
new_individual_value = Individual.__generate_indiv_regressive_tree(subtree, self._config, next_individual_type)
242+
new_individual_value = Individual.__generate_indiv_regressive_tree(
243+
subtree, Config.get_instance(), next_individual_type)
245244

246245
if new_individual_value:
247246
if sensor_spec:
248-
config_sensor_list = sorted(self._config.get_list('POPULATION', 'sensor_list'))
247+
config_sensor_list = sorted(Config.get_instance().get_list('POPULATION', 'sensor_list'))
249248
else:
250249
config_sensor_list = range(sensors - 1, -1, -1)
251250

@@ -275,11 +274,11 @@ def __mutate_tree(self, mutation_type):
275274
elif mutation_type == Individual.MutationType.HOIST:
276275
preevok = False
277276
counter = 0
278-
maxtries = self._config.getint("GP", "maxtries")
277+
maxtries = Config.get_instance().getint("GP", "maxtries")
279278

280279
while not preevok and counter < maxtries:
281280
counter += 1
282-
controls = self._config.getint("POPULATION", "controls")
281+
controls = Config.get_instance().getint("POPULATION", "controls")
283282
prob_threshold = 1 / float(controls)
284283

285284
cl = [stree.to_string() for stree in self.get_tree().get_root_node()._nodes]
@@ -336,8 +335,8 @@ def __extract_subtree(self, expression_tree, mindepth, subtreedepthmax, maxdepth
336335

337336
def __reparam_tree(self, tree_expression):
338337
def leaf_value_generator():
339-
leaf_value = (RandomManager.rand() - 0.5) * 2 * self._range
340-
return "%0.*f" % (self._precision, leaf_value)
338+
leaf_value = (RandomManager.rand() - 0.5) * 2 * Config.get_instance().getint("POPULATION", "range")
339+
return "%0.*f" % (Config.get_instance().getint("POPULATION", "precision"), leaf_value)
341340

342341
return self.__change_const_tree(tree_expression, leaf_value_generator)
343342

0 commit comments

Comments
 (0)