Ejemplo n.º 1
0
 def __init__(self,
              model="Kursawe",
              probability=0.5,
              no_steps=20,
              baseline_top=-10**6,
              baseline_bottom=10**6):
     self.n = 6
     self.p = probability
     self.evals = 0
     self.steps = no_steps
     self.number_of_evaluations = 0
     self.threshold = -400
     if model == "Osyczka":
         self.model = Osyczka()
     elif model == "Golinski":
         self.model = Golinski()
     elif model == "Kursawe":
         self.model = Kursawe()
     elif model == "Schaffer":
         self.model = Schaffer()
     elif model == "DTLZ7":
         self.model = DTLZ7(10, 2)
     self.model.resetBaselines()
     #self.threshold = self.model.baseline_low
     self.current_state = self.model.get_random_state()
Ejemplo n.º 2
0
class SA:
    def __init__(self, model="Schaffer"):
        if model == "Schaffer":
            self.model = Schaffer()

    def __repr__(self):
        return time.strftime("%Y-%m-%d %H:%M:%S") + "\nSimulated Annealing on the Schaffer model\n"

    def P(self, old_e, new_e, k):
        return math.e ** ((old_e - new_e) / k)

    def run(self):
        print self
        kmax = 1000
        max_e = -0.1
        output = ""

        current_s = self.model.get_random_state()
        best_s = current_s

        current_e = self.model.normalize_energy(self.model.energy(current_s))
        best_e = current_e

        k = 1

        while k < kmax and current_e > max_e:
            neighbor_s = self.model.get_random_state()
            # print 'Neighbor: ' + str(neighbor_s)
            neighbor_e = self.model.normalize_energy(self.model.energy(neighbor_s))
            # print current_e, neighbor_e, tmp

            if neighbor_e < best_e:
                best_s, best_e = neighbor_s, neighbor_e
                output += " !"

            if neighbor_e < current_e:
                current_s, current_e = neighbor_s, neighbor_e
                output += " +"

            elif self.P(current_e, neighbor_e, (1 - (float(k) / kmax)) ** 5) > random.random():
                current_s, current_e = neighbor_s, neighbor_e
                output += " ?"

            else:
                output += " ."

            if k % 25 == 0:
                print ", %4d, : %d, %25s" % (k, self.model.denormalize_energy(best_e), output)
                output = ""

            k += 1

        print
        print "Best State Found: " + str(best_s)
        print "Energy At Best State: " + str(self.model.denormalize_energy(best_e))
        print

        return best_e
Ejemplo n.º 3
0
 def __init__(self, model = "Schaffer", probability = 0.5, no_steps = 10, baseline_top = -10**6, baseline_bottom = 10**6):
         self.n = 6
         self.p = probability
         self.evals = 0
         self.steps = no_steps
         self.number_of_evaluations = 0
         self.threshold = - 400
         if model == "Osyczka":
                 self.model = Osyczka()
         elif model == "Golinski":
                 self.model = Golinski()
         elif model == "Kursawe":
                 self.model = Kursawe()
         elif model == "Schaffer":
                 self.model = Schaffer()
         self.model.resetBaselines()
         self.current_state = self.model.get_random_state()
Ejemplo n.º 4
0
 def __init__(self, model = "Schaffer"):
   self.evals = 0
   
   if model == "Schaffer":
     self.model = Schaffer()
   elif model == "Osyczka":
     self.model = Osyczka()
   elif model == "Golinski":
     self.model = Golinski()
   elif model == "Kursawe":
     self.model = Kursawe()
Ejemplo n.º 5
0
 def __init__(self, model = "Osyczka"):
     self.top_bound = [3.6, 0.8, 28.0, 8.3, 8.3, 3.9, 5.5]
     self.bottom_bound = [2.6, 0.7, 17.0, 7.3, 7.3, 2.9, 5]
     self.f2_high = -10**6
     self.f2_low = 10**6
     self.f1_high = -10**6
     self.f1_low = 10**6
     self.evals = 0
     self.median = []
     if model == "Osyczka":
         self.model = Osyczka()
     elif model == "Golinski":
         self.model = Golinski()
     elif model == "Kursawe":
         self.model = Kursawe()
     elif model == "Schaffer":
         self.model = Schaffer()
     self.best_solution = Thing()
     self.best_solution.score = 0;
     self.best_solution.energy = 1;
     self.best_solution.have = []
Ejemplo n.º 6
0
 def __init__(self, model="Osyczka"):
     self.top_bound = [3.6, 0.8, 28.0, 8.3, 8.3, 3.9, 5.5]
     self.bottom_bound = [2.6, 0.7, 17.0, 7.3, 7.3, 2.9, 5]
     self.f2_high = -10**6
     self.f2_low = 10**6
     self.f1_high = -10**6
     self.f1_low = 10**6
     self.evals = 0
     self.median = []
     if model == "Osyczka":
         self.model = Osyczka()
     elif model == "Golinski":
         self.model = Golinski()
     elif model == "Kursawe":
         self.model = Kursawe()
     elif model == "Schaffer":
         self.model = Schaffer()
     self.best_solution = Thing()
     self.best_solution.score = 0
     self.best_solution.energy = 1
     self.best_solution.have = []
