Пример #1
0
    def __init__(self, task_queue, result_queue, CurrentNotDomConstraints_queuelist, index, outputFileParentName, INPUT):
        multiprocessing.Process.__init__(self)
        self.task_queue = task_queue
        self.result_queue = result_queue
        self.CurrentNotDomConstraints_queuelist = CurrentNotDomConstraints_queuelist
        self.index = index
        self.outputFileParentName = outputFileParentName
        
        self.GIAOptions = GuidedImprovementAlgorithmOptions(verbosity=0, \
                        incrementallyWriteLog=False, \
                        writeTotalTimeFilename="timefile.csv", \
                        writeRandomSeedsFilename="randomseed.csv", useCallLogs=False)    

        self.GIAAlgorithm = GuidedImprovementAlgorithm(INPUT.s, INPUT.metrics_variables, \
                    INPUT.metrics_objective_direction, INPUT.FeatureVariable, options=self.GIAOptions)
        
        self.count_sat_calls = 0
        self.count_unsat_calls = 0
        self.count_paretoPoints = 0
        self.startTime = time.time()
Пример #2
0
    def __init__(self, task_queue, result_queue, totalTime,CurrentNotDomConstraints_queuelist, index, outputFileParentName, num_consumers, s, extraConstraint):
        
        multiprocessing.Process.__init__(self)
        s.add(extraConstraint)
        self.task_queue = task_queue
        self.result_queue = result_queue
        self.CurrentNotDomConstraints_queuelist = CurrentNotDomConstraints_queuelist
        self.totalTime = totalTime
        self.index = index
        self.outputFileParentName = outputFileParentName
        self.num_consumers = num_consumers
        
        # each group has an individual model and has two member consumers running on the model
        self.groupid = self.index / 2
        self.memberid= self.index % 2
        
       
        
        # split the objective space in terms of num_groups = num_consumers / 2
        # maximum 30 cores -> minimum 3 degrees, so we use range [degree, degree)
        num_groups = self.num_consumers / 2
        degree = 90.0 / num_groups
        # radian = degree * math.pi / 180.0
    
        
        self.GIAOptions = GuidedImprovementAlgorithmOptions(verbosity=0, \
                        incrementallyWriteLog=False, \
                        writeTotalTimeFilename="timefile.csv", \
                        writeRandomSeedsFilename="randomseed.csv", useCallLogs=False)    

        self.GIAAlgorithm = GuidedImprovementAlgorithm(s, metrics_variables, \
                    metrics_objective_direction, FeatureVariable, options=self.GIAOptions)
        
        self.count_sat_calls = 0
        self.count_unsat_calls = 0
        self.count_paretoPoints = 0
        self.startTime = time.time()
Пример #3
0
    def __init__(self, task_queue, result_queue, totalTime, index, outputFileParentName, num_consumers, s, extraConstraint):
        multiprocessing.Process.__init__(self)
        self.task_queue = task_queue
        self.result_queue = result_queue
#         self.CurrentNotDomConstraints_queuelist = CurrentNotDomConstraints_queuelist
        self.totalTime = totalTime 
        self.index = index
        self.outputFileParentName = outputFileParentName
        self.num_consumers = num_consumers
        self.GIAOptions = GuidedImprovementAlgorithmOptions(verbosity=0, \
                        incrementallyWriteLog=False, \
                        writeTotalTimeFilename="timefile.csv", \
                        writeRandomSeedsFilename="randomseed.csv", useCallLogs=False)    
        ''' add extra constraint'''
        #print extraConstraint
        s.add(extraConstraint)
        
        self.GIAAlgorithm = GuidedImprovementAlgorithm(s, metrics_variables, \
                    metrics_objective_direction, FeatureVariable, options=self.GIAOptions)
        
        self.count_sat_calls = 0
        self.count_unsat_calls = 0
        self.count_paretoPoints = 0
        self.startTime = time.time()
