Example #1
0
 def script (self,script) -> None:
     self.__script= script
     ##call the function to open the configuration file, and store it in the map
     ##to generate the initial solutions
     self.sf = ScriptFeatures('', script, self.algo_name )
     self.sf.run_id =  self.__run_id ###this have to be called after setting the run_id in the client call
     self.sf.read_features_file()
Example #2
0
 def setUp(self):
     '''Delete any configuration file'''
     os.system('rm -f config.ini')
     self.sf = ScriptFeatures(
         os.path.dirname(os.path.abspath(__file__)) +
         "/..")  #relative path to unit test
     self.jseh = self.sf.js_engine_helper
Example #3
0
class JSEngineHelperTestCases(unittest.TestCase):
    def setUp(self):
        '''Delete any configuration file'''
        os.system('rm -f config.ini')
        self.sf = ScriptFeatures(
            os.path.dirname(os.path.abspath(__file__)) +
            "/..")  #relative path to unit test
        self.jseh = self.sf.js_engine_helper

    def test_should_constructor_create_a_non_null_object(self):
        self.assertIsNotNone(self.jseh)

    def test_repair_solution(self):
        #sf = ScriptFeatures("..")
        self.sf.read_features_file()
        asolution = self.sf.get_random_individual()
        repaired_solution = self.jseh.repair_solution(asolution)
        repaired = True

        if asolution[13] & asolution[10] == False:
            if asolution[10] == True:
                repaired = False
        if asolution[26] & asolution[25] == False:
            if asolution[25] == True:
                repaired = False

        if asolution[31] & asolution[33] == False:
            if asolution[33] == True:
                repaired = False

        if asolution[71] & asolution[72] == False:
            if asolution[71] != asolution[72]:
                repaired = False

        if asolution[6] & asolution[7]  & asolution[8]&\
                asolution[9]& asolution[10]& asolution[11] == False:
            for f in range(6, 12):
                if asolution[f] == True:
                    repaired = False
                    break

        self.assertTrue(repaired)

    def test_evaluate_solution_performance_create_a_non_null_object(self):
        '''create a minimal solution to test: f1, f5 to true
            DUK_USE_ALLOW_UNDEFINED_BEHAVIOR, DUK_USE_LIGHTFUNC_BUILTINS
             f2 has a numerical value DUK_USE_FATAL_MAXLEN'''
        self.sf.read_features_file()
        asolution = [
            True, False, False, True, True, False, False, False, False, True,
            True, False, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, False, True, True, True, True, False, False, False
        ]
        ppm = self.jseh.evaluate_solution_performance_(asolution)
        self.assertIsNotNone(ppm)
Example #4
0
    def __init__(self, fm_name, run_id, script):
        self.name = fm_name
        self.run_id = run_id
        self.script = script
        cwd = os.getcwd()
        os.chdir('../..')
        mpath = os.getcwd()
        self.sf = ScriptFeatures(mpath, self.script, "SWAY")
        os.chdir(cwd)
        self.sf.run_id = self.run_id  ###this have to be called after setting the run_id in the client call
        self.sf.read_features_file()
        '''init variables'''
        _, self.featureNum, self.cnfs, self.cnfNum = load_product_url(fm_name)

        # self.cost = []
        # self.used_before = []
        # self.defects = []
        #
        # filen = os.path.dirname(os.path.abspath(__file__)) + '/../Benchmarks/dimacs/' + fm_name + '.dimacs.augment'
        # lines = open(filen, 'r').read().split('\n')[1:]
        #
        # lines = map(lambda x: x.rstrip(), lines)
        # for l in lines:
        #     if not len(l): continue
        #     _, a, b, c = l.split(" ")
        #     self.cost.append(float(a))
        #     self.used_before.append(bool(int(b)))
        #     self.defects.append(int(c))

        creator.create("FitnessMin", base.Fitness,
                       weights=[-1.0] * 4)  #4 objectives
        creator.create("Individual", str, fitness=creator.FitnessMin)

        self.creator = creator
        self.Individual = creator.Individual

        self.toolbox = base.Toolbox()
        self.toolbox.register("evaluate", self.eval_ind)
        self.eval = self.toolbox.evaluate
