In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Python algorithm technology". In daily operation, I believe many people have doubts about how to use Python algorithm technology. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Python algorithm technology". Next, please follow the editor to study!
Development tools
Python version: 3.6.4
Related modules:
Numpy module
Argparse module
Pygame module
And some modules that come with python.
Environment building
Install Python and add it to the environment variable, and pip installs the relevant modules you need.
Brief introduction of principle
Genetic algorithm, that is:
Genetic Algorithm, GA is a meta-heuristic algorithm, whose core idea is very similar to Darwin's evolution theory. To put it simply, in the process of evolution, good genes will be retained and bad genes will be eliminated. After many generations of evolution, the genes currently retained by the species can be regarded as the genes that are most adaptable to the current environment.
Applied to our little dinosaur Mini Game, the idea of our algorithm design is as follows. First, randomly generate several small dinosaurs (for example, 100):
Self.dinos = [Dinosaur (cfg.IMAGE_PATHS ['dino']) for _ in range (self.population_size)]
The actions of each dinosaur are controlled by a small neural network:
Self.populations = [Network () for _ in range (self.population_size)]
Among them, each neural network is composed of two fully connected layers, and their weight matrices are randomly generated.
'' define the network'''class Network (): def _ init__ (self, fc1=None, fc2=None, * * kwargs): self.fc1 = np.random.randn (5,16) if fc1 is None else fc1 self.fc2 = np.random.randn (16,2) if fc2 is None else fc2 self.fitness = 0''predict the action''' def predict (self) X): X = x.dot (self.fc1) x = self.activation (x) x = x.dot (self.fc2) x = self.activation (x) return x 'activation function''' def activation (self, x): return 0.5 * (1 + np.tanh (0.5 * x))
The output of each fully connected layer is activated by the following function to ensure that the output values range from 0 to 1:
There are 5 input values for the network, namely:
At present, the horizontal distance between the obstacle closest to the little dinosaur and the little dinosaur
The current height of the nearest obstacle to the dinosaur from the ground
The current width of the obstacle closest to the little dinosaur
The current height of the obstacle closest to the little dinosaur
The horizontal running speed of a small dinosaur.
Draw a diagram as follows:
Then, we let these neural networks control the corresponding little dinosaurs to play the game:
'' makedecision for all dinos'''def makedecision (self) X): threshold = 0.55 actions = self.ai.predict (x) for i in range (len (actions)): action = actions [I] if self.dinos [I] .is _ dead: continue if action [0] > = threshold: self.dinos.duck (self.sounds) elif action [1] > = threshold: self.dinosi [I] .duck () else: self.dinos.unduck () self.ai.populations [I] .score = self.dinos.score
Until all the neural networks let the little dinosaurs they control die when they hit an obstacle on the road. Next, from these neural networks, we select the ones that keep the little dinosaurs alive for the longest time (for example, choose two, that is, the two with the highest scores for the corresponding control of small dinosaurs):
Def keepbest (self): self.populations.sort (key=lambda x: x.fitness, reverse=True) self.keeped_nets = self.populations [: self.num_keeped_nets]
The weight matrix of the selected neural network is crossed and mutated to generate a new batch of neural networks:
'' crossover'''def crossover (self): def crossoverweight (fc1, fc2): assert len (fc1) = len (fc2) crossover_len = int (len (fc1) * random.uniform (0,1)) for i in range (crossover_len): fc1 [I], fc2 [I] = fc2 [I], fc1 [I] return fc1, fc2 nets_new = [] size = min (self.num_keeped_nets * self.num_keeped_nets) Self.population_size) for _ in range (size): net_1 = copy.deepcopy (random.choice (self.keeped_nets)) net_2 = copy.deepcopy (random.choice (self.keeped_nets)) for _ in range (self.num_crossover_times): net_1.fc1, net_2.fc1 = crossoverweight (net_1.fc1, net_2.fc1) net_1.fc2, net_2.fc2 = crossoverweight (net_1.fc2 Net_2.fc2) nets_new.append (net_1) return nets_new'''mutate'''def mutate (self, net): def mutateweight (fc, prob): if random.uniform (0,1) < prob: return fc * random.uniform (0.5,1.5) else: return fc net = copy.deepcopy (net) net.fc1 = mutateweight (net.fc1, self.mutation_prob) net.fc2 = mutateweight (net.fc2) Self.mutation_prob) return net
Similarly, each of these neural networks will control a new little dinosaur to play the game until all the small dinosaurs under the control of the new neural network die again. At this point, repeat the previous action, that is, select some of the best-performing neural networks and cross-mutate, and then start a new game again and again until satisfactory results are obtained.
At this point, the study on "how to use Python algorithm technology" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.