Esempio n. 1
0
    def test_generate_feasible_solution_lpt(
            self):  # Note this test fails on data/given_data
        fjs_data = get_files_with_suffix(project_root / 'data/fjs_data',
                                         '.fjs')
        for fjs_instance in random.choices(fjs_data, k=10):
            try:
                print(
                    f"test_generate_feasible_solution_lpt with fjs data: {fjs_instance}"
                )
                SolutionFactory(data.FJSData(fjs_instance)
                                ).get_n_longest_process_time_first_solution(50)

            except InfeasibleSolutionException:
                self.fail("Infeasible solution was generated")
    def test_crossover(self):

        fjs_data = get_files_with_suffix(project_root / 'data/fjs_data', '.fjs')
        num_choices = 10
        probability_mutation = 0.5

        for i, fjs_instance in enumerate(random.choices(fjs_data, k=num_choices)):
            print(f"testing GA crossover function for fjs instance {fjs_instance} ({i + 1} of {num_choices})")
            instance_data = data.FJSData(fjs_instance)
            try:
                for _ in range(50):
                    parent1 = SolutionFactory(instance_data).get_solution()
                    parent2 = SolutionFactory(instance_data).get_solution()
                    crossover(parent1, parent2, probability_mutation,
                              instance_data.job_task_index_matrix, instance_data.usable_machines_matrix)
                    crossover(parent2, parent1, probability_mutation,
                              instance_data.job_task_index_matrix, instance_data.usable_machines_matrix)
            except InfeasibleSolutionException:
                self.fail("Infeasible child created")
    def test_create_fjs_data(self):
        fjs_lst = get_files_with_suffix(project_root / 'data/fjs_data', '.fjs')

        for i, fjs_instance in enumerate(fjs_lst):
            print(
                f"testing fjs instance {fjs_instance} ({i + 1} of {len(fjs_lst)})"
            )
            fjs_data = data.FJSData(fjs_instance)

            self.assertIsNotNone(fjs_data.fjs_file_path)
            self.assertIsNotNone(fjs_data.sequence_dependency_matrix)
            self.assertIsNotNone(fjs_data.job_task_index_matrix)
            self.assertIsNotNone(fjs_data.usable_machines_matrix)
            self.assertIsNotNone(fjs_data.task_processing_times_matrix)

            self.assertNotEqual([], fjs_data.jobs)
            self.assertIsNotNone(fjs_data.total_number_of_jobs)
            self.assertIsNotNone(fjs_data.total_number_of_tasks)
            self.assertIsNotNone(fjs_data.total_number_of_machines)
            self.assertIsNotNone(fjs_data.max_tasks_for_a_job)
    def test_converting_fjs_instances(self):
        fjs_lst = get_files_with_suffix(project_root / 'data/fjs_data', '.fjs')

        for i, fjs_instance in enumerate(fjs_lst):
            print(
                f"testing fjs instance {fjs_instance} ({i + 1} of {len(fjs_lst)})"
            )
            fjs_data = data.FJSData(fjs_instance)

            # copy all of the data that was read in
            sequence_dependency_matrix = np.copy(
                fjs_data.sequence_dependency_matrix)
            job_task_index_matrix = np.copy(fjs_data.job_task_index_matrix)
            usable_machines_matrix = np.copy(fjs_data.usable_machines_matrix)
            task_processing_times_matrix = np.copy(
                fjs_data.task_processing_times_matrix)
            jobs = fjs_data.jobs[:]
            total_number_of_jobs = fjs_data.total_number_of_jobs
            total_number_of_tasks = fjs_data.total_number_of_tasks
            total_number_of_machines = fjs_data.total_number_of_machines
            max_tasks_for_a_job = fjs_data.max_tasks_for_a_job

            data.Data.convert_fjs_to_csv(fjs_instance, tmp_dir)

            # read in converted csv file
            csv_data = data.CSVData(tmp_dir / 'sequenceDependencyMatrix.csv',
                                    tmp_dir / 'machineRunSpeed.csv',
                                    tmp_dir / 'jobTasks.csv')

            # make sure the data is the same
            np.testing.assert_array_equal(
                sequence_dependency_matrix,
                csv_data.sequence_dependency_matrix,
                err_msg=
                f'sequence dependency matrices are not equal for {fjs_instance}'
            )
            np.testing.assert_array_equal(
                job_task_index_matrix,
                csv_data.job_task_index_matrix,
                err_msg=
                f'job-task index matrices are not equal for {fjs_instance}')
            np.testing.assert_array_equal(
                usable_machines_matrix,
                csv_data.usable_machines_matrix,
                err_msg=
                f'usable machines matrices are not equal for {fjs_instance}')

            # TODO task_processing_times_matrix will not always be equal because of the way Data.convert_fjs_to_csv is implemented
            # np.testing.assert_array_equal(task_processing_times_matrix, csv_data.task_processing_times_matrix)

            self.assertEqual(
                task_processing_times_matrix.shape,
                csv_data.task_processing_times_matrix.shape,
                f'task processing times matrices are not same shape for {fjs_instance}'
            )
            self.assertEqual(jobs, csv_data.jobs,
                             f'jobs lists are not equal for {fjs_instance}')
            self.assertEqual(
                total_number_of_jobs, csv_data.total_number_of_jobs,
                f'total number of jobs are not equal for {fjs_instance}')
            self.assertEqual(
                total_number_of_tasks, csv_data.total_number_of_tasks,
                f'total number of tasks are not equal for {fjs_instance}')
            self.assertEqual(
                total_number_of_machines, csv_data.total_number_of_machines,
                f'total number of machines are not equal for {fjs_instance}')
            self.assertEqual(
                max_tasks_for_a_job, csv_data.max_tasks_for_a_job,
                f'max tasks for a job are not equal for {fjs_instance}')
Esempio n. 5
0
import random
import unittest

from JSSP import data
from JSSP.solver import Solver
from tests.util import project_root, tmp_dir, get_files_with_suffix, rm_tree

fjs_data = get_files_with_suffix(project_root / 'data/fjs_data', '.fjs')
fjs_data = random.choices(fjs_data, k=20)


class TestFJSOptimization(unittest.TestCase):
    def setUp(self) -> None:
        if not tmp_dir.exists():
            tmp_dir.mkdir()

    def tearDown(self) -> None:
        rm_tree(tmp_dir)

    def test_ts_iter(self):
        # parameters
        iterations = 50  # keep this value small
        num_processes = 1
        tabu_list_size = 10
        neighborhood_size = 25
        neighborhood_wait = 0.1
        probability_change_machine = 0.8

        for i, fjs_instance in enumerate(fjs_data):
            print(
                f"testing fjs instance {fjs_instance} ({i + 1} of {len(fjs_data)})"