Example #5
0
 def setUp(self):
     self.sf = ScriptFeatures("..")
Example #6
0
class ScriptFeaturesTestCases(unittest.TestCase):
    def setUp(self):
        self.sf = ScriptFeatures("..")

    def test_should_constructor_create_a_non_null_object(self):
        self.assertIsNotNone(self.sf)

    def test_read_features_file_succeded(self):
        self.sf.read_features_file()
        self.assertIsNotNone(self.sf.d)

    def test_get_random_individual_create_a_non_null_object(self):
        self.sf.read_features_file()
        individual = self.sf.get_random_individual()
        self.assertIsNotNone(individual)

    def test_get_random_individual_create_two_different__objects(self):
        self.sf.read_features_file()
        individual_A = self.sf.get_random_individual()
        individual_B = self.sf.get_random_individual()
        self.assertNotEqual(individual_A, individual_B)

    def test_return_base_individual(self):
        self.sf.read_features_file()
        bi = self.sf.get_base_individual()
        ''' This base individual was generated from confOpt.csv'''
        bioracle = [
            False, True, False, True, False, False, False, False, False, True,
            True, False, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, True, True, True, True, True, True, True, True, True,
            True, True, False, True, True, True, True, False, False, False
        ]
        self.assertEqual(bi, bioracle)
Example #7
0
class DimacsModel:
    def __init__(self, fm_name, run_id, script):
        self.name = fm_name
        self.run_id = run_id
        self.script = script
        cwd = os.getcwd()
        os.chdir('../..')
        mpath = os.getcwd()
        self.sf = ScriptFeatures(mpath, self.script, "SWAY")
        os.chdir(cwd)
        self.sf.run_id = self.run_id  ###this have to be called after setting the run_id in the client call
        self.sf.read_features_file()
        '''init variables'''
        _, self.featureNum, self.cnfs, self.cnfNum = load_product_url(fm_name)

        # self.cost = []
        # self.used_before = []
        # self.defects = []
        #
        # filen = os.path.dirname(os.path.abspath(__file__)) + '/../Benchmarks/dimacs/' + fm_name + '.dimacs.augment'
        # lines = open(filen, 'r').read().split('\n')[1:]
        #
        # lines = map(lambda x: x.rstrip(), lines)
        # for l in lines:
        #     if not len(l): continue
        #     _, a, b, c = l.split(" ")
        #     self.cost.append(float(a))
        #     self.used_before.append(bool(int(b)))
        #     self.defects.append(int(c))

        creator.create("FitnessMin", base.Fitness,
                       weights=[-1.0] * 4)  #4 objectives
        creator.create("Individual", str, fitness=creator.FitnessMin)

        self.creator = creator
        self.Individual = creator.Individual

        self.toolbox = base.Toolbox()
        self.toolbox.register("evaluate", self.eval_ind)
        self.eval = self.toolbox.evaluate

    def __compute_delta(self, org: float, measured: float) -> float:
        return (measured - org) / org

    def compute_dsr(self, ppm, val):
        device_memory = float(val[2])
        device_storage = float(val[3])
        return ((ppm.memory_us[0] - device_memory) / device_memory +
                (ppm.code_size[0] - device_storage) / device_storage) / 2

    def eval_ind(self, ind, objectives=4):
        """
        return the fitness, but it might be no needed.
        Args:
            ind:

        Returns:

        """
        '''Convert the individual to a list of booleans'''
        solint = [int(x, 2) for x in ind]
        sol = [bool(x) for x in solint]
        # validated_solution = self.sf.js_engine_helper.\
        #     keep_mandatory_features_on_solution(sol)
        ppm = self.sf.js_engine_helper.evaluate_solution_performance_(sol)
        if objectives == 4:
            try:
                if ppm.memory_us[0] != float('inf'):
                    median_execution_time = median(ppm.execution_time)
                    '''Compute DSR of each  device'''
                    usr_list = []
                    dsr = []
                    cval_max = -1.0
                    for val in self.sf.bc.devices:
                        device_value = float(val[6])
                        dsr.append((self.compute_dsr(ppm, val), device_value))
                        if device_value > cval_max:
                            cval_max = device_value
                    ''' Compute USR'''
                    for val in dsr:
                        usr_list.append(val[0] * (val[1] / cval_max))
                    '''We normalize the code with the original measurements '''
                    ind.fitness.values = (
                        self.__compute_delta \
                            (self.sf.bc.file_size_org, ppm.code_size[0]),  # this does not vary through executions
                        self.__compute_delta \
                            (self.sf.bc.mem_us_org, ppm.memory_us[0]),  # this does not vary through executions
                        self.__compute_delta \
                            (self.sf.bc.use_time_avg, median_execution_time),
                        mean(usr_list)
                    )
                else:
                    '''we penalized the solution since it broke the execution'''
                    ind.fitness.values = (float('inf'), float('inf'),
                                          float('inf'), float('inf'))
                    # solution.objectives[3] = float('inf')
                print(str(ind.fitness.values))
            except TypeError:
                '''we penalized the solution since it broke the execution'''
                ind.fitness.values = (float('inf'), float('inf'), float('inf'),
                                      float('inf'))
        else:
            try:
                if ppm.memory_us[0] != float('inf'):
                    median_execution_time = median(ppm.execution_time)
                    '''We normalize the code with the original measurements '''
                    ind.fitness.values = (
                        self.__compute_delta \
                            (self.sf.bc.file_size_org, ppm.code_size[0]),  # this does not vary through executions
                        self.__compute_delta \
                            (self.sf.bc.mem_us_org, ppm.memory_us[0]),  # this does not vary through executions
                        self.__compute_delta \
                            (self.sf.bc.use_time_avg, median_execution_time)
                    )
                else:
                    '''we penalized the solution since it broke the execution'''
                    ind.fitness.values = (float('inf'), float('inf'),
                                          float('inf'))
                print(str(ind.fitness.values))
            except TypeError:
                '''we penalized the solution since it broke the execution'''
                ind.fitness.values = (float('inf'), float('inf'), float('inf'))
            # solution.objectives[3] = float('inf')
        return ind.fitness.values