Ejemplo n.º 7
0
class MaxWalkSat:
    def __init__(self,
                 model="Kursawe",
                 probability=0.5,
                 no_steps=20,
                 baseline_top=-10**6,
                 baseline_bottom=10**6):
        self.n = 6
        self.p = probability
        self.evals = 0
        self.steps = no_steps
        self.number_of_evaluations = 0
        self.threshold = -400
        if model == "Osyczka":
            self.model = Osyczka()
        elif model == "Golinski":
            self.model = Golinski()
        elif model == "Kursawe":
            self.model = Kursawe()
        elif model == "Schaffer":
            self.model = Schaffer()
        elif model == "DTLZ7":
            self.model = DTLZ7(10, 2)
        self.model.resetBaselines()
        #self.threshold = self.model.baseline_low
        self.current_state = self.model.get_random_state()

    def modify_to_better_state(self, state, index):
        if (index > len(self.model.top_bound)):
            return None
        increment = (self.model.top_bound[index] -
                     self.model.bottom_bound[index]) / self.steps
        temp_state = list(state)
        best_state = state
        for _ in range(0, self.steps):
            temp_state[index] += increment
            self.evals += 1
            if self.model.energy(temp_state) < self.model.energy(
                    best_state
            ):  #and self.model.are_constraints_satisfied(temp_state):
                best_state = list(temp_state)
        state = best_state
        return state

    def retry(self, state):
        index = randint(0, (self.model.getNumberOfDecisions() - 1))
        temp_state = list(state)
        if self.p < random():
            temp_state[index] = self.model.bottom_bound[index] + (
                self.model.top_bound[index] -
                self.model.bottom_bound[index]) * random()
            # if self.model.are_constraints_satisfied(temp_state):
            #         return temp_state
            # else:
            #         return state
        else:
            temp_state = self.modify_to_better_state(temp_state, index)
            if temp_state == state:
                return temp_state
            else:
                return temp_state

    def run(self):
        max_tries = 100
        max_changes = 100
        self.model.resetBaselines()
        best_state = self.model.get_random_state()
        output = str()
        MAX_LIVES = 10
        ERA_LENGTH = 10
        era_List = []
        current_era = []
        lives = MAX_LIVES
        for _ in range(0, max_tries):
            current_state = self.model.get_random_state()
            for i in range(0, max_changes):
                if self.model.energy(current_state) < self.threshold:
                    return current_state
                else:
                    new_state = self.retry(current_state)
                operation = ""
                while new_state == None:
                    new_state = self.retry(current_state)
                if self.model.type1(best_state, new_state):
                    best_state = new_state
                    operation = "!"
                elif self.model.type1(current_state, new_state):
                    operation = "+"
                elif self.model.type1(new_state, current_state):
                    operation = "."
                current_era.append(new_state)
                if len(current_era) == ERA_LENGTH and len(era_List) > 0:
                    lives += self.model.type2(current_era, era_List[-1])
                    if lives <= 0:
                        print "Early termination"
                        break
                    era_List.append(current_era)
                    current_era = []
                output += operation
                if len(output) == 50:
                    output + " current best state energy = " + str(
                        best_state) + " Evaluations: " + str(self.evals)
                    output = ""
        print output + " current best state energy = " + str(
            best_state) + " Evaluations: " + str(self.evals)
        return best_state
