I am currently creating my genetic algorithm and want to print the number of generations at the very end of the program when it finishes. However I am unsure how to access the counter variable that is the number of generations when it is outside of the class and method. So for example, at the end it would be like
Generation 100, average fit 18966, best fit 18947
Your best chromosone at generation 100
'\x06pzÂ\x8cYÆr¯n0q\x07l¿M8\x93Þ\x19\x87"\x01\x85\x1er\x89[F_VyER\x9b\x0bm=)\x9a\x9a¿¥\x10F\x12A\x84\x0fZ^\x14\x99\x8a4®\x9f¿*\\\xa0yi\x19E\x8aÇ+6(_<¾£cO~\x9c\x99\x932\x06\x0f\x82\x7f¤\x808xǸñA\x13\x0e<%\x06ÿ#í\x91Pô\x98 ®\r\x1b}\x89y¦\x0cqAK\tp\x95\x99ÔNj=Wn\x16\x94\x0cu!¯ñ\x13Qü[e8_ÂóU\x10\x1av_+%Q_¡ù\x87=\x08~ciÎ_Ï[\x8f#AëT\x14©qG\x89#Z«L\x9b¢\x94WL\x1dV¶R03\x84æ^ßr\x1fÃÈ\x1d\x8e Læª&®x\x94?TAÒD\x14£i\x82J\x15=w~\x03\x0c\xa0¾5\x02f5T\x91ol¢bIÞfk¬¡27W16(}6\x92\x87\n®xm0\x1a\n<8(à}ñ\x88̾\x17g\x9bj6\x8fI&\x12\x9aÂ\x9a_F\x1a\r[\x1dK\x15<.±DjcIy`98d>\x197Z\x91£%tIJ\x820\x93|\x07\x8dnÚ QÂ!Pf\x1d\nåòf\x91\x1d#S¾|\x9ff[d>O=T$ݶI\x9e»QÛÂ\x1d"¿U=û´F÷\x83C}wA\xa0É\x8aD\x93x»\x85\x7f\x14^\x0eL'
done:
100 generations
How do I exactly access the 100 from the method in the class?
import random
class GeneticAlgorithm(object):
def __init__(self, genetics):
self.genetics = genetics
pass
def run(self):
population = self.genetics.initial()
while True:
fits_pops = [(self.genetics.fitness(ch), ch) for ch in population]
if self.genetics.check_stop(fits_pops): break
population = self.next(fits_pops)
pass
return population
def next(self, fits):
parents_generator = self.genetics.parents(fits)
size = len(fits)
nexts = []
while len(nexts) < size:
parents = next(parents_generator)
cross = random.random() < self.genetics.probability_crossover()
children = self.genetics.crossover(parents) if cross else parents
for ch in children:
mutate = random.random() < self.genetics.probability_mutation()
nexts.append(self.genetics.mutation(ch) if mutate else ch)
pass
pass
return nexts[0:size]
pass
class GeneticFunctions(object):
def probability_crossover(self):
r"""returns rate of occur crossover(0.0-1.0)"""
return 1.0
def probability_mutation(self):
r"""returns rate of occur mutation(0.0-1.0)"""
return 0.0
def initial(self):
r"""returns list of initial population
"""
return []
def fitness(self, chromosome):
r"""returns domain fitness value of chromosome
"""
return len(chromosome)
def check_stop(self, fits_populations):
r"""stop run if returns True
- fits_populations: list of (fitness_value, chromosome)
"""
return False
def parents(self, fits_populations):
r"""generator of selected parents
"""
gen = iter(sorted(fits_populations))
while True:
f1, ch1 = next(gen)
f2, ch2 = next(gen)
yield (ch1, ch2)
pass
return
def crossover(self, parents):
r"""breed children
"""
return parents
def mutation(self, chromosome):
r"""mutate chromosome
"""
return chromosome
pass
if __name__ == "__main__":
"""
example: Mapped guess prepared Text
"""
class GuessText(GeneticFunctions):
def __init__(self, target_text,
limit=100, size=100,
prob_crossover=0.9, prob_mutation=0.2):
self.target = self.text2chromo(target_text)
self.counter = 0
self.limit = limit
self.size = size
self.prob_crossover = prob_crossover
self.prob_mutation = prob_mutation
pass
# GeneticFunctions interface impls
def probability_crossover(self):
return self.prob_crossover
def probability_mutation(self):
return self.prob_mutation
def initial(self):
return [self.random_chromo() for j in range(self.size)]
def fitness(self, chromo):
# larger is better, matched == 0
return -sum(abs(c - t) for c, t in zip(chromo, self.target))
def check_stop(self, fits_populations):
self.counter += 1
if self.counter % 100 == 0:
best_match = list(sorted(fits_populations))[-1][1]
fits = [f for f, ch in fits_populations]
best = -(max(fits))
ave = -(sum(fits) / len(fits))
print(
"Generation %3d, average fit %4d, best fit %4d" %
(self.counter, ave, best,
))
print("Your best chromosone at generation %3d" % self.counter)
print("%r" % self.chromo2text(best_match))
pass
return self.counter >= self.limit
def parents(self, fits_populations):
while True:
father = self.tournament(fits_populations)
mother = self.tournament(fits_populations)
yield (father, mother)
pass
pass
def crossover(self, parents):
father, mother = parents
index1 = random.randint(1, len(self.target) - 2)
index2 = random.randint(1, len(self.target) - 2)
if index1 > index2: index1, index2 = index2, index1
child1 = father[:index1] + mother[index1:index2] + father[index2:]
child2 = mother[:index1] + father[index1:index2] + mother[index2:]
return (child1, child2)
def mutation(self, chromosome):
index = random.randint(0, len(self.target) - 1)
vary = random.randint(-5, 5)
mutated = list(chromosome)
mutated[index] += vary
return mutated
# internals
def tournament(self, fits_populations):
alicef, alice = self.select_random(fits_populations)
bobf, bob = self.select_random(fits_populations)
return alice if alicef > bobf else bob
def select_random(self, fits_populations):
return fits_populations[random.randint(0, len(fits_populations)-1)]
def text2chromo(self, text):
return [ord(ch) for ch in text]
def chromo2text(self, chromo):
return "".join(chr(max(1, min(ch, 255))) for ch in chromo)
def random_chromo(self):
return [random.randint(1, 255) for i in range(len(self.target))]
pass
GeneticAlgorithm(GuessText("""The smartest and fastest Pixel yet.
Google Tensor: Our first custom-built processor.
The first processor designed by Google and made for Pixel, Tensor makes the new Pixel phones our most powerful yet.
The most advanced Pixel Camera ever.
Capture brilliant color and vivid detail with Pixels best-in-class computational photography and new pro-level lenses.""")).run()
print('done:')
print("%3d " 'generations' % counter)
pass
Define the GuessText first. Then access the counter.
gt = GuessText("""The smartest and fastest Pixel yet.
Google Tensor: Our first custom-built processor.
The first processor designed by Google and made for Pixel, Tensor makes the new Pixel phones our most powerful yet.
The most advanced Pixel Camera ever.
Capture brilliant color and vivid detail with Pixels best-in-class computational photography and new pro-level lenses.""")
GeneticAlgorithm(gt).run()
print('done:')
print("%3d " 'generations' % gt.counter)
Related
I am writing Python code for the implementation of the Genetic Algorithm.
I am stuck on creating children. I need this for my research. I have implemented the code to the best of my ability.
def initialise_city(num_dim, limit = 100):
X = np.random.randint(0,limit,size=num_dim)
return X
def initialise_cities(num_cities):
cities = []
for i in range(num_cities):
cities.append(initialise_city(2))
return cities
num_cities = 5
cities = initialise_cities(num_cities)
print("City Positions: ", cities)
def distance_function(cities, visit_order):
distance = 0.0
visit_pos = 0
next_pos = 0
for i, txt in enumerate(visit_order):
if (i < len(visit_order)-1):
visit_pos = visit_order[i]
next_pos = visit_order[i+1]
distance = distance + np.sqrt((cities[visit_pos][0]-cities[next_pos][0])**2 +(cities[visit_pos][1]-cities[next_pos][1])**2)
#raise NotImplementedError()
return -1.0*distance
def initialise_chromosome(chromosome_size):
# YOUR CODE HERE
chromosome = np.random.permutation(chromosome_size)
#raise NotImplementedError()
return chromosome
def initialise_population(population_size, chromosome_size):
population = []
# YOUR CODE HERE
for i in range(population_size):
population.append(initialise_chromosome(chromosome_size))
#raise NotImplementedError()
return population
def calculate_fitness(population, cities, fitness_function):
fitness_list = []
# YOUR CODE HERE
d = 0.0
for i,ix in enumerate(population):
d = fitness_function(cities,ix)
fitness_list.append(fitness_function(cities,ix))
#raise NotImplementedError()
return fitness_list
def selection(population, fitness_list):
## Select the top half of the best of the population
population = np.array(population)
sorted_indices = np.argsort(fitness_list)
selection_point = int(1+ len(fitness_list)/2)
# Randomply permute this top half of the poulation
indices = np.random.choice(sorted_indices[:selection_point], len(population))
best_population = population[indices]
return best_population
def pairing(selected_population):
## pair up parents that will be used to reproduce
count = 0
pairs = []
while count < len(selected_population)-1:
index = count
pairs.append([selected_population[index],selected_population[index+1]])
count +=2
return pairs
I am stuck on this part where children are supposed to be created.
def create_child(a,b):
child = []
# YOUR CODE HERE
point = random.randint(1,len(pairs))
#raise NotImplementedError()
return child
def cross_over(pairs):
final_population = []
for a,b in pairs:
child = create_child(a,b)
final_population.append(child)
child = create_child(b,a)
final_population.append(child)
return final_population
I'm coding a tree search in a simultaneous move game in python. I'm using python at this stage for ease of development, before rewriting the project in C++. For this reason, I would normally not be too concerned about performance. Originally my code was structured like this
class DecisionNode():
def __init__(self, players):
self.transitions = dict()
self.N = [dict() for _ in players]
self.V = [dict() for _ in players]
self.visited = 0
self.c = 2
self.players = players
def search(self, s, depth=0, verbose=False, mode=None):
if not self.visited: #unexpanded node
score, log = s.rollout()
actions = s.actions()
for acts, n, v in zip(actions, self.N, self.V):
for a in acts:
n[a] = 0
v[a] = 0
for x in actions[0]:
for y in actions[1]:
self.transitions[(x, y)] = TransitionNode(self.players)
self.visited += 1
return score, depth, False
else:
if not self.transitions: #terminal node
score, log = s.rollout()
return score, depth, True
else: #recursive call
actionTuple = self.selectActions(verbose, mode)
transition = self.transitions[actionTuple]
if not transition.step:
transition.expand(s.apply(actionTuple, battle.Path()))
steps = transition.select()
path, s_ = s.apply(actionTuple, battle.Path(steps))[0]
child = transition.decisions[steps]
score, d, terminal = child.search(s_, depth+1, verbose, mode)
for _, a, n, v, in zip(score, actionTuple, self.N, self.V):
n[a] += 1
v[a] += _
self.visited += 1
return score, d, terminal
def selectActions(self, verbose=False, mode=None):
actionTuple = ()
data = [[], []]
for _ in self.players:
actions = self.N[_].keys()
for a in actions:
n = self.N[_][a] + 1
v = self.V[_][a]
if mode is None:
score = v/n + self.c * math.sqrt(math.log(self.visited)/n)
data[_].append((a, score))
actionTuple += (max(data[_],key= lambda datum : datum[1])[0],)
return actionTuple
class TransitionNode():
def __init__(self, players):
self.players = players
self.step = []
self.probs = []
self.paths = []
self.decisions = dict()
def expand(self, batch):
for path, state in batch:
self.step.append(path.step)
self.probs.append(path.p)
self.paths.append(path)
self.decisions[path.step] = DecisionNode(self.players)
def select(self):
steps = numpy.random.choice(self.paths, 1, p=self.probs)[0].step
return steps
Essentially, there are two different nodes, one for the player's decisions and one for chance. Crucially, all the methods for the search are in the class. It's a recursive search; at each decision node each player selects a move according to the UCT formula, that node has a dictionary of action tuples and points to a chance node. That chance node has a dict of decision nodes it points to, and it randomly samples a decision node. This continues deeper down the tree, until it hits a terminal node or an unexpanded decision node, where it expands and returns the rollout value back up the tree to the root.
Now this code is python slow but not slow. I've been timing how long it takes to do bathes of 3000 root to leaf traversals like above, and importantly it does not take any longer to do the tenth batch vs the first. You might expect it would take longer because of expansion of the tree, therefore deeper traversals. But in the case I gave it most of the traversals are hitting terminal nodes about 8 recursive calls in.
So why fix what's not broken? Well I noticed that my memory usage was exploding, probably because each node had its own copy of the methods attached. So, I rewrote the code so that both node classes only had statistics attached, and all the motion was provided by another Search class which contains all the methods. I also changed the provably ineffective UCT algorithm to a regret matching one. This is going to be slower, since its a bit more involved:
import numpy
import battle
import time
class Decision():
def __init__(self):
self.expanded = False
class Chance():
def __init__(self):
self.X = (0,0)
self.n = 0
class Search():
def __init__(self):
pass
def expandDecision(self, node, state):
node.actions = state.actions()
node.regrets = tuple([0 for a in _] for _ in node.actions)
node.strategies = tuple([0 for a in _] for _ in node.actions)
node.chances = dict()
for _ in range(len(node.actions[0])):
for __ in range(len(node.actions[1])):
node.chances[(_, __)] = Chance()
node.expanded = True
def expandChance(self, chance, state, moves):
chance.decisions = dict()
batch = state.apply(moves)
chance.steps = tuple(pair[0].step for pair in batch)
chance.p = tuple(pair[0].p for pair in batch)
def sampleChance(self, chance):
step = chance.steps[numpy.random.choice(range(len(chance.steps)), 1, p=chance.p)[0]]
if not step in chance.decisions.keys():
chance.decisions[step] = Decision()
return chance.decisions[step]
def getStrategy(self, node):
epsilon = 10**-3
s = [[max(__, epsilon) for __ in _] for _ in node.regrets]
t = [sum(_) for _ in s]
s = [[___/__ for ___ in _] for _, __ in zip(s, t)]
return s
def avgScore(self, chance):
return tuple((chance.X[_])/(chance.n + (chance.n == 0)) for _ in range(2))
def getRegrets(self, node, actions, u):
r = [[self.avgScore(node.chances[actions[:_] + (i,) + actions[_+1:]])[_] - u[_] for i in range(len(node.actions[_]))] for _ in range(2)]
r[0][actions[0]] = 0
r[1][actions[1]] = 0
return r
def sampleActions(self, strategy, gamma=0):
uniform = tuple([1/len(_)]*len(_) for _ in strategy)
fuzzy = tuple([(1-gamma)*x + (gamma)*y for x, y in zip(_, __)] for _, __ in zip(strategy, uniform))
return tuple(numpy.random.choice(range(len(_)), 1, p=_)[0] for _ in fuzzy)
def getMoves(self, node, actions):
return tuple(_[i] for i, _ in zip(actions, node.actions))
def updateDecision(self, node, r, s):
for _ in range(2):
for __ in range(len(node.actions[_])):
node.regrets[_][__] += r[_][__]
node.strategies[_][__] += s[_][__]
def updateChance(self, chance, u):
chance.X = tuple(chance.X[_] + u[_] for _ in range(2))
chance.n += 1
def averageStrategy(self, node):
return tuple(tuple(__/s for __ in _) for _, s in zip(node.strategies, map(sum, node.strategies)))
def search(self, node, state, gamma=0, depth=0):
if not node.expanded:
self.expandDecision(node, state)
return state.rollout()[0], depth, False
if not any(node.actions):
return state.rollout()[0], depth, True
sigma = self.getStrategy(node)
actions = self.sampleActions(sigma, gamma)
moves = self.getMoves(node, actions)
chance = node.chances[actions]
if not chance.n:
self.expandChance(chance, state, moves)
node_ = self.sampleChance(chance)
u, _, terminal = self.search(node_, state.apply(moves)[0][1], gamma, depth+1)
regrets = self.getRegrets(node, actions, u)
self.updateDecision(node, regrets, sigma)
self.updateChance(chance, u)
return self.avgScore(chance), _, terminal
if __name__ == '__main__':
node = Decision()
state = battle.s
__ = 3000
test = Search()
a = time.time()
total_depth = 0
terminal_count = 0
for _ in range(__ * 10):
test = Search()
u, depth, terminal = test.search(node, state, gamma=.03)
total_depth += depth
terminal_count += terminal
if (_+1)%__ == 0:
print(_+1)
for action, chance in node.chances.items():
print(node.actions)
print(action, chance.n, test.avgScore(chance))
print(test.averageStrategy(node))
#depth
avg_depth = total_depth/__
terminal_ratio = terminal_count/__
print('avg_depth: ', avg_depth)
print('terminal: ', terminal_ratio)
total_depth = 0
terminal_count = 0
#time
b = time.time()
print('t= ', b - a)
a = b
However this implementation is much slower. rather that a constant time (~20 sec) per batch, I have times of 20, 60, 100, 140 etc. Each batch of 3000 simulations is taking 40 seconds slower than the last!
I actually wrote a control case that uses the detached search of the later code and the UCT of the former and this still exhibits the slowdown effect. I thought maybe it was because I was reusing the same search object for all calls so I tried periodically re-instantiating it but to no avail.
I have a problem with an algorithmic task. There is content of it: "You have ten points on a plane and none three of them are collinear, each pair of different points is connected by line segment, which is green or blue. Calculate how many triangles have sides only in one colour." I tried a solution with n-ary trees but I get repeated triangles with cyclic permutations of integers on the result list.
Patryk, the problem is solvable with n-trees. However, to avoid cyclic permutations you need to skip symmetric line segments. If you create a line segment from 0 -> 1 you do not need to create a segment from 1 -> 0. Below is a complete code which solves the problem with n-trees with the recursive depth-first search. Excuse for Polish names of classes and methods. The interface is in English however. If you analyze the code you will get the point surely.
from random import choice
import copy
class Punkt:
def __init__(self, numer):
self.__numer = numer
self.__odcinki = {}
def dodaj_odcinek_wychodzący(self, punkt_docelowy, kolor):
self.__odcinki[punkt_docelowy] = Odcinek(self.__numer, punkt_docelowy, kolor)
def wez_odcinek_do_punktu_docelowego(self, punkt_docelowy):
return (punkt_docelowy, self.__odcinki[punkt_docelowy].wez_kolor())
def liczba_odcinkow(self):
return len(self.__odcinki)
def wez_kolor_punktu_docelowego(self, punkt_docelowy):
return self.__odcinki[punkt_docelowy].wez_kolor()
def lista_punktow_docelowych(self):
return self.__odcinki.keys()
class Odcinek:
def __init__(self, punkt_zrodlowy, punkt_docelowy, kolor):
self.__punkt_zrodlowy = punkt_zrodlowy
self.__punkt_docelowy = punkt_docelowy
self.__kolor = kolor
def wez_kolor(self):
return self.__kolor
class Structure:
def __init__(self, liczba_punktow=10):
self.__punkty = [Punkt(i)
for i in range(liczba_punktow)]
for i in range(liczba_punktow):
for j in range(i + 1, liczba_punktow):
self.__punkty[i].dodaj_odcinek_wychodzący(j, choice(["green", "blue"]))
# for j in range(liczba_punktow):
# for i in range (j + 1, liczba_punktow):
# self.__punkty[j].dodaj_odcinek_wychodzący(*(self.__punkty[j].wez_odcinek_do_punktu_docelowego(i)))
def wez_punkt(self, numer):
return self.__punkty[numer]
def wez_liczbe_punktow(self):
return len(self.__punkty)
class Search:
def __init__(self, struktura):
self.__s = struktura
def wez_liczbe_punktow(self):
return self.__s.wez_liczbe_punktow()
def wez_punkt(self, numer):
return self.__s.wez_punkt(numer)
def szukaj(self, kolor="green"):
self.__szukany_kolor = kolor
lista_trojkatow = []
liczba_trojkatow = 0
wszystkie_trojkaty = []
for i in range(self.wez_liczbe_punktow()):
lista_odwiedzonych_punktow = [i]
lista_trojkatow = self.szukaj_z_punktu(i,lista_odwiedzonych_punktow,lista_trojkatow)
return len(lista_trojkatow), lista_trojkatow
def wez_szukany_kolor(self):
return self.__szukany_kolor
def szukaj_z_punktu(self, numer, trojkat, lista_trojkatow):
if len(trojkat) == 3: # jeżeli zebraliśmy już trzy punkty to brakuje tylko zamykającego, czwartego
if self.wez_punkt(trojkat[0]).wez_kolor_punktu_docelowego(
trojkat[-1]) == self.wez_szukany_kolor(): # sprawdź czy do punktu zamykającego prowadzi odcinek o szukanym kolorze
trojkat.append(trojkat[0]) # dodaj punkt zamykajacy do trójkąta
lista_trojkatow.append(trojkat) # dodaj trojkąt do listy trójkątów
# return lista_trojkatow # zwróć liste trójkątów obliczonych dotychczas
else:
potomkowie = []
for punkt_docelowy in self.wez_punkt(numer).lista_punktow_docelowych():
if self.wez_punkt(numer).wez_kolor_punktu_docelowego(punkt_docelowy) == self.wez_szukany_kolor():
potomkowie.append(punkt_docelowy)
for potomek in potomkowie:
trojkat_kopia = copy.copy(trojkat)
trojkat_kopia.append(potomek)
lista_trojkatow = self.szukaj_z_punktu(potomek, trojkat_kopia, lista_trojkatow)
return lista_trojkatow
if __name__ == "__main__":
s = Structure()
for source_point in range(s.wez_liczbe_punktow()):
for destination_point in s.wez_punkt(source_point).lista_punktow_docelowych():
print(f"{source_point} -> {destination_point} = {s.wez_punkt(source_point).wez_kolor_punktu_docelowego(destination_point)}")
color = "green"
searching = Search(s)
number_of_triangles, all_triangles = searching.szukaj("green")
print(f"Number of triangles of color {color} = {number_of_triangles}")
print(f"List of all triangles: {all_triangles}")
My basic idea was to create a linked list, and as each new value comes in, add 1/N times the new value and subtract 1/N times the first value, then move the pointer to first along by one and free the memory that had been associated with first.
This won't ultimately be implemented in Python but just to get the process clear in my head, I tried to write it in Python, but my implementation is flawed. Do I need a doubly linked list for this? Is there an alternative approach (not linked-list based) that would be better?
Here's my attempt so far:
class Link:
def __init__(self,val):
self.next = None
self.value = val
class LinkedList:
def __init__(self,maxlength):
self.current_link = None
self.maxlength = maxlength
self.sum = 0.
self.average = None
self.length = 0
self._first_link = None
def add_link(self,val):
new_link = Link(val)
new_link.next = self.current_link
self.current_link = new_link
if self._first_link is None:
self._first_link = self.current_link
self.sum += val
if self.length < self.maxlength:
self.length += 1
else:
self.sum -= self._first_link.value
self._first_link = self._first_link.next # this line is flawed
self.average = self.sum/self.length
def get_first(self):
return self._first_link.value
# Main
ll = LinkedList(5)
for ii in xrange(10):
ll.add_link(ii)
print ii,ll.get_first(),ll.average
The problem is that _first_link gets set to a value that doesn’t have a next. That is, _first_link gets set to the first item that's added, but its next is None, so I don't see how to move it along by 1 as I want to. This is what makes me wonder if a doubly linked list is needed.
I'd appreciate any advice.
I think the simplest implementation is to use a circular linked list (a.k.a. a ring):
class Link(object):
def __init__(self, value=0.0):
self.next = None
self.value = value
class LinkedRing(object):
def __init__(self, length):
self.sum = 0.0
self.length = length
self.current = Link()
# Initialize all the nodes:
last = self.current
for i in xrange(length-1): # one link is already created
last.next = Link()
last = last.next
last.next = self.current # close the ring
def add_val(self, val):
self.sum -= current.value
self.sum += val
self.current.value = val
self.current = self.current.next
def average(self):
return self.sum / self.length
# Test example:
rolling_sum = LinkedRing(5)
while True:
x = float(raw_input())
rolling_sum.add_val(x)
print(">> Average: %f" % rolling_sum.average())
You can implement this using collections.deque and the numerically stable math for maintaining running averages:
import collections
class AveragingBuffer(object):
def __init__(self, maxlen):
assert( maxlen>1)
self.q=collections.deque(maxlen=maxlen)
self.xbar=0.0
def append(self, x):
if len(self.q)==self.q.maxlen:
# remove first item, update running average
d=self.q.popleft()
self.xbar=self.xbar+(self.xbar-d)/float(len(self.q))
# append new item, update running average
self.q.append(x)
self.xbar=self.xbar+(x-self.xbar)/float(len(self.q))
if __name__=="__main__":
import scipy
ab=AveragingBuffer(10)
for i in xrange(32):
ab.append(scipy.rand())
print ab.xbar, scipy.average(ab.q), len(ab.q)
Okay, I thought of a solution that works in O[1] time. I'm still curious if anyone has a linked-list-based solution, but this solution avoids the LL entirely:
class Recent:
def __init__(self,maxlength):
self.maxlength = maxlength
self.length = 0
self.values = [0 for ii in xrange(maxlength)]
self.index = 0
self.total = 0.
self.average = 0.
def add_val(self,val):
last = self.values[self.index%self.maxlength]
self.values[self.index%self.maxlength] = val
self.total += val
self.total -= last
if self.length < self.maxlength:
self.length += 1
self.average = self.total / self.length
self.index += 1
def print_vals(self):
print ""
for ii in xrange(self.length):
print ii,self.values[ii%self.maxlength]
print "average:",self.average
# Example to show it works
rr = Recent(5)
for ii in xrange(3):
rr.add_val(ii)
rr.print_vals()
for ii in xrange(13):
rr.add_val(ii)
rr.print_vals()
Why is it giving me an error " 'int' object is not subscriptable " when i run the program? I looked if i was doing anything wrong, i understand it has to be an integer on line 24, but when I'm changing capacity[1] to capacity(int[1]) , it gives me the same error. Any hint would be appreciated.
class Bag():
__slots__=('name', 'weight', 'value')
def mkBag(name, weight, value):
thisBag = Bag()
thisBag.name = name
thisBag.weight = weight
thisBag.value = value
return thisBag
def ratio(treasure):
print(treasure)
print(treasure)
return treasure[2]//treasure[1]
def plunder(treasure, capacity):
treasure = sorted(treasure, key=ratio, reverse=True)
bagLst = []
current = 0
while current < capacity:
if capacity != 0:
if capacity > current[1]:
bagLst.append(mkBag(treasure[0],weight[1],current[2]))
capacity = capacity - current[1]
else:
bagLst.append(mkBag(current[0], capacity, (current[2]/current[1]), capacity))
capacity = 0
return bagLst
def main():
capacity = 10
name = ''
weight = 0
value = 0
treasure = [('silver', 20, 100), ('platinum', 10, 400), ('paladium',10,800), ('diamonds',5,900), ('gold', 10,60)]
bagLst = plunder(treasure, capacity)
for line in bagLst:
print('bagLst')
current is an int:
current = 0
but you are trying to use it as a list:
if capacity > current[1]:
bagLst.append(mkBag(treasure[0],weight[1],current[2]))
capacity = capacity - current[1]
else:
bagLst.append(mkBag(current[0], capacity, (current[2]/current[1]), capacity))
everywhere you use current[index] you are trying to index the integer value.
If you expected current to be a sequence instead, you'd need to set it to one.
I suspect you want to inspect the current treasure to add to the bag; you didn't pick any treasure item however. Something along the lines of:
current = 0
while capacity and current < len(treasure):
item = treasure[current]
current += 1
if capacity > item[1]:
bagLst.append(mkBag(item[0], item[1], item[2]))
capacity = capacity - item[1]
else:
bagLst.append(mkBag(item[0], capacity, (item[2]/item[1]), capacity))
capacity = 0
"int" object not subscriptable means you're trying to do 1234[1]. That doesn't make any sense! You can subscript a string ('abcdefg'[1] == 'b') and a list ([1,2,3,4,5][1] == 2) but you can't get the "nth element" of an integer.
In your line:
# in def plunder(...):
if capacity > current[1]:
You're trying to access the 2nd element of current, which is currently equal to the integer 0. Are you trying to make that a list? What are you expecting to be in current[1]?
Here's a substantially better way to accomplish this
Hey there, so I figured you meant that current[1] was actually item[1], meaning the weight of the item you were looking at. Instead, current was intended to be the running-weight of the bag. Understood! That said, I wrote up a better solution for this: take a look see!
class Treasure(object):
def __init__(self,name,weight=0,value=0,id_=0):
self.name = name
self.weight = weight
self.value = value
self.id = id_ # bootstrap for further development
#property
def ratio(self):
return self.value/self.weight
class BagFullError(ValueError):
pass
class Bag(object):
def __init__(self,owner=None,capacity=10):
self.owner = owner
self.capacity = capacity
self.contents = list()
def __str__(self):
return_value = "CONTENTS:"
for item in self.contents:
return_value += "\n ${0.value:4} {0.name:10}{0.weight} lb".format(item)
return return_value
def add(self,other):
if not isinstance(other,Treasure):
raise TypeError("Must pick up Treasure")
if self.weight + other.weight > self.capacity:
raise BagFullError("Bag cannot fit {}({} lb) ({} lb/{} lb)".format(
other.name,other.weight,self.weight,self.capacity))
self.contents.append(other)
def remove(self,other):
self.contents.remove(other)
# may throw ValueError if `other` not in `self.contents`
#property
def weight(self):
return sum(item.weight for item in self.contents)
treasure = [Treasure('silver', 20, 100), Treasure('platinum', 10, 400),
Treasure('paladium',10,800), Treasure('diamonds',5,900),
Treasure('gold', 10,60)]
## map(lambda x: Treasure(*x), [('silver',20,100), ... ])
def plunder(treasure_list,bag=None):
_bag = bag or Bag()
treasures = sorted(treasure_list,
key = lambda x: x.ratio,
reverse = True)
while True:
for treasure in treasures:
try: _bag.add(treasure)
except BagFullError as e:
print(e)
return _bag
bag = Bag("Adam",100)
print(bag)
plunder(treasure,bag)
print(bag)
print("Total Value: {}".format(sum(item.value for item in bag.contents)))