Пример #4
0
class Consumer(multiprocessing.Process):
    def __init__(self, task_queue, result_queue, CurrentNotDomConstraints_queuelist, index, outputFileParentName, INPUT):
        multiprocessing.Process.__init__(self)
        self.task_queue = task_queue
        self.result_queue = result_queue
        self.CurrentNotDomConstraints_queuelist = CurrentNotDomConstraints_queuelist
        self.index = index
        self.outputFileParentName = outputFileParentName
        
        self.GIAOptions = GuidedImprovementAlgorithmOptions(verbosity=0, \
                        incrementallyWriteLog=False, \
                        writeTotalTimeFilename="timefile.csv", \
                        writeRandomSeedsFilename="randomseed.csv", useCallLogs=False)    

        self.GIAAlgorithm = GuidedImprovementAlgorithm(INPUT.s, INPUT.metrics_variables, \
                    INPUT.metrics_objective_direction, INPUT.FeatureVariable, options=self.GIAOptions)
        
        self.count_sat_calls = 0
        self.count_unsat_calls = 0
        self.count_paretoPoints = 0
        self.startTime = time.time()

    def run(self):
        while True:
            if self.task_queue.empty() == True:
                #print self.result_queue.qsize()
                #print '%s : Exiting' % multiprocessing.current_process().pid
                #self.task_queue.task_done()
                #print multiprocessing.current_process().is_alive()
                #os.exit(0)
                #print multiprocessing.current_process().is_alive()
                break
            else:
                next_task = self.task_queue.get(False)
                if next_task is None:
                    # Poison pill means shutdown
                    #print self.result_queue.qsize()
                    #print '%s : Exiting' % multiprocessing.current_process().pid
    #                 while self.result_queue.empty() != True:
    #                     print self.result_queue.get()
                    #multiprocessing.current_process().terminate()
                    self.task_queue.task_done()
                    #print count_paretoPoints, count_sat_calls, count_sat_calls
                    outputFileChild = open(str(str(self.outputFileParentName)+'C'+str(self.index)+'.csv'), 'a')
                    try:
                        outputFileChild.writelines(str(self.index)+','+
                                                   str(self.count_paretoPoints) + ',' +
                                                   str(self.count_sat_calls) + ',' +
                                                   str(self.count_unsat_calls) + ',' +
                                                   str(time.time()-self.startTime) +',' +
                                                   '\n')
                    finally:
                        outputFileChild.close()
                    #print multiprocessing.current_process().is_alive()
                    #os.exit(0)
                    #print multiprocessing.current_process().is_alive()
    #                 multiprocessing.current_process().terminate()
    #                 print multiprocessing.current_process().is_alive()
                    break
                # execute a task, i.e., find a Pareto point
                # 1) update CurrentNotDomConstraints
                while self.CurrentNotDomConstraints_queuelist[self.index].empty() != True:
                    strconstraintlist = self.CurrentNotDomConstraints_queuelist[self.index].get()
                    ConvertedZ3ConstraintList  = list()
                    for constraint in strconstraintlist:
                        #print constraint
                        # eval() may lose precision, for example, eval(9962778141/19531250000) = 0
                        # that's why we need repr(), but not work for z3
                        # split constraint via > or <
                        constraintSplitList = []
                        if constraint.find('>') != -1:
                            constraintSplitList = constraint.split('>')                    
                            #print constraintSplitList
                            if constraintSplitList[1].find('/') != -1:
                                ConvertedZ3ConstraintList.append( Real(constraintSplitList[0].strip()) > RealVal(constraintSplitList[1].strip()))
                            else:
                                ConvertedZ3ConstraintList.append( Int(constraintSplitList[0].strip()) > IntVal(constraintSplitList[1].strip()))
                            #print ConvertedZ3ConstraintList
                        else:
                            constraintSplitList = constraint.split('<') 
                            #print constraintSplitList
                            if constraintSplitList[1].find('/') != -1:
                                ConvertedZ3ConstraintList.append( Real(constraintSplitList[0].strip()) < RealVal(constraintSplitList[1].strip()))
                            else:
                                ConvertedZ3ConstraintList.append( Int(constraintSplitList[0].strip()) < IntVal(constraintSplitList[1].strip()))
                            #print ConvertedZ3ConstraintList                   
                    #print Or(ConvertedZ3ConstraintList)
                    tmpNotDominatedByNextParetoPoint = Or(ConvertedZ3ConstraintList)
                    #print tmpNotDominatedByNextParetoPoint            
                    self.GIAAlgorithm.s.add(tmpNotDominatedByNextParetoPoint)
                
                # 2) if find all Pareto points, add a poison pill; otherwise find a Pareto point
                if self.GIAAlgorithm.s.check() != sat:
                    self.count_unsat_calls += 1
                    self.task_queue.put(None)
                else:
                    self.count_sat_calls += 1
                    self.task_queue.put("Task")      
                    prev_solution = self.GIAAlgorithm.s.model()
                    self.GIAAlgorithm.s.push()
                    NextParetoPoint, local_count_sat_calls, local_count_unsat_calls = self.GIAAlgorithm.ranToParetoFront(prev_solution)
                    self.count_sat_calls += local_count_sat_calls
                    self.count_unsat_calls += local_count_unsat_calls
                    self.count_paretoPoints += 1
                    #                 #for EShop
#                   outputFile = open(outfilename, 'a')
#                   try:
#                       outputFile.writelines('Found Pareto Points ' + str(count_paretoPoints) + ',' +
#                                         '\n')
#                   finally:
#                       outputFile.close()
                    self.GIAAlgorithm.s.pop()
                    tmpNotDominatedByNextParetoPoint = self.GIAAlgorithm.ConstraintNotDominatedByX(NextParetoPoint)
                    self.GIAAlgorithm.s.add(tmpNotDominatedByNextParetoPoint)
                    
                    # picklize and store Pareto point and constraints
                    strNextParetoPoint = list((d.name(), str(NextParetoPoint[d])) for d in NextParetoPoint.decls())
                    self.result_queue.put(strNextParetoPoint)
                    
                    constraintlist = self.GIAAlgorithm.EtractConstraintListNotDominatedByX(NextParetoPoint)
                    strconstraintlist = list(str(item) for item in constraintlist)
                    for j in xrange(len(self.CurrentNotDomConstraints_queuelist)):
                        if j != self.index:
                            self.CurrentNotDomConstraints_queuelist[j].put(strconstraintlist)
                    self.task_queue.task_done()
        return 0
