Guest User

AI_DLA_IDJOTOW.PY

a guest
Sep 17th, 2017
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. from tensorforce import Configuration
  2. from tensorforce.agents import TRPOAgent
  3. from tensorforce.core.networks import layered_network_builder
  4. import random
  5.  
  6. # Przyklad "nauczania ze wzmocnieniem" na podstawie frameworka Tensorforce: https://github.com/reinforceio/tensorforce/
  7.  
  8. # siec uczy sie "obstawiac" wyniki prostego generatora losowego.
  9. # Dostaje punkty za postawienie 0 lub 1 jesli generator wylosuje wartosc<5
  10. # Za jak nawyzszy "zaklad" przy najwiekszej liczbie dostaje max punktow czyli 10
  11. # Po 8192 loopach skutecznosc sieci to 100%:)
  12. # Tych parametrow ponizej jeszcze nie rozumiem :F
  13.  
  14.  
  15. config = Configuration(
  16.     batch_size=10,
  17.     states=dict(shape=(2,)),
  18.     actions=dict(continuous=False, num_actions=4),
  19.     network=layered_network_builder([dict(type='dense', size=3)])
  20. )
  21.  
  22. class MyClass:
  23.     def __init__(self):
  24.         self.state=[random.randint(0,10),0] # Nie wiem czemu musza byc 2 wymiary, inaczej nie dziala :/
  25.     def get_state(self): # Czy inaczej, wylosuj()
  26.         return self.state
  27.    
  28.     def execute(self,action): # Funkcja "uczaca"
  29.         #print("\texecute:",action)
  30.         if(self.state[0]<5 and action<=1):
  31.             return 10 # Nagroda 10
  32.         if(self.state[0]>=9):
  33.             return random.randint(8,10) # obstawiamy
  34.         if(self.state[0] >=5 and action>=3 and action<=9):
  35.             return random.randint(6,8)
  36.         else:
  37.             return 0
  38.  
  39. agent = TRPOAgent(config=config)
  40.  
  41. for i in range(8192):
  42.     client = MyClass()
  43.  
  44.     state = client.get_state()
  45.     print("GENERATOR:",state)
  46.     # Get prediction from agent, execute
  47.     action = agent.act(state=state)
  48.     print("(AI)STAWIAM:",action)
  49.     reward = client.execute(action)
  50.     print("NAGRODA:",reward)
  51.     # Add experience, agent automatically updates model according to batch size
  52.     agent.observe(reward=reward, terminal=True)
Add Comment
Please, Sign In to add comment