Skip to content

Commit 9373cff

Browse files
author
Ezequiel Torres
committed
Added start over button functionality
* #49
1 parent f725730 commit 9373cff

File tree

7 files changed

+146
-11
lines changed

7 files changed

+146
-11
lines changed

MLC/Common/LispTreeExpr/LispTreeExpr.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ def check_operands(expr):
144144
break
145145

146146
if arg_counter != expr_op["nbarg"]:
147-
print arg_counter
148147
raise OperationArgumentsAmountException(expr)
149148

150149
# The position always finished in the last bracket.

MLC/GUI/Autogenerated/mlc_qtcreator/experiment.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<enum>QTabWidget::North</enum>
2525
</property>
2626
<property name="currentIndex">
27-
<number>1</number>
27+
<number>0</number>
2828
</property>
2929
<widget class="QWidget" name="experiment_tab">
3030
<property name="layoutDirection">

MLC/GUI/Experiment/ExperimentWindow.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from MLC.Population.Population import Population
2525
from PyQt5.QtCore import pyqtSignal
2626
from PyQt5.QtCore import QFileSystemWatcher
27-
# from PyQt5.QtCore import QHeaderView
2827
from PyQt5.QtCore import Qt
2928
from PyQt5.QtCore import QUrl
3029
from PyQt5.QtGui import QDesktopServices
@@ -435,29 +434,65 @@ def on_gen_cut_button_clicked(self):
435434
QMessageBox.No)
436435