Пример #5
0
class Consumer(multiprocessing.Process):
    def __init__(self, task_queue, result_queue, totalTime,CurrentNotDomConstraints_queuelist, index, outputFileParentName, num_consumers, s, extraConstraint):
        
        multiprocessing.Process.__init__(self)
        s.add(extraConstraint)
        self.task_queue = task_queue
        self.result_queue = result_queue
        self.CurrentNotDomConstraints_queuelist = CurrentNotDomConstraints_queuelist
        self.totalTime = totalTime
        self.index = index
        self.outputFileParentName = outputFileParentName
        self.num_consumers = num_consumers
        
        # each group has an individual model and has two member consumers running on the model
        self.groupid = self.index / 2
        self.memberid= self.index % 2
        
       
        
        # split the objective space in terms of num_groups = num_consumers / 2
        # maximum 30 cores -> minimum 3 degrees, so we use range [degree, degree)
        num_groups = self.num_consumers / 2
        degree = 90.0 / num_groups
        # radian = degree * math.pi / 180.0
    
        
        self.GIAOptions = GuidedImprovementAlgorithmOptions(verbosity=0, \
                        incrementallyWriteLog=False, \
                        writeTotalTimeFilename="timefile.csv", \
                        writeRandomSeedsFilename="randomseed.csv", useCallLogs=False)    

        self.GIAAlgorithm = GuidedImprovementAlgorithm(s, metrics_variables, \
                    metrics_objective_direction, FeatureVariable, options=self.GIAOptions)
        
        self.count_sat_calls = 0
        self.count_unsat_calls = 0
        self.count_paretoPoints = 0
        self.startTime = time.time()

    def run(self):
        while True:
            if self.task_queue[self.groupid].empty() == True:
                break
            else:
                next_task = self.task_queue[self.groupid].get(False)
                if next_task is None:
                    self.task_queue[self.groupid].task_done()
                    self.totalTime.put(str(time.time()-self.startTime))
                    outputFileChild = open(str(str(self.outputFileParentName)+'C'+str(self.index)+'.csv'), 'a')
                    try:
                        outputFileChild.writelines(str(self.index)+','+
                                                   str(self.count_paretoPoints) + ',' +
                                                   str(self.count_sat_calls) + ',' +
                                                   str(self.count_unsat_calls) + ',' +
                                                   str(time.time()-self.startTime) +',' +
                                                   '\n')
                    finally:
                        outputFileChild.close()
                    break
                
                # execute a task, i.e., find a Pareto point
                # 1) update CurrentNotDomConstraints
                while self.CurrentNotDomConstraints_queuelist[self.index].empty() != True:
                    strconstraintlist = self.CurrentNotDomConstraints_queuelist[self.index].get()
                    ConvertedZ3ConstraintList  = list()
                    for constraint in strconstraintlist:
                        constraintSplitList = []
                        if constraint.find('>') != -1:
                            constraintSplitList = constraint.split('>')                    
                            #print constraintSplitList
                            if constraintSplitList[1].find('/') != -1:
                                ConvertedZ3ConstraintList.append( Real(constraintSplitList[0].strip()) > RealVal(constraintSplitList[1].strip()))
                            else:
                                ConvertedZ3ConstraintList.append( Int(constraintSplitList[0].strip()) > IntVal(constraintSplitList[1].strip()))
                            #print ConvertedZ3ConstraintList
                        else:
                            constraintSplitList = constraint.split('<') 
                            #print constraintSplitList
                            if constraintSplitList[1].find('/') != -1:
                                ConvertedZ3ConstraintList.append( Real(constraintSplitList[0].strip()) < RealVal(constraintSplitList[1].strip()))
                            else:
                                ConvertedZ3ConstraintList.append( Int(constraintSplitList[0].strip()) < IntVal(constraintSplitList[1].strip()))
                            #print ConvertedZ3ConstraintList                   
                    #print Or(ConvertedZ3ConstraintList)
                    tmpNotDominatedByNextParetoPoint = Or(ConvertedZ3ConstraintList)
                    #print tmpNotDominatedByNextParetoPoint            
                    self.GIAAlgorithm.s.add(tmpNotDominatedByNextParetoPoint)
                
                # 2) if find all Pareto points, add a poison pill; otherwise find a Pareto point
                
                start_time = time.time()
                
                if self.GIAAlgorithm.s.check() != sat:
                    self.count_unsat_calls += 1
                    self.task_queue[self.groupid].put(None)
                else:
                    self.count_sat_calls += 1
                    self.task_queue[self.groupid].put("Task")      
                    prev_solution = self.GIAAlgorithm.s.model()
                    self.GIAAlgorithm.s.push()
                    NextParetoPoint, local_count_sat_calls, local_count_unsat_calls = self.GIAAlgorithm.ranToParetoFront(prev_solution)
                    end_time = time.time()
                    self.count_sat_calls += local_count_sat_calls
                    self.count_unsat_calls += local_count_unsat_calls
                    self.count_paretoPoints += 1

                    # RecordPoint
                    strNextParetoPoint = list((d.name(), str(NextParetoPoint[d])) for d in NextParetoPoint.decls())
                    if RECORDPOINT:
                        strNextParetoPoint = list((d.name(), str(NextParetoPoint[d])) for d in NextParetoPoint.decls())
                        outputFileChild = open(str(str(self.outputFileParentName)+'C'+str(self.index)+'.csv'), 'a')
                        try:
                            outputFileChild.writelines(str(self.index)+','+
                                                       str(self.count_paretoPoints) + ',' +
                                                       str(self.count_sat_calls) + ',' +
                                                       str(end_time-start_time) +',' +
                                                       str(strNextParetoPoint) +',' +
                                                       '\n')
                        finally:
                            outputFileChild.close()
                        
                    self.GIAAlgorithm.s.pop()
                    tmpNotDominatedByNextParetoPoint = self.GIAAlgorithm.ConstraintNotDominatedByX(NextParetoPoint)
                    self.GIAAlgorithm.s.add(tmpNotDominatedByNextParetoPoint)
                    
                    # picklize and store Pareto point and constraints
                    self.result_queue.put(strNextParetoPoint)
                    
                    constraintlist = self.GIAAlgorithm.EtractConstraintListNotDominatedByX(NextParetoPoint)
                    strconstraintlist = list(str(item) for item in constraintlist)
                    # broadcast the constraints to the other queue in the same group
                    brother_index = self.groupid * 2 + (1-self.memberid)
                    self.CurrentNotDomConstraints_queuelist[brother_index].put(strconstraintlist)
                    self.task_queue[self.groupid].task_done()
        return 0