Ejemplo n.º 8
0
class DE():
    def __init__(self, model = "Osyczka"):
        self.top_bound = [3.6, 0.8, 28.0, 8.3, 8.3, 3.9, 5.5]
        self.bottom_bound = [2.6, 0.7, 17.0, 7.3, 7.3, 2.9, 5]
        self.f2_high = -10**6
        self.f2_low = 10**6
        self.f1_high = -10**6
        self.f1_low = 10**6
        self.evals = 0
        self.median = []
        if model == "Osyczka":
            self.model = Osyczka()
        elif model == "Golinski":
            self.model = Golinski()
        elif model == "Kursawe":
            self.model = Kursawe()
        elif model == "Schaffer":
            self.model = Schaffer()
        self.best_solution = Thing()
        self.best_solution.score = 0;
        self.best_solution.energy = 1;
        self.best_solution.have = []
        
    #Returns three different things that are not 'avoid'.
    def threeOthers(self, lst, avoid):
        def oneOther():
            x = self.a(lst)
            while x in seen: 
                x = self.a(lst)
            seen.append( x )
            return x
        # -----------------------
        seen = list(avoid.have)
        this = oneOther()
        that = oneOther()
        theOtherThing = oneOther()
        return this, that, theOtherThing

    def a(self, lst):
        return lst[self.n(len(lst))]

    def n(self, number):
        return int(uniform(0, number))

    def candidate(self):
        something = [uniform(self.model.low(d), self.model.high(d))
        for d in self.model.decisions()]
        new = Thing()
        new.have  = something
        new.score = self.model.energy(new.have)
        new.energy = self.model.aggregate_energy(new.have)
        return new
    
    def run(self, max     = 100,  # number of repeats 
            np      = 100,  # number of candidates
            f       = 0.75, # extrapolate amount
            cf      = 0.3,  # prob of cross-over 
            epsilon = 0.01,
            s       = 0.1
    ):
        frontier = [self.candidate() for _ in range(np)] 
        median = []
        for k in range(max):
            total, n, output = self.update(f, cf, frontier)
            self.evals += n
            frontier.sort(key=lambda x: x.energy)
            #print output + "Frontier energy:" + str(total) + " Count:" + str(n) + " Max Energy:" + str(frontier[0].energy) + " Min Energy:" + str(frontier[len(frontier)-1].energy)
            min = frontier[0].energy
            max = frontier[len(frontier)-1].energy
            median.append(frontier[int(len(frontier)/2)].energy)
            big = max - (max-min)*s/100
            new_frontier = [x for x in frontier if x.energy <= big]
            frontier = new_frontier
            if (n > 0 and total/n > (1 - epsilon)) or n <= 0 or len(frontier) < 3:
                break
        # print "Median values:"
        # print median
        return self.best_solution.have

    def update(self, f, cf, frontier, total = 0.0, n = 0):
        output = ""
        for x in frontier:
            s = x.energy
            new = self.extrapolate(frontier, x, f, cf)
            if s < new.energy:
                output += "."
            elif new.energy < s:
                x.energy = new.energy
                x.score = new.score
                x.have  = new.have
                output += "+"
            if new.energy < self.best_solution.energy:
                self.best_solution.score = new.score
                self.best_solution.have = new.have
                self.best_solution.energy = new.energy
                self.best_solution.evals = self.evals + n
                output += "!"
            if len(output) == 50:
                print output + " Best Solution: [",
                for a in self.best_solution.have:
                    print("%.2f " % a),
                print "] Energy: " + str(self.best_solution.score) + " Evals: " + str(self.evals + n)
            n += 1
            total += x.energy
        return total, n, output
        
    def extrapolate(self, frontier, one, f, cf):
        out = Thing(id = one.id, have = list(one.have), score = self.model.energy(one.have), energy = self.model.aggregate_energy(one.have))
        two, three, four = self.threeOthers(frontier, one)
        changed = False
        for d in self.model.decisions():
            x, y, z = two.have[d], three.have[d], four.have[d]
            if random() < cf:
                changed = True
                new = x + f*(y - z)
                out.have[d]  = self.model.trim(new, d) # keep in range
        if not changed:
            d = self.a(self.model.decisions())
            out.have[d] = two.have[d]
        out.score = self.model.energy(out.have) # remember to score it
        out.energy = self.model.aggregate_energy(out.have)
        return out
Ejemplo n.º 9
0
 def __init__(self, model="Schaffer"):
     if model == "Schaffer":
         self.model = Schaffer()
Ejemplo n.º 10
0
class SA:
    def __init__(self, model="Schaffer"):
        if model == "Schaffer":
            self.model = Schaffer()

    def __repr__(self):
        return time.strftime(
            "%Y-%m-%d %H:%M:%S"
        ) + "\nSimulated Annealing on the Schaffer model\n"

    def P(self, old_e, new_e, k):
        return math.e**((old_e - new_e) / k)

    def run(self):
        print self
        kmax = 1000
        max_e = -0.1
        output = ''

        current_s = self.model.get_random_state()
        best_s = current_s

        current_e = self.model.normalize_energy(self.model.energy(current_s))
        best_e = current_e

        k = 1

        while k < kmax and current_e > max_e:
            neighbor_s = self.model.get_random_state()
            # print 'Neighbor: ' + str(neighbor_s)
            neighbor_e = self.model.normalize_energy(
                self.model.energy(neighbor_s))
            # print current_e, neighbor_e, tmp

            if neighbor_e < best_e:
                best_s, best_e = neighbor_s, neighbor_e
                output += ' !'

            if neighbor_e < current_e:
                current_s, current_e = neighbor_s, neighbor_e
                output += ' +'

            elif self.P(current_e, neighbor_e,
                        (1 - (float(k) / kmax))**5) > random.random():
                current_s, current_e = neighbor_s, neighbor_e
                output += ' ?'

            else:
                output += ' .'

            if k % 25 == 0:
                print ', %4d, : %d, %25s' % (
                    k, self.model.denormalize_energy(best_e), output)
                output = ''

            k += 1

        print
        print 'Best State Found: ' + str(best_s)
        print 'Energy At Best State: ' + str(
            self.model.denormalize_energy(best_e))
        print

        return best_e
Ejemplo n.º 11
0
 def __init__(self, model="Schaffer"):
     if model == "Schaffer":
         self.model = Schaffer()