Example #8
0
class Miniaturization(BinaryProblem):
    """ Problem Miniaturization

    .. note:: Unconstrained problem. The default number of variables and objectives are
      and 3 objectives (code size, memory and size).
    """

    def __init__(self, number_of_variables: int = 1, number_of_objectives=4):
        """ :param number_of_variables: number of decision variables of the problem.
        """
        super(Miniaturization, self).__init__()
        self.number_of_variables = number_of_variables
        self.number_of_objectives = number_of_objectives
        self.number_of_constraints = 0

        self.obj_directions = [self.MINIMIZE] * number_of_objectives

        self.lower_bound = self.number_of_variables * [0.0]
        self.upper_bound = self.number_of_variables * [1.0]

        BinarySolution.lower_bound = self.lower_bound
        BinarySolution.upper_bound = self.upper_bound


        self.__run_id = None
        self.repair_solution = True

        '''to naming the different executions with an id'''
    @property
    def run_id(self) -> str:
        return self.__run_id
    @run_id.setter
    def run_id(self,run_id) -> None:
        self.__run_id = run_id
    '''to parametrize the name of the script to analyse to perform batch mode using bash script'''
    @property
    def script(self) -> str:
        return self.__script
    @property
    def algo_name(self) -> str:
        return self.__algo_name
    @algo_name.setter
    def algo_name (self, algo_name) -> None:
        self.__algo_name = algo_name
    @script.setter
    def script (self,script) -> None:
        self.__script= script
        ##call the function to open the configuration file, and store it in the map
        ##to generate the initial solutions
        self.sf = ScriptFeatures('', script, self.algo_name )
        self.sf.run_id =  self.__run_id ###this have to be called after setting the run_id in the client call
        self.sf.read_features_file()


    def evaluate(self, solution: BinarySolution) -> BinarySolution:
        ''' First apply the mandatory features '''
        ''' Note that in python the class is pass as reference so the value is modified automatically.  We use tmp variables for readability though.'''
        validated_solution = self.sf.js_engine_helper.keep_mandatory_features_on_solution(solution.variables[0])
        ''' Then repair the solution that could have been corrupted through the
            transformation operators'''
        if self.repair_solution==True:
            repaired_solution = self.sf.js_engine_helper.repair_solution(validated_solution)
            solution.variables[0] = repaired_solution
        else:
            solution.variables[0] = validated_solution
        ppm = self.sf.js_engine_helper.evaluate_solution_performance_(solution.variables[0])
        try:
            if ppm.memory_us[0]!=float('inf'):
                '''We normalize the code with the original measurements '''
                solution.objectives[0] = self.__compute_delta\
                    (self.sf.bc.file_size_org,ppm.code_size[0])#this does not vary through executions
                solution.objectives[1] = self.__compute_delta\
                    (self.sf.bc.mem_us_org,ppm.memory_us[0])  #this does not vary through executions
                median_execution_time = median(ppm.execution_time)
                solution.objectives[2] = self.__compute_delta\
                    (self.sf.bc.use_time_avg,median_execution_time)
                '''Compute DSR of each  device'''
                usr_list = []
                dsr = []
                cval_max = -1.0
                for val in self.sf.bc.devices:
                   device_value = float(val[6])
                   dsr.append ( (self.compute_dsr(ppm, val), device_value))
                   if device_value > cval_max:
                        cval_max = device_value
                ''' Compute USR'''
                for val in dsr:
                    usr_list.append( val[0] * ( val[1] /cval_max )  )
                solution.objectives[3] = mean ( usr_list )
            else:
                '''we penalized the solution since it broke the execution'''
                solution.objectives[0] = float('inf')
                solution.objectives[1] = float('inf')
                solution.objectives[2] = float('inf')
                solution.objectives[3] = float('inf')
            print (solution.objectives)
        except TypeError:
            '''we penalized the solution since it broke the execution'''
            solution.objectives[0] = float('inf')
            solution.objectives[1] = float('inf')
            solution.objectives[2] = float('inf')
            solution.objectives[3] = float('inf')
        return solution

    def compute_dsr(self, ppm, val):
        device_memory = float(val[2])
        device_storage = float(val[3])
        return ((ppm.memory_us[0] - device_memory) / device_memory + (ppm.code_size[0] - device_storage) / device_storage) / 2

    def get_name(self):
        return 'Miniaturization'

    def __compute_delta (self,org:float, measured: float) -> float:
        return (measured-org)/org

    def create_solution(self) -> BinarySolution:

        new_solution = BinarySolution(self.number_of_variables, self.number_of_objectives, self.number_of_constraints)
        new_solution.variables = \
            [self.sf.get_random_individual() for i in range(self.number_of_variables)]
        return new_solution

    def save_values_achieved(self,solution_list: list, file_name):
        '''to compare the results in disk space, memory usage and execution time'''
        with open(file_name, 'w') as of:
            for solution in solution_list:
                file_size = (solution.objectives[0]*self.sf.bc.file_size_org) +self.sf.bc.file_size_org
                usr_mem = (solution.objectives[1]*self.sf.bc.mem_us_org) +self.sf.bc.mem_us_org
                time_usr =(solution.objectives[2]*self.sf.bc.use_time_avg) +self.sf.bc.use_time_avg
                dsr = solution.objectives[3]
                of.write(str(file_size) + ",")
                of.write(str(usr_mem) + ",")
                of.write(str(time_usr) + ",")
                of.write(str(dsr))
                of.write("\n")