def _evaluate(self, x, # out, *args, **kwargs): """ This method iterate over an Individual, execute the refactoring operation sequentially, and compute quality attributes for the refactored version of the program, as objectives of the search params: x (Individual): x is an instance of Individual (i.e., a list of refactoring operations) """ # Stage 0: Git restore logger.debug("Executing git restore.") git_restore(config.PROJECT_PATH) update_understand_database(config.UDB_PATH) # Stage 1: Execute all refactoring operations in the sequence x logger.debug(f"Reached Individual with Size {len(x[0])}") for refactoring_operation in x[0]: refactoring_operation.do_refactoring() # Update Understand DB update_understand_database(config.UDB_PATH) # Stage 2: Computing quality attributes obj = DesignQualityAttributes(udb_path=config.UDB_PATH) o1 = obj.average_sum del obj o2 = testability_main(config.UDB_PATH) o3 = modularity_main(config.UDB_PATH) logger.info(f"QMOOD AVG Score: {o1}") logger.info(f"Testability Score: {o2}") logger.info(f"Modularity Score: {o3}") # Stage 3: Marshal objectives into vector out["F"] = np.array([-1 * o1, -1 * o2, -1 * o3], dtype=float)
def _evaluate(self, x, # out, *args, **kwargs): """ This method iterate over a population, execute the refactoring operations in each individual sequentially, and compute quality attributes for the refactored version of the program, as objectives of the search Args: x (Population): x is a matrix where each row is an individual, and each column a variable. \ We have one variable of type list (Individual) ==> x.shape = (len(Population), 1) """ objective_values = [] for k, individual_ in enumerate(x): # Stage 0: Git restore logger.debug("Executing git restore.") git_restore(config.PROJECT_PATH) logger.debug("Updating understand database after git restore.") update_understand_database(config.UDB_PATH) # Stage 1: Execute all refactoring operations in the sequence x logger.debug(f"Reached Individual with Size {len(individual_[0])}") for refactoring_operation in individual_[0]: refactoring_operation.do_refactoring() # Update Understand DB logger.debug(f"Updating understand database after {refactoring_operation.name}.") update_understand_database(config.UDB_PATH) # Stage 2: arr = Array('d', range(self.n_obj_virtual)) if self.evaluate_in_parallel: # Stage 2 (parallel mood): Computing quality attributes p1 = Process(target=calc_qmood_objectives, args=(arr,)) p2 = Process(target=calc_testability_objective, args=(config.UDB_PATH, arr,)) p3 = Process(target=calc_modularity_objective, args=(config.UDB_PATH, arr,)) p1.start(), p2.start(), p3.start() p1.join(), p2.join(), p3.join() o1 = sum([i for i in arr[:6]]) / 6. o2 = arr[6] o3 = arr[7] else: # Stage 2 (sequential mood): Computing quality attributes qmood_quality_attributes = DesignQualityAttributes(udb_path=config.UDB_PATH) o1 = qmood_quality_attributes.average_sum o2 = testability_main(config.UDB_PATH, initial_value=config.CURRENT_METRICS.get("TEST", 1.0)) o3 = modularity_main(config.UDB_PATH, initial_value=config.CURRENT_METRICS.get("MODULE", 1.0)) del qmood_quality_attributes # Stage 3: Marshal objectives into vector objective_values.append([-1 * o1, -1 * o2, -1 * o3]) logger.info(f"Objective values for individual {k}: {[-1 * o1, -1 * o2, -1 * o3]}") # Stage 4: Marshal all objectives into out dictionary out['F'] = np.array(objective_values, dtype=float)
def _evaluate( self, x, # out, *args, **kwargs): """ This method iterate over an Individual, execute the refactoring operation sequentially, and compute quality attributes for the refactored version of the program, as objectives of the search params: x (Individual): x is an instance of Individual (i.e., a list of refactoring operations) """ # Git restore` logger.debug("Executing git restore.") git_restore(config.PROJECT_PATH) update_understand_database(config.UDB_PATH) # Stage 1: Execute all refactoring operations in the sequence x logger.debug(f"Reached Individual with Size {len(x[0])}") for refactoring_operation in x[0]: refactoring_operation.do_refactoring() # Update Understand DB update_understand_database(config.UDB_PATH) # Stage 2: Computing quality attributes qmood = DesignQualityAttributes(udb_path=config.UDB_PATH) o1 = qmood.reusability o2 = qmood.understandability o3 = qmood.flexibility o4 = qmood.functionality o5 = qmood.effectiveness o6 = qmood.extendability del qmood o7 = testability_main(config.UDB_PATH) o8 = modularity_main(config.UDB_PATH) logger.info(f"Reusability Score: {o1}") logger.info(f"Understandability Score: {o2}") logger.info(f"Flexibility Score: {o3}") logger.info(f"Functionality Score: {o4}") logger.info(f"Effectiveness Score: {o5}") logger.info(f"Extendability Score: {o6}") logger.info(f"Testability Score: {o7}") logger.info(f"Modularity Score: {o8}") # Stage 3: Marshal objectives into vector out["F"] = np.array([ -1 * o1, -1 * o2, -1 * o3, -1 * o4, -1 * o5, -1 * o6, -1 * o7, -1 * o8, ], dtype=float)
rand_pop = rand_init.generate_population() score = DesignQualityAttributes(udb_path=udb_path).reusability k = 0 limit = len(rand_pop) best_answer = None print("Reusability before starting the algorithm:", score) # Random search for improving reusability while k < MAX_ITERATIONS and limit > 0: print("Iteration No.", k) index = random.randint(0, limit - 1) limit -= 1 print(f"index is {index}") individual = rand_pop.pop(index) print(f"len(individual) is {len(individual)}") # git restore prev changes git_restore(project_dir) # execute individual for refactoring, params in individual: print(params) try: refactoring(**params) except Exception as e: print(e) # update understand database update_understand_database(udb_path) # calculate objective obj_score = DesignQualityAttributes(udb_path=udb_path).reusability print("Objective Score", obj_score) if obj_score < score: score = obj_score best_answer = individual
def _evaluate(self, x, out, *args, **kwargs): """ This method iterate over a population, execute the refactoring operations in each individual sequentially, and compute quality attributes for the refactored version of the program, as objectives of the search. By default, elementwise_evaluation is set to False, which implies the _evaluate retrieves a set of solutions. Args: x (Population): x is a matrix where each row is an individual, and each column a variable.\ We have one variable of type list (Individual) ==> x.shape = (len(Population), 1) """ objective_values = [] for k, individual_ in enumerate(x): # Stage 0: Git restore logger.debug("Executing git restore.") git_restore(config.PROJECT_PATH) logger.debug("Updating understand database after git restore.") update_understand_database(config.UDB_PATH) # Stage 1: Execute all refactoring operations in the sequence x logger.debug(f"Reached an Individual with size {len(individual_[0])}") for refactoring_operation in individual_[0]: res = refactoring_operation.do_refactoring() # Update Understand DB logger.debug(f"Updating understand database after {refactoring_operation.name}.") update_understand_database(config.UDB_PATH) # Stage 2: arr = Array('d', range(self.n_obj)) if self.evaluate_in_parallel: # Stage 2 (parallel mood): Computing quality attributes p1 = Process(target=calc_qmood_objectives, args=(arr,)) if self.n_obj == 8: p2 = Process(target=calc_testability_objective, args=(config.UDB_PATH, arr,)) p3 = Process(target=calc_modularity_objective, args=(config.UDB_PATH, arr,)) p1.start(), p2.start(), p3.start() p1.join(), p2.join(), p3.join() else: p1.start() p1.join() else: # Stage 2 (sequential mood): Computing quality attributes qmood_quality_attributes = DesignQualityAttributes(udb_path=config.UDB_PATH) arr[0] = qmood_quality_attributes.reusability arr[1] = qmood_quality_attributes.understandability arr[2] = qmood_quality_attributes.flexibility arr[3] = qmood_quality_attributes.functionality arr[4] = qmood_quality_attributes.effectiveness arr[5] = qmood_quality_attributes.extendability if self.n_obj == 8: arr[6] = testability_main(config.UDB_PATH, initial_value=config.CURRENT_METRICS.get("TEST", 1.0)) arr[7] = modularity_main(config.UDB_PATH, initial_value=config.CURRENT_METRICS.get("MODULE", 1.0)) if self.verbose_design_metrics: design_metrics = { "DSC": [qmood_quality_attributes.DSC], "NOH": [qmood_quality_attributes.NOH], "ANA": [qmood_quality_attributes.ANA], "MOA": [qmood_quality_attributes.MOA], "DAM": [qmood_quality_attributes.DAM], "CAMC": [qmood_quality_attributes.CAMC], "CIS": [qmood_quality_attributes.CIS], "NOM": [qmood_quality_attributes.NOM], "DCC": [qmood_quality_attributes.DCC], "MFA": [qmood_quality_attributes.MFA], "NOP": [qmood_quality_attributes.NOP] } self.log_design_metrics(design_metrics) del qmood_quality_attributes # Stage 3: Marshal objectives into vector objective_values.append([-1 * i for i in arr]) logger.info(f"Objective values for individual {k}: {[i for i in arr]}") # Stage 4: Marshal all objectives into out dictionary out['F'] = np.array(objective_values, dtype=float)