437436
if response == QMessageBox.Yes:
438-
logger.info("[GEN_CUT_BUTTON] Proceed to remove {0} generations (From {1} onwards)"
437+
logger.info("[GEN_CUT_BUTTON] Proceed to remove {0} generations (From Generation N°{1} onwards)"
439438
.format(amount_of_gen_losts, self._current_gen))
440439
self._mlc_local.remove_generations_from(self._experiment_name,
441440
self._current_gen)
442441
last_gen_removed = self._current_gen
443442
# Recalculate the amount of generations in the project
444443
experiment_info = self._mlc_local.get_experiment_info(self._experiment_name)
445-
number_of_gens = experiment_info["generations"]
446-
self._current_gen = number_of_gens
444+
self._current_gen = experiment_info["generations"]
447445
self._update_individuals_per_generation_list()
448446
self._update_experiment_info()
449447
self._update_individuals_figure()
450448
self._update_scatter_chart()
451449

452450
QMessageBox.information(self, "Cut Generations",
453-
"{0} generations were succesfully removed (From {1} onwards)"
451+
"{0} Generations were succesfully removed (From {1} onwards)"
454452
.format(amount_of_gen_losts, last_gen_removed))
455453

456454
def on_gen_start_over_button_clicked(self):
457455
logger.debug('[EXPERIMENT {0}] [GEN_START_OVER_BUTTON] - '
458456
'Executing on_gen_start_over_button_clicked function'
459457
.format(self._experiment_name))
460458

459+
response = QMessageBox.warning(self, "Start Over",
460+
"Except for Generation N°{0}, all generations will be removed. "
461+
"Do you really want to continue?"
462+
.format(self._current_gen),
463+
QMessageBox.Yes | QMessageBox.No,
464+
QMessageBox.No)
465+
466+
if response == QMessageBox.Yes:
467+
chosen_generation = self._current_gen
468+
logger.info("[GEN_START_OVER] Removing all the experiment generations "
469+
"except for generation N°{0}".format(self._current_gen))
470+
471+
experiment_info = self._mlc_local.get_experiment_info(self._experiment_name)
472+
number_of_gens = experiment_info["generations"]
473+
474+
if self._current_gen != number_of_gens:
475+
self._mlc_local.remove_generations_from(self._experiment_name,
476+
self._current_gen + 1)
477+
478+
if self._current_gen != 1:
479+
self._mlc_local.remove_generations_to(self._experiment_name,
480+
self._current_gen - 1)
481+
482+
# Recalculate the amount of generations in the project
483+
experiment_info = self._mlc_local.get_experiment_info(self._experiment_name)
484+
self._current_gen = experiment_info["generations"]
485+
self._update_individuals_per_generation_list()
486+
self._update_experiment_info()
487+
self._update_individuals_figure()
488+
self._update_scatter_chart()
489+
490+
QMessageBox.information(self, "Star Over Generations",
491+
"Experiment was succesfully pruned. Past Generation "
492+
"N°{0} is the only generation of the experiment"
493+
.format(chosen_generation))
494+
495+
461496
################################### VIEW AND MODEL METHODS #########################################
462497

463498
def _config_table_edited(self, left, right):
@@ -714,7 +749,6 @@ def _update_individuals_figure(self):
714749
chart_layout.itemAt(i).widget().setParent(None)
715750
chart_layout.addWidget(self._autogenerated_object.textEdit_2)
716751

717-
718752
def _reload_experiment_config(self, some_string):
719753
logger.debug('[EXPERIMENT {0}] [EXPERIMENT_CONF_FILE_CHANGED] - '
720754
'Executing _experiment_conf_file_changed function'

MLC/api/MLCLocal.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def get_experiment_info(self, experiment_name):
219219
experiment_info["best_indiv_value"] = min_indiv_data.get_value()
220220
return experiment_info
221221

222-
def go(self, experiment_name, to_generation, from_generation=0,
222+
def go(self, experiment_name, to_generation, from_generation=0,
223223
callbacks={}, gen_creator=None):
224224
if experiment_name not in self._experiments:
225225
raise ExperimentNotExistException(experiment_name)
@@ -273,6 +273,16 @@ def remove_generations_from(self, experiment_name, gen_number):
273273
MLCRepository.get_instance().remove_population_from(gen_number)
274274
MLCRepository.get_instance().remove_unused_individuals()
275275

276+
def remove_generations_to(self, experiment_name, gen_number):
277+
if experiment_name not in self._experiments:
278+
raise ExperimentNotExistException(experiment_name)
279+
280+
if experiment_name not in self._open_experiments:
281+
raise ClosedExperimentException("get_experiment_info", experiment_name)
282+
283+
MLCRepository.get_instance().remove_population_to(gen_number)
284+
MLCRepository.get_instance().remove_unused_individuals()
285+
276286
def get_individual(self, experiment_name, individual_id):
277287
if experiment_name not in self._experiments:
278288
raise ExperimentNotExistException(experiment_name)

MLC/api/mlc.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,15 @@ def get_generation(self, experiment_name, generation_number):
190190

191191
def remove_generations_from(self, experiment_name, gen_number):
192192
"""
193-
Remove all the generations above the given as a parameter
193+
Remove all the generations above the given as a parameter (include the gen given)
194+
:param experiment_name
195+
:param gen_number
196+
"""
197+
198+
def remove_generations_to(self, experiment_name, gen_number):
199+
"""
200+
Remove all the generations below the given as a parameter (include the gen given)
201+
the one passed as an argument
194202
:param experiment_name
195203
:param gen_number
196204
"""

MLC/db/sqlite/sqlite_repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def remove_population_to(self, to_generation):
129129
if self.__generations == 0:
130130
self.__base_gen = 1
131131
else:
132-
self.__base_gen = to_generation + 1
132+
self.__base_gen = self.__base_gen + to_generation
133133

134134
def remove_unused_individuals(self):
135135
to_delete = []

tests/mlc/db/test_mlc_repository.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,90 @@ def test_cut_generation(self):
541541
individual = mlc_repo.get_individual(4)
542542
self.assertEqual(individual.get_value(), "4+4")
543543

544+
545+
def test_cut_generations_more_than_once(self):
546+
"""
547+
Cut a generation using remove_population_from/remove_population_last
548+
:return:
549+
"""
550+
mlc_repo = self.__get_new_repo()
551+
552+
# add individuals
553+
mlc_repo.add_individual(Individual("1+1"))
554+
mlc_repo.add_individual(Individual("2+2"))
555+
mlc_repo.add_individual(Individual("3+3"))
556+
mlc_repo.add_individual(Individual("4+4"))
557+
mlc_repo.add_individual(Individual("5+5"))
558+
mlc_repo.add_individual(Individual("6+6"))
559+
mlc_repo.add_individual(Individual("7+7"))
560+
mlc_repo.add_individual(Individual("8+8"))
561+
562+
# add population
563+
p = Population(3, 0, Config.get_instance(), mlc_repo)
564+
p._individuals = [1, 1, 1]
565+
mlc_repo.add_population(p)
566+
567+
# add population
568+
p = Population(3, 0, Config.get_instance(), mlc_repo)
569+
p._individuals = [2, 2, 2]
570+
mlc_repo.add_population(p)
571+
572+
# add population
573+
p = Population(3, 0, Config.get_instance(), mlc_repo)
574+
p._individuals = [3, 3, 3]
575+
mlc_repo.add_population(p)
576+
577+
# add population
578+
p = Population(3, 0, Config.get_instance(), mlc_repo)
579+
p._individuals = [4, 4, 4]
580+
mlc_repo.add_population(p)
581+
582+
# add population
583+
p = Population(3, 0, Config.get_instance(), mlc_repo)
584+
p._individuals = [5, 5, 5]
585+
mlc_repo.add_population(p)
586+
587+
self.assertEqual(mlc_repo.count_population(), 5)
588+
589+
# Cut population 4
590+
mlc_repo.remove_population_from(4+1)
591+
mlc_repo.remove_population_to(4-1)
592+
593+
p = Population(3, 0, Config.get_instance(), mlc_repo)
594+
p._individuals = [5, 5, 5]
595+
mlc_repo.add_population(p)
596+
597+
p = Population(3, 0, Config.get_instance(), mlc_repo)
598+
p._individuals = [6, 6, 6]
599+
mlc_repo.add_population(p)
600+
601+
p = Population(3, 0, Config.get_instance(), mlc_repo)
602+
p._individuals = [7, 7, 7]
603+
mlc_repo.add_population(p)
604+
605+
p = Population(3, 0, Config.get_instance(), mlc_repo)
606+
p._individuals = [8, 8, 8]
607+
mlc_repo.add_population(p)
608+
609+
self.assertEqual(mlc_repo.count_population(), 5)
610+
611+
# Cut population 4
612+
mlc_repo.remove_population_from(4+1)
613+
mlc_repo.remove_population_to(4-1)
614+
615+
# remove unused individuals
616+
mlc_repo.remove_unused_individuals()
617+
618+
self.assertEqual(mlc_repo.count_population(), 1)
619+
self.assertEqual(mlc_repo.count_individual(), 1)
620+
621+
p = mlc_repo.get_population(1)
622+
self.assertEqual(p._individuals, [7, 7, 7])
623+
624+
individual = mlc_repo.get_individual(7)
625+
self.assertEqual(individual.get_value(), "7+7")
626+
627+
544628
def test_remove_population_to_from_bad_values(self):
545629
mlc_repo = self.__get_new_repo()
546630

0 commit comments

Comments
 (0)