Пример #6
0
class Consumer(multiprocessing.Process):
    def __init__(self, task_queue, result_queue, totalTime, index, outputFileParentName, num_consumers, s, extraConstraint):
        multiprocessing.Process.__init__(self)
        self.task_queue = task_queue
        self.result_queue = result_queue
#         self.CurrentNotDomConstraints_queuelist = CurrentNotDomConstraints_queuelist
        self.totalTime = totalTime 
        self.index = index
        self.outputFileParentName = outputFileParentName
        self.num_consumers = num_consumers
        self.GIAOptions = GuidedImprovementAlgorithmOptions(verbosity=0, \
                        incrementallyWriteLog=False, \
                        writeTotalTimeFilename="timefile.csv", \
                        writeRandomSeedsFilename="randomseed.csv", useCallLogs=False)    
        ''' add extra constraint'''
        #print extraConstraint
        s.add(extraConstraint)
        
        self.GIAAlgorithm = GuidedImprovementAlgorithm(s, metrics_variables, \
                    metrics_objective_direction, FeatureVariable, options=self.GIAOptions)
        
        self.count_sat_calls = 0
        self.count_unsat_calls = 0
        self.count_paretoPoints = 0
        self.startTime = time.time()

    def run(self):
        while True:
            if self.task_queue[self.index].empty() == True:
                break
            else:
                next_task = self.task_queue[self.index].get(False)
                if next_task is None:
                    self.task_queue[self.index].task_done()
                    self.totalTime.put(str(time.time()-self.startTime))
                    outputFileChild = open(str(str(self.outputFileParentName)+'C'+str(self.index)+'.csv'), 'a')
                    try:
                        outputFileChild.writelines(str(self.index)+','+
                                                   str(self.count_paretoPoints) + ',' +
                                                   str(self.count_sat_calls) + ',' +
                                                   str(self.count_unsat_calls) + ',' +
                                                   str(time.time()-self.startTime) +',' +
                                                   '\n')
                    finally:
                        outputFileChild.close()
                    
                    
                    break
                
                start_time = time.time()
            
                if self.GIAAlgorithm.s.check() != sat:
                    self.count_unsat_calls += 1
                    self.task_queue[self.index].put(None)
                else:
                    self.count_sat_calls += 1
                    self.task_queue[self.index].put("Task")      
                    prev_solution = self.GIAAlgorithm.s.model()
                    self.GIAAlgorithm.s.push()
                    NextParetoPoint, local_count_sat_calls, local_count_unsat_calls = self.GIAAlgorithm.ranToParetoFront(prev_solution)
                    end_time = time.time()
                    self.count_sat_calls += local_count_sat_calls
                    self.count_unsat_calls += local_count_unsat_calls
                    self.count_paretoPoints += 1
                 
                    self.GIAAlgorithm.s.pop()
                    tmpNotDominatedByNextParetoPoint = self.GIAAlgorithm.ConstraintNotDominatedByX(NextParetoPoint)
                    self.GIAAlgorithm.s.add(tmpNotDominatedByNextParetoPoint)
                    
                    # picklize and store Pareto point and constraints
                    strNextParetoPoint = list((d.name(), str(NextParetoPoint[d])) for d in NextParetoPoint.decls())
                    self.result_queue.put(strNextParetoPoint)
                    
                    # RecordPoint
                    if RECORDPOINT:
                        strNextParetoPoint = list((d.name(), str(NextParetoPoint[d])) for d in NextParetoPoint.decls())
                        outputFileChild = open(str(str(self.outputFileParentName)+'C'+str(self.index)+'.points'), 'a')
                        try:
                            outputFileChild.writelines(str(self.index)+','+
                                                       str(self.count_paretoPoints) + ',' +
                                                       str(self.count_sat_calls) + ',' +
                                                       str(end_time-start_time) +',' +
                                                       str(strNextParetoPoint) +',' +
                                                       '\n')
                        finally:
                            outputFileChild.close()
                    

                    self.task_queue[self.index].task_done()
        return 0