Ejemplo n.º 12
0
class MaxWalkSat:
        def __init__(self, model = "Schaffer", probability = 0.5, no_steps = 10, baseline_top = -10**6, baseline_bottom = 10**6):
                self.n = 6
                self.p = probability
                self.evals = 0
                self.steps = no_steps
                self.number_of_evaluations = 0
                self.threshold = - 400
                if model == "Osyczka":
                        self.model = Osyczka()
                elif model == "Golinski":
                        self.model = Golinski()
                elif model == "Kursawe":
                        self.model = Kursawe()
                elif model == "Schaffer":
                        self.model = Schaffer()
                self.model.resetBaselines()
                self.current_state = self.model.get_random_state()

        def modify_to_better_state(self, state, index):
                if(index > len(self.model.top_bound)):
                        return None
                increment = (self.model.top_bound[index] - self.model.bottom_bound[index])/self.steps
                temp_state = list(state)
                best_state = state
                for _ in range(0, self.steps):
                        temp_state[index] += increment
                        self.evals += 1
                        if self.model.energy(temp_state) < self.model.energy(best_state) and self.model.are_constraints_satisfied(temp_state):
                                best_state = list(temp_state)
                state = best_state
                return state

        def retry(self, state):
                index = randint(0, (self.model.getNumberOfDecisions() - 1))
                temp_state = list(state)
                if self.p < random():
                        #print self.model.bottom_bound[index]
                        #print self.model.top_bound[index]
                        temp_state[index] = self.model.bottom_bound[index] + (self.model.top_bound[index] - self.model.bottom_bound[index])*random()
                        if self.model.are_constraints_satisfied(temp_state):
                                return temp_state
                        else:
                                return state
                else:
                        temp_state = self.modify_to_better_state(temp_state, index)
                        if temp_state == state:
                                return temp_state
                        else:
                                return temp_state

        def run(self):
                max_tries = 100
                max_changes = 50
                self.model.resetBaselines()
                best_state = self.model.get_random_state()
                output = str()
                lives = 5
                ERA_LENGTH = 25
                current_era = []
                eras = []
                for _ in range(0, max_tries):
                        current_state = self.model.get_random_state()
                        for i in range(0, max_changes):
                                if self.model.energy(current_state) < self.threshold:
                                        return current_state
                                else:
                                        new_state = self.retry(current_state)
                                operation = ""
                                if self.model.energy(new_state) > self.model.energy(best_state):
                                        best_state = new_state
                                        operation = "!"
                                elif self.model.energy(new_state) > self.model.energy(current_state):
                                        operation = "+"        
                                elif self.model.energy(new_state) < self.model.energy(current_state):
                                        operation = "."
                                output += operation
                                current_era.append(self.model.normalize_energy(self.model.energy(current_state), self.model.baseline_low, self.model.baseline_high))
                                if len(current_era) == ERA_LENGTH and len(eras) > 0:
                                        if a12(current_era, eras[-1]) < 0.56:
                                                lives -= 1
                                                #print "reducing the life by 1 lives: " + str(lives)
                                        else:
                                                lives = 5
                                if lives <= 0:
                                        print "Early termination"
                                        return
                                if len(current_era) == ERA_LENGTH:
                                        eras.append(current_era)
                                        current_era = []
                                if len(output) == 50:
                                        print "Lives: " + str(lives) + " " + output + " current best state energy(normalized) = " + str(self.model.energy(best_state)) + " Evaluations: " + str(self.evals)
                                        output = ""
                print output + " current best state energy(normalized) = " + str(self.model.aggregate_energy(best_state)) + " Evaluations: " + str(self.evals)
                return best_state
