Esempio n. 1
0
    def localSearch(self, solution, solution_fitness, materials_changed):
        new_concept_coverage = solution
        new_best_fitness = solution_fitness

        for j in range(self.local_search_size):
            fitness_improved = False
            for material in materials_changed:
                for concept in range(num_concepts):
                    step_concept_coverage = new_concept_coverage.copy()
                    step_concept_coverage[
                        material,
                        concept] = ~step_concept_coverage[material, concept]
                    step_fitness = sum([
                        Fitness.get_fitnessConcepts(student_id,
                                                    step_concept_coverage.T)
                        for student_id in range(num_students)
                    ]) / num_students

                    if new_best_fitness > step_fitness:
                        new_best_fitness = step_fitness
                        new_concept_coverage = step_concept_coverage
                        fitness_improved = True

            if not fitness_improved:
                break
        return new_concept_coverage, new_best_fitness
    def run(self, concept_coverage, fitnessConcepts_total, DATFILE=None):
        best_solution = concept_coverage
        best_fitness = fitnessConcepts_total

        current_temperature = self.initial_temperature
        current_solution = concept_coverage
        current_fitness = fitnessConcepts_total

        self.get_orderOfModifiedMaterials()
        fitness_progress = np.empty(self.max_iterations)

        for i in range(self.max_iterations):
            for l in range(self.cycle):
                next_solution = self.get_randNeighbor(best_solution)
                next_fitness = sum([
                    Fitness.get_fitnessConcepts(student_id, current_solution.T)
                    for student_id in range(num_students)
                ]) / num_students

                deltaE = next_fitness - current_fitness
                if (deltaE < 0.0):
                    current_solution = next_solution
                    current_fitness = next_fitness

                    if (next_fitness < best_fitness):
                        best_solution = next_solution
                        best_fitness = next_fitness
                else:
                    if (random.uniform(0, 1) < math.exp(
                            -deltaE / current_temperature)):
                        current_solution = next_solution
                        current_fitness = next_fitness

            current_temperature = self.decreaseTemperature(current_temperature)
            fitness_progress[i] = best_fitness

        # print("current_temperature: ", current_temperature)
        if (DATFILE):
            with open(DATFILE, 'w') as f:
                f.write(str(best_fitness))
        else:
            with open('results_SA.pickle', 'wb') as file:
                pickle.dump(
                    {
                        "fitness_progress": fitness_progress,
                        "sa_concept_coverage": best_solution,
                        "sa_fitness": best_fitness
                    }, file)

            return best_solution, best_fitness
Esempio n. 3
0
    def greadyRandomizedConstruction(self):
        student_yes = np.matmul(
            recommendation.astype(int), objectives.astype(int)
        )  #quantos alunos receberam o material E o tem como objetivo
        student_no = np.matmul(
            recommendation.astype(int), (~objectives).astype(int)
        )  #quantos alunos receberam o material E não o tem como objetivo
        student_difference = student_no - student_yes  #(quantos alunos querem ter o conveito adicionado) - (quantos alunos querem ter o conceito removido)
        scaled_coverage = (concept_coverage * 2 - 1
                           )  # concept_coverage, onde False é -1 e True é 1
        conflicts = student_difference * scaled_coverage
        change_potential = np.maximum(0, conflicts).sum(axis=1)

        materials_changed = []
        new_concept_coverage = concept_coverage.copy()
        change_potential_order = np.argsort(-change_potential).tolist()

        for j in range(int(self.max_materials_changes * num_materials)):
            # Select a material and remove from the list
            selected_material_index = random.randrange(
                int(len(change_potential_order) * self.alpha_m))
            material = change_potential_order[selected_material_index]
            materials_changed.append(material)
            del change_potential_order[selected_material_index]

            # print('[', material, change_potential[material], ']')
            # print(conflicts[material])

            conflicts_order = np.argsort(-conflicts[material]).tolist()
            for k in range(int(self.max_concepts_changes * num_concepts)):
                # Select a concept from the material and remove from the list
                selected_concept_index = random.randrange(
                    int(len(conflicts_order) * self.alpha_c))
                concept = conflicts_order[selected_concept_index]
                del conflicts_order[selected_concept_index]

                # print(material, concept)
                if conflicts[material, concept] > 0:
                    new_concept_coverage[
                        material,
                        concept] = ~new_concept_coverage[material, concept]

        new_best_fitness = sum([
            Fitness.get_fitnessConcepts(student_id, new_concept_coverage.T)
            for student_id in range(num_students)
        ]) / num_students
        return new_concept_coverage, new_best_fitness, materials_changed
                    type=float,
                    help='Final temperature')
    ap.add_argument('-a', '--alpha', type=float, default=0.90, help='alpha')
    ap.add_argument('-b', '--beta', type=str, default=0.2, help='beta')
    ap.add_argument('--datfile',
                    dest='datfile',
                    type=str,
                    help='File where it will be save the score (result)')

    args = ap.parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)

    logging.debug(args)

    student_results_before = sum([
        Fitness.get_fitnessConcepts(student_id, concept_coverage.T)
        for student_id in range(num_students)
    ]) / num_students

    simulatedAnnealing = SimulatedAnnealing(args.max_iterations, args.cycle,
                                            args.initial_temperature,
                                            args.final_temperature, args.alpha,
                                            args.beta, 0.0356, 0.1667,
                                            98092891)

    simulatedAnnealing.run(concept_coverage, student_results_before,
                           args.datfile)
    # annealing_concept_coverage, student_results_annealing = simulatedAnnealing.run(concept_coverage, student_results_before)
    # print(f'student_results_annealing: {student_results_annealing}')
Esempio n. 5
0
import pickle
import numpy as np
import matplotlib.pyplot as plt

import config
from fitness import Fitness

old_student_results = [Fitness.get_fitnessConcepts(student_id, config.concept_coverage.T) for student_id in range(config.num_students)]
student_results_before = sum(old_student_results)/config.num_students
print(f'student_results_before: {student_results_before}')



grasp_results = pickle.load( open( "results_grasp.pickle", "rb" ) )
grasp_concept_coverage = grasp_results["grasp_concept_coverage"]
grasp_progress = grasp_results["fitness_progress"]

grasp_student_results = [Fitness.get_fitnessConcepts(student_id, grasp_concept_coverage.T) for student_id in range(config.num_students)]


sa_results = pickle.load( open( "results_SA.pickle", "rb" ) )
sa_concept_coverage = sa_results["sa_concept_coverage"]
sa_progress = sa_results["fitness_progress"]

annealing_student_results = [Fitness.get_fitnessConcepts(student_id, sa_concept_coverage.T) for student_id in range(config.num_students)]



fig = plt.figure()
plt.bar(np.arange(config.num_students)-0.2, old_student_results,       width=0.2, align='center', label='None')
plt.bar(np.arange(config.num_students)    , annealing_student_results, width=0.2, align='center', label='SA')