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)
def log_project_info(reset_=True, design_metrics_path=None, quality_attributes_path=None, generation=0, testability_verbose=True, testability_log_path=None): """ Logging project metrics and information """ if reset_: reset_project() if quality_attributes_path is None: quality_attributes_path = os.path.join(config.PROJECT_LOG_DIR, 'quality_attrs_initial_values.csv') if design_metrics_path is None: design_metrics_path = os.path.join(config.PROJECT_LOG_DIR, 'design_metrics.csv') design_quality_attribute = DesignQualityAttributes(config.UDB_PATH) avg_, sum_ = design_quality_attribute.average_sum predicted_testability = testability_main( config.UDB_PATH, initial_value=config.CURRENT_METRICS.get("TEST", 1.0), verbose=testability_verbose, log_path=testability_log_path ) mdg_modularity = modularity_main( config.UDB_PATH, initial_value=config.CURRENT_METRICS.get("MODULE", 1.0) ) design_metrics = { "DSC": [design_quality_attribute.DSC], "NOH": [design_quality_attribute.NOH], "ANA": [design_quality_attribute.ANA], "MOA": [design_quality_attribute.MOA], "DAM": [design_quality_attribute.DAM], "CAMC": [design_quality_attribute.CAMC], "CIS": [design_quality_attribute.CIS], "NOM": [design_quality_attribute.NOM], "DCC": [design_quality_attribute.DCC], "MFA": [design_quality_attribute.MFA], "NOP": [design_quality_attribute.NOP] } quality_objectives = { "generation": [generation], "reusability": [design_quality_attribute.reusability], "understandability": [design_quality_attribute.understandability], "flexibility": [design_quality_attribute.flexibility], "functionality": [design_quality_attribute.functionality], "effectiveness": [design_quality_attribute.effectiveness], "extendability": [design_quality_attribute.extendability], "testability": [predicted_testability], "modularity": [mdg_modularity], } logger.info('QMOOD design metrics (N):') logger.info(design_metrics) logger.info('Objectives:') logger.info(quality_objectives) logger.info('QMOOD quality attributes sum:') logger.info(sum_) logger.info('QMOOD quality attributes mean:') logger.info(avg_) df_quality_attributes = pd.DataFrame(data=quality_objectives) if os.path.exists(quality_attributes_path): df = pd.read_csv(quality_attributes_path, index_col=False) df_result = pd.concat([df, df_quality_attributes], ignore_index=True) df_result.to_csv(quality_attributes_path, index=False) else: df_quality_attributes.to_csv(quality_attributes_path, index=False) df_design_metrics = pd.DataFrame(data=design_metrics) if os.path.exists(design_metrics_path): df = pd.read_csv(design_metrics_path, index_col=False) df_results = pd.concat([df, df_design_metrics], ignore_index=True) # df = df.append(df_design_metrics, ignore_index=True) df_results.to_csv(design_metrics_path, index=False) else: df_design_metrics.to_csv(design_metrics_path, index=False)
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)
def calc_modularity_objective(path_, arr_): arr_[7] = modularity_main( path_, initial_value=config.CURRENT_METRICS.get("MODULE", 1.0) )