Ejemplo n.º 13
0
class DE():
    def __init__(self, model = "DTLZ7"):
        self.top_bound = [3.6, 0.8, 28.0, 8.3, 8.3, 3.9, 5.5]
        self.bottom_bound = [2.6, 0.7, 17.0, 7.3, 7.3, 2.9, 5]
        self.f2_high = -10**6
        self.f2_low = 10**6
        self.f1_high = -10**6
        self.f1_low = 10**6
        self.evals = 0
        self.median = []
        if model == "Osyczka":
            self.model = Osyczka()
        elif model == "Golinski":
            self.model = Golinski()
        elif model == "Kursawe":
            self.model = Kursawe()
        elif model == "Schaffer":
            self.model = Schaffer()
        elif model == "DTLZ7":
            self.model = DTLZ7(10, 2)
        self.best_solution = Thing()
        self.best_solution.score = 0;
        self.best_solution.energy = 1;
        self.best_solution.have = []
        self.previous_era = []
        
    #Returns three different things that are not 'avoid'.
    def threeOthers(self, lst, avoid):
        def oneOther():
            x = self.a(lst)
            while x in seen: 
                x = self.a(lst)
            seen.append( x )
            return x
        # -----------------------
        seen = list(avoid.have)
        this = oneOther()
        that = oneOther()
        theOtherThing = oneOther()
        return this, that, theOtherThing

    def a(self, lst):
        return lst[self.n(len(lst))]

    def n(self, number):
        return int(uniform(0, number))

    def candidate(self):
        # something = [uniform(self.model.low(d), self.model.high(d))
        # for d in self.model.decisions()]
        something = self.model.get_random_state()
        new = Thing()
        new.have  = something
        new.score = self.model.energy(new.have)
        new.energy = self.model.energy(new.have)
        return new
    
    def run(self, maximum = 100,  # number of repeats 
            np      = 100,  # number of candidates
            f       = 0.75, # extrapolate amount
            cf      = 0.3,  # prob of cross-over 
            epsilon = 0.01,
            s       = 0.1
    ):
        print "asd" + str(np)
        frontier = [self.candidate() for _ in range(np)] 
        print "test"
        median = []
        print maximum
        for k in range(0, 100):
            total, n, output = self.update(f, cf, frontier)
            self.evals += n
            frontier.sort(key=lambda x: x.energy)
            #print output + "Frontier energy:" + str(total) + " Count:" + str(n) + " Max Energy:" + str(frontier[0].energy) + " Min Energy:" + str(frontier[len(frontier)-1].energy)
            min = frontier[0].energy
            max = frontier[len(frontier)-1].energy
            median.append(frontier[int(len(frontier)/2)].score)
            big = max - (max-min)*s/100
            new_frontier = [x for x in frontier if x.energy <= big]
            frontier = new_frontier
            if (n > 0 and total/n > (1 - epsilon)) or n <= 0 or len(frontier) < 3:
                break
        print "Median values:"
        print median
        return self.previous_era

    def update(self, f, cf, frontier, total = 0.0, n = 0):
        output = ""
        MAX_LIVES = 10
        ERA_LENGTH = 10
        era_List = []
        current_era = []
        lives = MAX_LIVES
        for x in frontier:
            s = x.energy
            new = self.extrapolate(frontier, x, f, cf)
        #     if self.model.type1(x.have, new.have):
        #         output += "."
            if s < new.energy:
                output += "."
        #     elif self.model.type1(new.have, x.have):
        #         x.energy = new.energy
        #         x.score = new.score
        #         x.have  = new.have
        #         output += "+"
            elif new.energy < s:
                x.energy = new.energy
                x.score = new.score
                x.have  = new.have
                output += "+"
        #     if self.model.type1(new.have, self.best_solution.have):
        #         self.best_solution.score = new.score
        #         self.best_solution.have = new.have
        #         self.best_solution.energy = new.energy
        #         self.best_solution.evals = self.evals + n
        #         output += "!"    
            if new.energy < self.best_solution.energy:
                self.best_solution.score = new.score
                self.best_solution.have = new.have
                self.best_solution.energy = new.energy
                self.best_solution.evals = self.evals + n
                output += "!"
            if len(output) == 50:
                print output + " Best Solution: [",
                for a in self.best_solution.have:
                    print("%.2f " % a),
                print "] Energy: " + str(self.best_solution.score) + " Evals: " + str(self.evals + n)
            current_era.append(x.have)
            if len(current_era) == ERA_LENGTH and len(era_List) > 0:
                increment = self.model.type2(current_era, era_List[-1])
                lives += increment
                if lives <= 0:
                    self.previous_era = era_List[-1]
                    print "Early termination"
                    break
                era_List.append(current_era)
                current_era = []
            n += 1
            total += x.energy
        return total, n, output
        
    def extrapolate(self, frontier, one, f, cf):
        out = Thing(id = one.id, have = list(one.have), score = self.model.energy(one.have), energy = self.model.energy(one.have))
        two, three, four = self.threeOthers(frontier, one)
        changed = False
        for d in self.model.decisions():
            x, y, z = two.have[d], three.have[d], four.have[d]
            if random() < cf:
                changed = True
                new = x + f*(y - z)
                out.have[d]  = max(0.0, min(new, 1.0)) # keep in range
        if not changed:
            d = self.a(self.model.decisions())
            out.have[d] = two.have[d]
        out.score = self.model.energy(out.have) # remember to score it
        out.energy = self.model.energy(out.have)
        return out
