Skip to content

Commit 889563d

Browse files
committed
Added feature (with ton of bugs yet) of editing Individuals costs
* There are a lot of bugs to be fixed yet. #41
1 parent 764aa8f commit 889563d

File tree

6 files changed

+85
-20
lines changed

6 files changed

+85
-20
lines changed

MLC/Application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __init__(self, simulation, callbacks={}):
7171

7272
def _set_numpy_parameters(self):
7373
# Set printable resolution (don't alter numpy interval resolution)
74-
np.set_printoptions(precision=3)
74+
np.set_printoptions(precision=9)
7575
# Show full arrays, no matter what size do they have
7676
np.set_printoptions(threshold=np.inf)
7777
# Don't show scientific notation

MLC/GUI/Experiment/ExperimentDialog.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import numpy as np
55
import sys
6+
import time
67
sys.path.append(os.path.abspath(".") + "/../..")
78

89
from MLC.Log.log import get_gui_logger
@@ -104,10 +105,6 @@ def on_prev_gen_button_clicked(self):
104105

105106
if self._current_gen > 1:
106107
self._current_gen -= 1
107-
108-
# Change combo box currentIndex
109-
gen_count_combo = self._autogenerated_object.gen_count_combo
110-
111108
self._update_experiment_info()
112109
self._update_individuals_figure()
113110

@@ -118,10 +115,6 @@ def on_next_gen_button_clicked(self):
118115

119116
if self._current_gen < number_of_gens:
120117
self._current_gen += 1
121-
122-
# Change combo box currentIndex
123-
gen_count_combo = self._autogenerated_object.gen_count_combo
124-
125118
self._update_experiment_info()
126119
self._update_individuals_figure()
127120

@@ -140,7 +133,7 @@ def on_test_button_clicked(self):
140133
# Calculate individual cost
141134
try:
142135