Пример #7
0
    def __init__(self, task_queue, result_queue, totalTime, CurrentNotDomConstraints_queuelist, index, outputFileParentName, num_consumers, solver, INPUT):
        multiprocessing.Process.__init__(self)
        self.task_queue = task_queue
        self.result_queue = result_queue
        self.CurrentNotDomConstraints_queuelist = CurrentNotDomConstraints_queuelist
        self.totalTime = totalTime
        self.index = index
        self.outputFileParentName = outputFileParentName
        self.num_consumers = num_consumers
        s = solver
        
        # each group has an individual model and has two member consumers running on the model
        self.groupid = self.index / 2
        self.memberid= self.index % 2
        
        # split the objective space in terms of num_groups = num_consumers / 2
        # maximum 30 cores -> minimum 3 degrees, so we use range [degree, degree)
        num_groups = self.num_consumers / 2
        degree = 90.0 / num_groups
        # radian = degree * math.pi / 180.0
        splitRuleList = []
        if sys.argv[1] == "ERS":
            if (self.groupid == 0):
                # from the reference point with a larger angle -> a bigger range
    #             radian_higher = (degree + 1) * math.pi / 180.0
                radian_higher = (degree) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             print str(self.groupid) + ">=" + str(gradient_higher)
                # squarization
                # choosing "the two best" dimensions of the projective plane could be an interesting problem
                # try to use Shannon Diversity Index, but seems not working; i think it only works when we normalize all values into [0, 1]
                # so, still use the two dimensions with the maximum value range
                # the challenge is how to know the scattering of configurations in the objective space, given the quality attributes of each feature? 
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 >= IntVal(gradient_higher) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_responsetime - 2070) * 629 >= IntVal(gradient_higher) * (INPUT.total_cost - 3145) * 414 )
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)
            elif (self.groupid == num_groups - 1):
                # from the reference point with a smaller angle -> a bigger range
    #             radian_lower = (degree * self.groupid - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             print str(self.groupid) + "<" + str(gradient_lower)
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 < IntVal(gradient_lower) * (total_batteryusage - 121) * 130 ) 
                splitRuleList.append( 1000 * (INPUT.total_responsetime - 2070) * 629 < IntVal(gradient_lower) * (INPUT.total_cost - 3145) * 414 )
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)   
            else:
    #             radian_lower = (degree * self.groupid - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 < IntVal(gradient_lower) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_responsetime - 2070) * 629 < IntVal(gradient_lower) * (INPUT.total_cost - 3145) * 414 )
    #             radian_higher = (degree * (self.groupid+1) + 1) * math.pi / 180.0
                radian_higher = (degree * (self.groupid + 1)) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 >= IntVal(gradient_higher) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_responsetime - 2070) * 629 >= IntVal(gradient_higher) * (INPUT.total_cost - 3145) * 414 )  
    #             print str(self.groupid) + ">=" + str(gradient_higher) + "<" + str(gradient_lower)          
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)   
        elif sys.argv[1] == "ESH":
            if (self.groupid == 0):
                # from the reference point with a larger angle -> a bigger range
    #             radian_higher = (degree + 1) * math.pi / 180.0
                radian_higher = (degree) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             print str(self.groupid) + ">=" + str(gradient_higher)
                # squarization
                # choosing "the two best" dimensions of the projective plane could be an interesting problem
                # try to use Shannon Diversity Index, but seems not working; i think it only works when we normalize all values into [0, 1]
                # so, still use the two dimensions with the maximum value range
                # the challenge is how to know the scattering of configurations in the objective space, given the quality attributes of each feature? 
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 >= IntVal(gradient_higher) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 2887) * 708 >= IntVal(gradient_higher) * (INPUT.total_Defects - 708) * 2887 )
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)
            elif (self.groupid == num_groups - 1):
                # from the reference point with a smaller angle -> a bigger range
    #             radian_lower = (degree * self.groupid - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             print str(self.groupid) + "<" + str(gradient_lower)
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 < IntVal(gradient_lower) * (total_batteryusage - 121) * 130 ) 
                splitRuleList.append( 1000 * (INPUT.total_Cost - 2887) * 708 < IntVal(gradient_lower) * (INPUT.total_Defects - 708) * 2887 )
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)   
            else:
    #             radian_lower = (degree * self.groupid - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 < IntVal(gradient_lower) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 2887) * 708 < IntVal(gradient_lower) * (INPUT.total_Defects - 708) * 2887 )
    #             radian_higher = (degree * (self.groupid+1) + 1) * math.pi / 180.0
                radian_higher = (degree * (self.groupid + 1)) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 >= IntVal(gradient_higher) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 2887) * 708 >= IntVal(gradient_higher) * (INPUT.total_Defects - 708) * 2887 )  
    #             print str(self.groupid) + ">=" + str(gradient_higher) + "<" + str(gradient_lower)          
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)   
        elif sys.argv[1] == "WPT":
            if (self.groupid == 0):
                # from the reference point with a larger angle -> a bigger range
    #             radian_higher = (degree + 1) * math.pi / 180.0
                radian_higher = (degree) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             print str(self.groupid) + ">=" + str(gradient_higher)
                # squarization
                # choosing "the two best" dimensions of the projective plane could be an interesting problem
                # try to use Shannon Diversity Index, but seems not working; i think it only works when we normalize all values into [0, 1]
                # so, still use the two dimensions with the maximum value range
                # the challenge is how to know the scattering of configurations in the objective space, given the quality attributes of each feature? 
    #             splitRuleList.append( 1000 * (total_rampuptime - 13) * 10 >= IntVal(gradient_higher) * (total_batteryusage - 10) * 13 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 422) * 145 >= IntVal(gradient_higher) * (INPUT.total_Defects - 145) * 422 )
                tmpsplitRuleList = And(splitRuleList)
                s.add(tmpsplitRuleList)
            elif (self.groupid == num_groups - 1):
                # from the reference point with a smaller angle -> a bigger range
    #             radian_lower = (degree * self.index - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             print str(self.groupid) + "<" + str(gradient_lower)
    #             splitRuleList.append( 1000 * (total_rampuptime - 13) * 10 < IntVal(gradient_lower) * (total_batteryusage - 10) * 13 ) 
                splitRuleList.append( 1000 * (INPUT.total_Cost - 422) * 145 < IntVal(gradient_lower) * (INPUT.total_Defects - 145) * 422 )
                tmpsplitRuleList = And(splitRuleList)
                s.add(tmpsplitRuleList)   
            else:
    #             radian_lower = (degree * self.index - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 13) * 10 < IntVal(gradient_lower) * (total_batteryusage - 10) * 13 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 422) * 145 < IntVal(gradient_lower) * (INPUT.total_Defects - 145) * 422 )
    #             radian_higher = (degree * (self.index+1) + 1) * math.pi / 180.0
                radian_higher = (degree * (self.groupid+1)) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 13) * 10 >= IntVal(gradient_higher) * (total_batteryusage - 10) * 13 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 422) * 145 >= IntVal(gradient_higher) * (INPUT.total_Defects - 145) * 422 )  
    #             print str(self.groupid) + ">=" + str(gradient_higher) + "<" + str(gradient_lower)          
                tmpsplitRuleList = And(splitRuleList)
                s.add(tmpsplitRuleList)   
        elif sys.argv[1] == "BDB":
            pass
        else:
            print "messed up"
            sys.exit()
        
        self.GIAOptions = GuidedImprovementAlgorithmOptions(verbosity=0, \
                        incrementallyWriteLog=False, \
                        writeTotalTimeFilename="timefile.csv", \
                        writeRandomSeedsFilename="randomseed.csv", useCallLogs=False)    

        self.GIAAlgorithm = GuidedImprovementAlgorithm(s, INPUT.metrics_variables, \
                    INPUT.metrics_objective_direction, INPUT.FeatureVariable, options=self.GIAOptions)
        
        self.count_sat_calls = 0
        self.count_unsat_calls = 0
        self.count_paretoPoints = 0
        self.startTime = time.time()