Ejemplo n.º 14
0
class MaxWalkSat:
        def __init__(self, model = "Kursawe", probability = 0.5, no_steps = 20, baseline_top = -10**6, baseline_bottom = 10**6):
                self.n = 6
                self.p = probability
                self.evals = 0
                self.steps = no_steps
                self.number_of_evaluations = 0
                self.threshold = - 400
                if model == "Osyczka":
                        self.model = Osyczka()
                elif model == "Golinski":
                        self.model = Golinski()
                elif model == "Kursawe":
                        self.model = Kursawe()
                elif model == "Schaffer":
                        self.model = Schaffer()
                elif model == "DTLZ7":
                        self.model = DTLZ7(10, 2)
                self.model.resetBaselines()
                #self.threshold = self.model.baseline_low
                self.current_state = self.model.get_random_state()

        def modify_to_better_state(self, state, index):
                if(index > len(self.model.top_bound)):
                        return None
                increment = (self.model.top_bound[index] - self.model.bottom_bound[index])/self.steps
                temp_state = list(state)
                best_state = state
                for _ in range(0, self.steps):
                        temp_state[index] += increment
                        self.evals += 1
                        if self.model.energy(temp_state) < self.model.energy(best_state): #and self.model.are_constraints_satisfied(temp_state):
                                best_state = list(temp_state)
                state = best_state
                return state

        def retry(self, state):
                index = randint(0, (self.model.getNumberOfDecisions() - 1))
                temp_state = list(state)
                if self.p < random():
                        temp_state[index] = self.model.bottom_bound[index] + (self.model.top_bound[index] - self.model.bottom_bound[index])*random()
                        # if self.model.are_constraints_satisfied(temp_state):
                        #         return temp_state
                        # else:
                        #         return state
                else:
                        temp_state = self.modify_to_better_state(temp_state, index)
                        if temp_state == state:
                                return temp_state
                        else:
                                return temp_state

        def run(self):
                max_tries = 100
                max_changes = 100
                self.model.resetBaselines()
                best_state = self.model.get_random_state()
                output = str()
                MAX_LIVES = 10
                ERA_LENGTH = 10
                era_List = []
                current_era = []
                lives = MAX_LIVES
                for _ in range(0, max_tries):
                        current_state = self.model.get_random_state()
                        for i in range(0, max_changes):
                                if self.model.energy(current_state) < self.threshold:
                                        return current_state
                                else:
                                        new_state = self.retry(current_state)
                                operation = ""
                                while new_state == None:
                                        new_state = self.retry(current_state)
                                if self.model.type1(best_state, new_state):
                                        best_state = new_state
                                        operation = "!"
                                elif self.model.type1(current_state, new_state):
                                        operation = "+"        
                                elif self.model.type1(new_state, current_state):
                                        operation = "."
                                current_era.append(new_state)
                                if len(current_era) == ERA_LENGTH and len(era_List) > 0:
                                        lives += self.model.type2(current_era, era_List[-1])
                                        if lives <= 0:
                                                print "Early termination"
                                                break
                                        era_List.append(current_era)
                                        current_era = []
                                output += operation
                                if len(output) == 50:
                                        output + " current best state energy = " + str(best_state) + " Evaluations: " + str(self.evals)
                                        output = ""
                print  output + " current best state energy = " + str(best_state) + " Evaluations: " + str(self.evals)
                return best_state
Ejemplo n.º 15
0
class MaxWalkSat:
        def __init__(self, model = "Kursawe", probability = 0.5, no_steps = 10, baseline_top = -10**6, baseline_bottom = 10**6):
                self.n = 6
                self.p = probability
                self.evals = 0
                self.steps = no_steps
                self.number_of_evaluations = 0
                if model == "Osyczka":
                        self.model = Osyczka()
                elif model == "Golinski":
                        self.model = Golinski()
                elif model == "Kursawe":
                        self.model = Kursawe()
                elif model == "Schaffer":
                        self.model = Schaffer()
                self.model.resetBaselines()
                self.threshold = self.model.baseline_low
                self.current_state = self.model.get_random_state()

        def modify_to_better_state(self, state, index):
                if(index > len(self.model.top_bound)):
                        return None
                increment = (self.model.top_bound[index] - self.model.bottom_bound[index])/self.steps
                temp_state = list(state)
                best_state = state
                for _ in range(0, self.steps):
                        temp_state[index] += increment
                        self.evals += 1
                        if self.model.energy(temp_state) < self.model.energy(best_state) and self.model.are_constraints_satisfied(temp_state):
                                best_state = list(temp_state)
                state = best_state
                return state

        def retry(self, state):
                index = randint(0, (self.model.getNumberOfDecisions() - 1))
                temp_state = list(state)
                if self.p < random():
                        #print self.model.bottom_bound[index]
                        #print self.model.top_bound[index]
                        temp_state[index] = self.model.bottom_bound[index] + (self.model.top_bound[index] - self.model.bottom_bound[index])*random()
                        if self.model.are_constraints_satisfied(temp_state):
                                return temp_state
                        else:
                                return state
                else:
                        temp_state = self.modify_to_better_state(temp_state, index)
                        if temp_state == state:
                                return temp_state
                        else:
                                return temp_state

        def run(self):
                max_tries = 100
                max_changes = 50
                self.model.resetBaselines()
                best_state = self.model.get_random_state()
                output = str()
                for _ in range(0, max_tries):
                        current_state = self.model.get_random_state()
                        for i in range(0, max_changes):
                                if self.model.energy(current_state) < self.threshold:
                                        return current_state
                                else:
                                        new_state = self.retry(current_state)
                                operation = ""
                                if self.model.aggregate_energy(new_state) < self.model.aggregate_energy(best_state):
                                        best_state = new_state
                                        operation = "!"
                                elif self.model.aggregate_energy(new_state) < self.model.aggregate_energy(current_state):
                                        operation = "+"        
                                elif self.model.aggregate_energy(new_state) > self.model.aggregate_energy(current_state):
                                        operation = "."
                                output += operation
                                if len(output) == 50:
                                        print output + " best_energy = " + str(self.model.energy(best_state)) + " Evaluations: " + str(self.evals)
                                        output = ""
                print output + " best_energy = " + str(self.model.energy(best_state)) + " Evaluations: " + str(self.evals)
                return best_state