143-
individual = Individual.generate(config=Config.get_instance(),
136+
individual = Individual.generate(config=Config.get_instance(),
144137
rhs_value=test_indiv_edit.text())
145138
callback = EvaluatorFactory.get_callback()
146139
cost = callback.cost(individual)
@@ -265,8 +258,47 @@ def _config_table_edited(self, left, right):
265258
self._autogenerated_object.save_config_button.setDisabled(False)
266259

267260
def _db_view_edited(self, left, right):
268-
# TODO
269-
pass
261+
db_view = self._autogenerated_object.db_view
262+
table_model = db_view.model()
263+
264+
response = QMessageBox.information(self, "Editing Experiment DB",
265+
"Do you really want to change value?",
266+
QMessageBox.No | QMessageBox.Yes,
267+
QMessageBox.No)
268+
269+
indiv_id = int(table_model.get_data(left.row(), 1))
270+
if response == QMessageBox.No:
271+
# Get the value stored in the database
272+
indiv_data = self._mlc_local.get_individual(self._experiment_name, indiv_id)
273+
274+
if left.column() == 4:
275+
# Cost modified
276+
old_value = indiv_data.get_cost_history()[self._current_gen][0][0]
277+
logger.info('[EXPERIMENT {0}] [DB_VIEW_EDITED] - '
278+
'Edition was canceled. Cell({1}, {2}) - Old value: {3}'
279+
.format(self._experiment_name, left.row(),
280+
left.column(), old_value))
281+
table_model.set_data(left.row(), left.column(), str(old_value))
282+
elif left.column() == 5:
283+
# Value modified
284+
# TODO:
285+
pass
286+
else:
287+
value = table_model.get_data(left.row(), left.column())
288+
289+
if left.column() == 4:
290+
logger.info('[EXPERIMENT {0}] [DB_VIEW_EDITED] - '
291+
'Updating database. Cell ({1}, {2}) - Value: {3}'
292+
.format(self._experiment_name, left.row(),
293+
left.column(), value))
294+
self._mlc_local.update_individual_cost(experiment_name=self._experiment_name,
295+
indiv_id=indiv_id,
296+
new_cost=float(value),
297+
new_ev_time=time.time(),
298+
generation=self._current_gen)
299+
elif left.column() == 5:
300+
# TODO
301+
pass
270302

271303
def _update_individuals_per_generation_list(self):
272304
# Clean up ye olde list
@@ -297,12 +329,12 @@ def _update_individuals_per_generation_list(self):
297329

298330
for pop_index in xrange(1, indivs_per_gen + 1):
299331
indiv_index = pop_individuals[pop_index - 1]
300-
indiv_cost = costs[pop_index - 1]
332+
indiv_cost = str(costs[pop_index - 1])
301333
indiv_value = individuals[indiv_index].get_value()
302334
indiv_appearences = individuals[indiv_index].get_appearances()
303335

304336
indiv_gen_method = Population.gen_method_description(gen_methods[pop_index - 1])
305-
gens_list.append([pop_index, indiv_index + 1, indiv_gen_method,
337+
gens_list.append([pop_index, indiv_index, indiv_gen_method,
306338
indiv_appearences, indiv_cost, indiv_value])
307339

308340
self._individuals_per_generation.append(gens_list)
@@ -358,6 +390,7 @@ def _update_experiment_info(self):
358390
db_view.resizeColumnsToContents()
359391
db_view.setSortingEnabled(True)
360392
table_model.set_editable_columns(editable_columns)
393+
table_model.set_data_changed_callback(self._db_view_edited)
361394
table_model.sort_by_col(0)
362395

363396
# Refresh the gen_count_label

MLC/GUI/Tables/ConfigTableModel.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ def setData(self, index, value, role):
8888
.format(self._name, index.row(), index.column()))
8989
return False
9090

91+
def set_data(self, row, col, value):
92+
try:
93+
# Check if the row exists, indexing the element to modify
94+
self.layoutAboutToBeChanged.emit()
95+
self._data[row][col] = value
96+
self.layoutChanged.emit()
97+
except IndexError:
98+
logger.error("[TABLE_VIEW] [{0}] - [SET_DATA] IndexError while retrieving data. "
99+
"Row: {1} - Col: {2}"
100+
.format(self._name, index.row(), index.column()))
101+
91102
def get_data(self, row, col):
92103
try:
93104
return self._data[row][col]

MLC/api/MLCLocal.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,6 @@ def go(self, experiment_name, to_generation, from_generation=0, callbacks={}):
211211
return True
212212

213213
def get_individuals(self, experiment_name):
214-
"""
215-
Return a list with IndividualData (see MLCRepository::IndividualData)
216-
:param experiment_name:
217-
:return:
218-
"""
219214
if experiment_name not in self._experiments:
220215
raise ExperimentNotExistException(experiment_name)
221216

@@ -229,6 +224,20 @@ def get_individuals(self, experiment_name):
229224
individuals = MLCRepository.get_instance().get_individuals_data()
230225
return individuals
231226

227+
def get_individual(self, experiment_name, individual_id):
228+
if experiment_name not in self._experiments:
229+
raise ExperimentNotExistException(experiment_name)
230+
231+
if experiment_name not in self._open_experiments:
232+
raise ClosedExperimentException("get_experiment_info", experiment_name)
233+
234+
# get simulation in order to load mlc experiment database
235+
simulation = self._open_experiments[experiment_name].get_simulation()
236+
237+
# obtain individuals from the database
238+
individual = MLCRepository.get_instance().get_individual_data(individual_id)
239+
return individual
240+
232241
def update_individual_cost(self, experiment_name, indiv_id, new_cost, new_ev_time, generation=-1):
233242
if experiment_name not in self._experiments:
234243
raise ExperimentNotExistException(experiment_name)

MLC/api/mlc.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ def __init__(self, experiment_name, script_path):
3535
"existent evaluation script. Script: {1}"
3636
.format(experiment_name, script_path))
3737

38+
3839
class ImportExperimentPathNotExistException(MLCException):
3940

4041
def __init__(self, experiment_path):
4142
MLCException.__init__(self, "Import Experiment Error: Path {0} does not exists."
4243
.format(experiment_path))
4344

45+
4446
class MLC:
4547

4648
def open_experiment(self, experiment_name):
@@ -159,14 +161,22 @@ def get_generation(self, experiment_name, generation_number):
159161
"""
160162
raise NotImplementedError("MLC::get_generation not implemented")
161163

162-
def get_individuals(self, experiment_name, individual_id=None):
164+
def get_individuals(self, experiment_name):
163165
"""
164166
Obtained generated individuals during the simulation.
165167
:param experiment_name:
166168
:return:
167169
"""
168170
raise NotImplementedError("MLC::get_individuals not implemented")
169171

172+
def get_individual(self, experiment_name, individual_id):
173+
"""
174+
Obtained generated individuals during the simulation.
175+
:param experiment_name:
176+
:return:
177+
"""
178+
raise NotImplementedError("MLC::get_individual not implemented")
179+
170180
def update_individual_cost(self, experiment_name, indiv_id, new_cost, new_ev_time, generation=-1):
171181
"""
172182
Update individual cost. If generation == -1 Individual cost will

MLC/db/mlc_repository.py

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

99

1010
class MLCRepositoryHelper:
11+
1112
@staticmethod
1213
def get_hash_for_individual(individual):
1314
m = hashlib.md5()
@@ -30,6 +31,7 @@ class IndividualData:
3031
appearances:
3132
number of time the individual appears
3233
"""
34+
3335
def __init__(self, value):
3436
self._value = value
3537
self._cost_history = defaultdict(list)

0 commit comments

Comments
 (0)