Пример #8
0
class Consumer(multiprocessing.Process):
    def __init__(self, task_queue, result_queue, totalTime, CurrentNotDomConstraints_queuelist, index, outputFileParentName, num_consumers, solver, INPUT):
        multiprocessing.Process.__init__(self)
        self.task_queue = task_queue
        self.result_queue = result_queue
        self.CurrentNotDomConstraints_queuelist = CurrentNotDomConstraints_queuelist
        self.totalTime = totalTime
        self.index = index
        self.outputFileParentName = outputFileParentName
        self.num_consumers = num_consumers
        s = solver
        
        # each group has an individual model and has two member consumers running on the model
        self.groupid = self.index / 2
        self.memberid= self.index % 2
        
        # split the objective space in terms of num_groups = num_consumers / 2
        # maximum 30 cores -> minimum 3 degrees, so we use range [degree, degree)
        num_groups = self.num_consumers / 2
        degree = 90.0 / num_groups
        # radian = degree * math.pi / 180.0
        splitRuleList = []
        if sys.argv[1] == "ERS":
            if (self.groupid == 0):
                # from the reference point with a larger angle -> a bigger range
    #             radian_higher = (degree + 1) * math.pi / 180.0
                radian_higher = (degree) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             print str(self.groupid) + ">=" + str(gradient_higher)
                # squarization
                # choosing "the two best" dimensions of the projective plane could be an interesting problem
                # try to use Shannon Diversity Index, but seems not working; i think it only works when we normalize all values into [0, 1]
                # so, still use the two dimensions with the maximum value range
                # the challenge is how to know the scattering of configurations in the objective space, given the quality attributes of each feature? 
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 >= IntVal(gradient_higher) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_responsetime - 2070) * 629 >= IntVal(gradient_higher) * (INPUT.total_cost - 3145) * 414 )
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)
            elif (self.groupid == num_groups - 1):
                # from the reference point with a smaller angle -> a bigger range
    #             radian_lower = (degree * self.groupid - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             print str(self.groupid) + "<" + str(gradient_lower)
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 < IntVal(gradient_lower) * (total_batteryusage - 121) * 130 ) 
                splitRuleList.append( 1000 * (INPUT.total_responsetime - 2070) * 629 < IntVal(gradient_lower) * (INPUT.total_cost - 3145) * 414 )
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)   
            else:
    #             radian_lower = (degree * self.groupid - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 < IntVal(gradient_lower) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_responsetime - 2070) * 629 < IntVal(gradient_lower) * (INPUT.total_cost - 3145) * 414 )
    #             radian_higher = (degree * (self.groupid+1) + 1) * math.pi / 180.0
                radian_higher = (degree * (self.groupid + 1)) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 >= IntVal(gradient_higher) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_responsetime - 2070) * 629 >= IntVal(gradient_higher) * (INPUT.total_cost - 3145) * 414 )  
    #             print str(self.groupid) + ">=" + str(gradient_higher) + "<" + str(gradient_lower)          
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)   
        elif sys.argv[1] == "ESH":
            if (self.groupid == 0):
                # from the reference point with a larger angle -> a bigger range
    #             radian_higher = (degree + 1) * math.pi / 180.0
                radian_higher = (degree) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             print str(self.groupid) + ">=" + str(gradient_higher)
                # squarization
                # choosing "the two best" dimensions of the projective plane could be an interesting problem
                # try to use Shannon Diversity Index, but seems not working; i think it only works when we normalize all values into [0, 1]
                # so, still use the two dimensions with the maximum value range
                # the challenge is how to know the scattering of configurations in the objective space, given the quality attributes of each feature? 
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 >= IntVal(gradient_higher) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 2887) * 708 >= IntVal(gradient_higher) * (INPUT.total_Defects - 708) * 2887 )
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)
            elif (self.groupid == num_groups - 1):
                # from the reference point with a smaller angle -> a bigger range
    #             radian_lower = (degree * self.groupid - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             print str(self.groupid) + "<" + str(gradient_lower)
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 < IntVal(gradient_lower) * (total_batteryusage - 121) * 130 ) 
                splitRuleList.append( 1000 * (INPUT.total_Cost - 2887) * 708 < IntVal(gradient_lower) * (INPUT.total_Defects - 708) * 2887 )
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)   
            else:
    #             radian_lower = (degree * self.groupid - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 < IntVal(gradient_lower) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 2887) * 708 < IntVal(gradient_lower) * (INPUT.total_Defects - 708) * 2887 )
    #             radian_higher = (degree * (self.groupid+1) + 1) * math.pi / 180.0
                radian_higher = (degree * (self.groupid + 1)) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 130) * 121 >= IntVal(gradient_higher) * (total_batteryusage - 121) * 130 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 2887) * 708 >= IntVal(gradient_higher) * (INPUT.total_Defects - 708) * 2887 )  
    #             print str(self.groupid) + ">=" + str(gradient_higher) + "<" + str(gradient_lower)          
                tmpsplitRuleList = And(splitRuleList)
    #             print tmpsplitRuleList
                s.add(tmpsplitRuleList)   
        elif sys.argv[1] == "WPT":
            if (self.groupid == 0):
                # from the reference point with a larger angle -> a bigger range
    #             radian_higher = (degree + 1) * math.pi / 180.0
                radian_higher = (degree) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             print str(self.groupid) + ">=" + str(gradient_higher)
                # squarization
                # choosing "the two best" dimensions of the projective plane could be an interesting problem
                # try to use Shannon Diversity Index, but seems not working; i think it only works when we normalize all values into [0, 1]
                # so, still use the two dimensions with the maximum value range
                # the challenge is how to know the scattering of configurations in the objective space, given the quality attributes of each feature? 
    #             splitRuleList.append( 1000 * (total_rampuptime - 13) * 10 >= IntVal(gradient_higher) * (total_batteryusage - 10) * 13 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 422) * 145 >= IntVal(gradient_higher) * (INPUT.total_Defects - 145) * 422 )
                tmpsplitRuleList = And(splitRuleList)
                s.add(tmpsplitRuleList)
            elif (self.groupid == num_groups - 1):
                # from the reference point with a smaller angle -> a bigger range
    #             radian_lower = (degree * self.index - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             print str(self.groupid) + "<" + str(gradient_lower)
    #             splitRuleList.append( 1000 * (total_rampuptime - 13) * 10 < IntVal(gradient_lower) * (total_batteryusage - 10) * 13 ) 
                splitRuleList.append( 1000 * (INPUT.total_Cost - 422) * 145 < IntVal(gradient_lower) * (INPUT.total_Defects - 145) * 422 )
                tmpsplitRuleList = And(splitRuleList)
                s.add(tmpsplitRuleList)   
            else:
    #             radian_lower = (degree * self.index - 1) * math.pi / 180.0
                radian_lower = (degree * self.groupid) * math.pi / 180.0
                gradient_lower = int(1000*round(math.tan(radian_lower), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 13) * 10 < IntVal(gradient_lower) * (total_batteryusage - 10) * 13 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 422) * 145 < IntVal(gradient_lower) * (INPUT.total_Defects - 145) * 422 )
    #             radian_higher = (degree * (self.index+1) + 1) * math.pi / 180.0
                radian_higher = (degree * (self.groupid+1)) * math.pi / 180.0
                gradient_higher = int(1000*round(math.tan(radian_higher), 3))
    #             splitRuleList.append( 1000 * (total_rampuptime - 13) * 10 >= IntVal(gradient_higher) * (total_batteryusage - 10) * 13 )
                splitRuleList.append( 1000 * (INPUT.total_Cost - 422) * 145 >= IntVal(gradient_higher) * (INPUT.total_Defects - 145) * 422 )  
    #             print str(self.groupid) + ">=" + str(gradient_higher) + "<" + str(gradient_lower)          
                tmpsplitRuleList = And(splitRuleList)
                s.add(tmpsplitRuleList)   
        elif sys.argv[1] == "BDB":
            pass
        else:
            print "messed up"
            sys.exit()
        
        self.GIAOptions = GuidedImprovementAlgorithmOptions(verbosity=0, \
                        incrementallyWriteLog=False, \
                        writeTotalTimeFilename="timefile.csv", \
                        writeRandomSeedsFilename="randomseed.csv", useCallLogs=False)    

        self.GIAAlgorithm = GuidedImprovementAlgorithm(s, INPUT.metrics_variables, \
                    INPUT.metrics_objective_direction, INPUT.FeatureVariable, options=self.GIAOptions)
        
        self.count_sat_calls = 0
        self.count_unsat_calls = 0
        self.count_paretoPoints = 0
        self.startTime = time.time()

    def run(self):
        while True:
            if self.task_queue[self.groupid].empty() == True:
                break
            else:
                next_task = self.task_queue[self.groupid].get(False)
                if next_task is None:
                    self.task_queue[self.groupid].task_done()
                    self.totalTime.put(str(time.time()-self.startTime))
                    outputFileChild = open(str(str(self.outputFileParentName)+'C'+str(self.index)+'.csv'), 'a')
                    try:
                        outputFileChild.writelines(str(self.index)+','+
                                                   str(self.count_paretoPoints) + ',' +
                                                   str(self.count_sat_calls) + ',' +
                                                   str(self.count_unsat_calls) + ',' +
                                                   str(time.time()-self.startTime) +',' +
                                                   '\n')
                    finally:
                        outputFileChild.close()
                    break
                
                # execute a task, i.e., find a Pareto point
                # 1) update CurrentNotDomConstraints
                while self.CurrentNotDomConstraints_queuelist[self.index].empty() != True:
                    strconstraintlist = self.CurrentNotDomConstraints_queuelist[self.index].get()
                    ConvertedZ3ConstraintList  = list()
                    for constraint in strconstraintlist:
                        constraintSplitList = []
                        if constraint.find('>') != -1:
                            constraintSplitList = constraint.split('>')                    
                            #print constraintSplitList
                            if constraintSplitList[1].find('/') != -1:
                                ConvertedZ3ConstraintList.append( Real(constraintSplitList[0].strip()) > RealVal(constraintSplitList[1].strip()))
                            else:
                                ConvertedZ3ConstraintList.append( Int(constraintSplitList[0].strip()) > IntVal(constraintSplitList[1].strip()))
                            #print ConvertedZ3ConstraintList
                        else:
                            constraintSplitList = constraint.split('<') 
                            #print constraintSplitList
                            if constraintSplitList[1].find('/') != -1:
                                ConvertedZ3ConstraintList.append( Real(constraintSplitList[0].strip()) < RealVal(constraintSplitList[1].strip()))
                            else:
                                ConvertedZ3ConstraintList.append( Int(constraintSplitList[0].strip()) < IntVal(constraintSplitList[1].strip()))
                            #print ConvertedZ3ConstraintList                   
                    #print Or(ConvertedZ3ConstraintList)
                    tmpNotDominatedByNextParetoPoint = Or(ConvertedZ3ConstraintList)
                    #print tmpNotDominatedByNextParetoPoint            
                    self.GIAAlgorithm.s.add(tmpNotDominatedByNextParetoPoint)
                
                # 2) if find all Pareto points, add a poison pill; otherwise find a Pareto point
                
                if self.GIAAlgorithm.s.check() != sat:
                    self.count_unsat_calls += 1
                    self.task_queue[self.groupid].put(None)
                else:
                    self.count_sat_calls += 1
                    self.task_queue[self.groupid].put("Task")      
                    prev_solution = self.GIAAlgorithm.s.model()
                    self.GIAAlgorithm.s.push()
                    NextParetoPoint, local_count_sat_calls, local_count_unsat_calls = self.GIAAlgorithm.ranToParetoFront(prev_solution)
                    self.count_sat_calls += local_count_sat_calls
                    self.count_unsat_calls += local_count_unsat_calls
                    self.count_paretoPoints += 1
                    #                 #for EShop
#                   outputFile = open(outfilename, 'a')
#                   try:
#                       outputFile.writelines('Found Pareto Points ' + str(count_paretoPoints) + ',' +
#                                         '\n')
#                   finally:
#                       outputFile.close()
                    self.GIAAlgorithm.s.pop()
                    tmpNotDominatedByNextParetoPoint = self.GIAAlgorithm.ConstraintNotDominatedByX(NextParetoPoint)
                    self.GIAAlgorithm.s.add(tmpNotDominatedByNextParetoPoint)
                    
                    # picklize and store Pareto point and constraints
                    strNextParetoPoint = list((d.name(), str(NextParetoPoint[d])) for d in NextParetoPoint.decls())
                    self.result_queue.put(strNextParetoPoint)
                    
                    constraintlist = self.GIAAlgorithm.EtractConstraintListNotDominatedByX(NextParetoPoint)
                    strconstraintlist = list(str(item) for item in constraintlist)
                    # broadcast the constraints to the other queue in the same group
                    brother_index = self.groupid * 2 + (1-self.memberid)
                    self.CurrentNotDomConstraints_queuelist[brother_index].put(strconstraintlist)
                    self.task_queue[self.groupid].task_done()
        return 0