Ejemplo n.º 16
0
class DE():
    def __init__(self, model="DTLZ7"):
        self.top_bound = [3.6, 0.8, 28.0, 8.3, 8.3, 3.9, 5.5]
        self.bottom_bound = [2.6, 0.7, 17.0, 7.3, 7.3, 2.9, 5]
        self.f2_high = -10**6
        self.f2_low = 10**6
        self.f1_high = -10**6
        self.f1_low = 10**6
        self.evals = 0
        self.median = []
        if model == "Osyczka":
            self.model = Osyczka()
        elif model == "Golinski":
            self.model = Golinski()
        elif model == "Kursawe":
            self.model = Kursawe()
        elif model == "Schaffer":
            self.model = Schaffer()
        elif model == "DTLZ7":
            self.model = DTLZ7(10, 2)
        self.best_solution = Thing()
        self.best_solution.score = 0
        self.best_solution.energy = 1
        self.best_solution.have = []
        self.previous_era = []

    #Returns three different things that are not 'avoid'.
    def threeOthers(self, lst, avoid):
        def oneOther():
            x = self.a(lst)
            while x in seen:
                x = self.a(lst)
            seen.append(x)
            return x

        # -----------------------
        seen = list(avoid.have)
        this = oneOther()
        that = oneOther()
        theOtherThing = oneOther()
        return this, that, theOtherThing

    def a(self, lst):
        return lst[self.n(len(lst))]

    def n(self, number):
        return int(uniform(0, number))

    def candidate(self):
        # something = [uniform(self.model.low(d), self.model.high(d))
        # for d in self.model.decisions()]
        something = self.model.get_random_state()
        new = Thing()
        new.have = something
        new.score = self.model.energy(new.have)
        new.energy = self.model.energy(new.have)
        return new

    def run(
            self,
            maximum=100,  # number of repeats 
            np=100,  # number of candidates
            f=0.75,  # extrapolate amount
            cf=0.3,  # prob of cross-over 
            epsilon=0.01,
            s=0.1):
        print "asd" + str(np)
        frontier = [self.candidate() for _ in range(np)]
        print "test"
        median = []
        print maximum
        for k in range(0, 100):
            total, n, output = self.update(f, cf, frontier)
            self.evals += n
            frontier.sort(key=lambda x: x.energy)
            #print output + "Frontier energy:" + str(total) + " Count:" + str(n) + " Max Energy:" + str(frontier[0].energy) + " Min Energy:" + str(frontier[len(frontier)-1].energy)
            min = frontier[0].energy
            max = frontier[len(frontier) - 1].energy
            median.append(frontier[int(len(frontier) / 2)].score)
            big = max - (max - min) * s / 100
            new_frontier = [x for x in frontier if x.energy <= big]
            frontier = new_frontier
            if (n > 0 and total / n >
                (1 - epsilon)) or n <= 0 or len(frontier) < 3:
                break
        print "Median values:"
        print median
        return self.previous_era

    def update(self, f, cf, frontier, total=0.0, n=0):
        output = ""
        MAX_LIVES = 10
        ERA_LENGTH = 10
        era_List = []
        current_era = []
        lives = MAX_LIVES
        for x in frontier:
            s = x.energy
            new = self.extrapolate(frontier, x, f, cf)
            #     if self.model.type1(x.have, new.have):
            #         output += "."
            if s < new.energy:
                output += "."
        #     elif self.model.type1(new.have, x.have):
        #         x.energy = new.energy
        #         x.score = new.score
        #         x.have  = new.have
        #         output += "+"
            elif new.energy < s:
                x.energy = new.energy
                x.score = new.score
                x.have = new.have
                output += "+"
        #     if self.model.type1(new.have, self.best_solution.have):
        #         self.best_solution.score = new.score
        #         self.best_solution.have = new.have
        #         self.best_solution.energy = new.energy
        #         self.best_solution.evals = self.evals + n
        #         output += "!"
            if new.energy < self.best_solution.energy:
                self.best_solution.score = new.score
                self.best_solution.have = new.have
                self.best_solution.energy = new.energy
                self.best_solution.evals = self.evals + n
                output += "!"
            if len(output) == 50:
                print output + " Best Solution: [",
                for a in self.best_solution.have:
                    print("%.2f " % a),
                print "] Energy: " + str(
                    self.best_solution.score) + " Evals: " + str(self.evals +
                                                                 n)
            current_era.append(x.have)
            if len(current_era) == ERA_LENGTH and len(era_List) > 0:
                increment = self.model.type2(current_era, era_List[-1])
                lives += increment
                if lives <= 0:
                    self.previous_era = era_List[-1]
                    print "Early termination"
                    break
                era_List.append(current_era)
                current_era = []
            n += 1
            total += x.energy
        return total, n, output

    def extrapolate(self, frontier, one, f, cf):
        out = Thing(id=one.id,
                    have=list(one.have),
                    score=self.model.energy(one.have),
                    energy=self.model.energy(one.have))
        two, three, four = self.threeOthers(frontier, one)
        changed = False
        for d in self.model.decisions():
            x, y, z = two.have[d], three.have[d], four.have[d]
            if random() < cf:
                changed = True
                new = x + f * (y - z)
                out.have[d] = max(0.0, min(new, 1.0))  # keep in range
        if not changed:
            d = self.a(self.model.decisions())
            out.have[d] = two.have[d]
        out.score = self.model.energy(out.have)  # remember to score it
        out.energy = self.model.energy(out.have)
        return out
