Skip to content

Commit

Permalink
project_task_scheduling: Change the name of a function
Browse files Browse the repository at this point in the history
Change the name of a function and improve its docstring
  • Loading branch information
ernestotejeda committed Sep 25, 2018
1 parent 4101b4c commit 445a350
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
18 changes: 10 additions & 8 deletions project_task_scheduling/tests/test_scheduling_wizard.py
Expand Up @@ -140,12 +140,12 @@ def test_get_init_state(self):
self.assertEqual(assigment.end_datetime, data_check[1])
self.assertEqual(assigment.data['employee'], data_check[2])

def test_obj_func(self):
def test_evaluation_function(self):
max_hours_delayed = self.wizard._MAX_HOURS_DELAYED

# evaluate initial state
init_state = self.wizard._get_init_state()
evaluation = self.wizard._obj_func(init_state)
evaluation = self.wizard._evaluation_function(init_state)

# two tasks are scheduled after their date_deadline
# (self.task_7 and self.task_1) see test_get_init_state_1 method
Expand All @@ -165,14 +165,14 @@ def test_obj_func(self):

self.assertEqual(evaluation, expected_eval)

def test_obj_func_fail(self):
def test_evaluation_function_fail(self):
date_start = fields.Datetime.from_string(self.wizard.date_start)
date_deadline = date_start + timedelta(days=50000)
self.task_2.write({'date_deadline': date_deadline})

state = self.wizard._get_init_state()
with self.assertRaises(ValidationError):
self.wizard._obj_func(state)
self.wizard._evaluation_function(state)

def test_generate_neighbor_false(self):
init_st = self.wizard._get_init_state()
Expand Down Expand Up @@ -218,20 +218,22 @@ def test_generate_neighbor_pos_arg_1(self):

def test_generate_neighbor_pos_arg_1_is_better_than_start_state(self):
init_st = self.wizard._get_init_state()
eval_init_st = round(self.wizard._obj_func(init_st), 10)
eval_init_st = round(self.wizard._evaluation_function(init_st), 10)

neighbor_st = self.wizard._generate_neighbor(init_st, pos_arg=1)
eval_neighbor_st = round(self.wizard._obj_func(neighbor_st), 10)
eval_neighbor_st = self.wizard._evaluation_function(neighbor_st)
eval_neighbor_st = round(eval_neighbor_st, 10)

self.assertLess(eval_neighbor_st, eval_init_st)

def test_simulated_annealing_can_improve_initial_state(self):
init_state = self.wizard._get_init_state()
eval_init_st = round(self.wizard._obj_func(init_state), 10)
eval_init_st = round(self.wizard._evaluation_function(init_state), 10)

cooling_ratio = float(self.wizard.cooling_ratio)
sa_state = self.wizard.simulated_annealing(cooling_ratio=cooling_ratio)
eval_sa_state = round(self.wizard._obj_func(sa_state[-1]), 10)
eval_sa_state = self.wizard._evaluation_function(sa_state[-1])
eval_sa_state = round(eval_sa_state, 10)

self.assertLessEqual(eval_sa_state, eval_init_st)

Expand Down
22 changes: 18 additions & 4 deletions project_task_scheduling/wizards/scheduling_wizard.py
Expand Up @@ -494,8 +494,21 @@ def _get_employees_dict(self):

return employees_dict

def _obj_func(self, state):
""" It's called from simulated_annealing method to evaluate a state
def _evaluation_function(self, state):
""" Evaluation of the given state
This function is called from simulated_annealing method to
evaluate a state. It's based on delayed task and the sum of the
lateness over all tasks.
The lateness of a task 'i' is defined as the difference between the
completion time of the task and its due date. The lateness can be
zero (if the task finishes exactly on time), positive (if the task
finishes late) or negative (if the task finishes early).
It's the function to be minimized by simulated annealing algorithm
implemented in simulated_annealing method. It's called
objective function.
:param _state_obj state: state
Expand Down Expand Up @@ -833,7 +846,7 @@ def simulated_annealing(self, init_temp=100000000, final_temp=0.1,
states_list = []

st = self._get_init_state()
best_eval = eval_st = round(self._obj_func(st), 10)
best_eval = eval_st = round(self._evaluation_function(st), 10)
states_list.append(st._replace(evaluation=best_eval))

while temp >= final_temp and current_duration < max_duration:
Expand All @@ -842,7 +855,8 @@ def simulated_annealing(self, init_temp=100000000, final_temp=0.1,
if not st_neighbor:
continue
it_count += 1
eval_st_neighbor = round(self._obj_func(st_neighbor), 10)
eval_st_neighbor = self._evaluation_function(st_neighbor)
eval_st_neighbor = round(eval_st_neighbor, 10)
eval_diff = eval_st_neighbor - eval_st
if eval_diff < 0:
st = st_neighbor
Expand Down

0 comments on commit 445a350

Please sign in to comment.