Ejemplo n.º 17
0
class DE():
    def __init__(self, model="Osyczka"):
        self.top_bound = [3.6, 0.8, 28.0, 8.3, 8.3, 3.9, 5.5]
        self.bottom_bound = [2.6, 0.7, 17.0, 7.3, 7.3, 2.9, 5]
        self.f2_high = -10**6
        self.f2_low = 10**6
        self.f1_high = -10**6
        self.f1_low = 10**6
        self.evals = 0
        self.median = []
        if model == "Osyczka":
            self.model = Osyczka()
        elif model == "Golinski":
            self.model = Golinski()
        elif model == "Kursawe":
            self.model = Kursawe()
        elif model == "Schaffer":
            self.model = Schaffer()
        self.best_solution = Thing()
        self.best_solution.score = 0
        self.best_solution.energy = 1
        self.best_solution.have = []

    #Returns three different things that are not 'avoid'.
    def threeOthers(self, lst, avoid):
        def oneOther():
            x = self.a(lst)
            while x in seen:
                x = self.a(lst)
            seen.append(x)
            return x

        # -----------------------
        seen = list(avoid.have)
        this = oneOther()
        that = oneOther()
        theOtherThing = oneOther()
        return this, that, theOtherThing

    def a(self, lst):
        return lst[self.n(len(lst))]

    def n(self, number):
        return int(uniform(0, number))

    def candidate(self):
        something = [
            uniform(self.model.low(d), self.model.high(d))
            for d in self.model.decisions()
        ]
        new = Thing()
        new.have = something
        new.score = self.model.energy(new.have)
        new.energy = self.model.aggregate_energy(new.have)
        return new

    def run(
            self,
            max=100,  # number of repeats 
            np=100,  # number of candidates
            f=0.75,  # extrapolate amount
            cf=0.3,  # prob of cross-over 
            epsilon=0.01,
            s=0.1):
        frontier = [self.candidate() for _ in range(np)]
        median = []
        for k in range(max):
            total, n, output = self.update(f, cf, frontier)
            self.evals += n
            frontier.sort(key=lambda x: x.energy)
            #print output + "Frontier energy:" + str(total) + " Count:" + str(n) + " Max Energy:" + str(frontier[0].energy) + " Min Energy:" + str(frontier[len(frontier)-1].energy)
            min = frontier[0].energy
            max = frontier[len(frontier) - 1].energy
            median.append(frontier[int(len(frontier) / 2)].energy)
            big = max - (max - min) * s / 100
            new_frontier = [x for x in frontier if x.energy <= big]
            frontier = new_frontier
            if (n > 0 and total / n >
                (1 - epsilon)) or n <= 0 or len(frontier) < 3:
                break
        # print "Median values:"
        # print median
        return self.best_solution.have

    def update(self, f, cf, frontier, total=0.0, n=0):
        output = ""
        for x in frontier:
            s = x.energy
            new = self.extrapolate(frontier, x, f, cf)
            if s < new.energy:
                output += "."
            elif new.energy < s:
                x.energy = new.energy
                x.score = new.score
                x.have = new.have
                output += "+"
            if new.energy < self.best_solution.energy:
                self.best_solution.score = new.score
                self.best_solution.have = new.have
                self.best_solution.energy = new.energy
                self.best_solution.evals = self.evals + n
                output += "!"
            if len(output) == 50:
                print output + " Best Solution: [",
                for a in self.best_solution.have:
                    print("%.2f " % a),
                print "] Energy: " + str(
                    self.best_solution.score) + " Evals: " + str(self.evals +
                                                                 n)
            n += 1
            total += x.energy
        return total, n, output

    def extrapolate(self, frontier, one, f, cf):
        out = Thing(id=one.id,
                    have=list(one.have),
                    score=self.model.energy(one.have),
                    energy=self.model.aggregate_energy(one.have))
        two, three, four = self.threeOthers(frontier, one)
        changed = False
        for d in self.model.decisions():
            x, y, z = two.have[d], three.have[d], four.have[d]
            if random() < cf:
                changed = True
                new = x + f * (y - z)
                out.have[d] = self.model.trim(new, d)  # keep in range
        if not changed:
            d = self.a(self.model.decisions())
            out.have[d] = two.have[d]
        out.score = self.model.energy(out.have)  # remember to score it
        out.energy = self.model.aggregate_energy(out.have)
        return out