Best Python code snippet using pyatom_python
search.py
Source:search.py
1from queue import Queue, PriorityQueue2from sys import maxsize3from stats import Stats4def breadthSearch(ini):5 stats = Stats()6 states = Queue()7 states.put(ini)8 searchedStates = set()9 stats.startTimer()10 while not states.empty():11 stateToAnalyze = states.get()12 if stateToAnalyze in searchedStates:13 stats.nodeSkipped()14 continue15 searchedStates.add(stateToAnalyze)16 stats.nodeExpanded()17 for nextState in stateToAnalyze.getAllStates():18 if nextState in searchedStates:19 stats.nodeSkipped()20 continue21 if nextState.isFinal():22 stats.endTimer()23 return stats.setState(nextState)24 states.put(nextState)25def depthSearch(ini, maxDepth=maxsize):26 stats = Stats()27 states = [ini]28 searchedStates = set()29 stats.startTimer()30 while not (len(states) == 0):31 stateToAnalyze = states.pop()32 if stateToAnalyze in searchedStates:33 stats.nodeSkipped()34 continue35 searchedStates.add(stateToAnalyze)36 stats.nodeExpanded()37 for nextState in stateToAnalyze.getAllStates():38 if nextState in searchedStates:39 stats.nodeSkipped()40 continue41 if nextState.isFinal():42 stats.endTimer()43 return stats.setState(nextState)44 if stateToAnalyze.depth() < maxDepth:45 states.append(nextState)46def pSearchStep(ini, stats, maxDepth):47 states = [ini]48 searchedStates = set()49 while not (len(states) == 0):50 stateToAnalyze = states.pop()51 if (stateToAnalyze, stateToAnalyze.depth()) in searchedStates:52 stats.nodeSkipped()53 continue54 if stateToAnalyze.isFinal():55 stats.endTimer()56 return stats.setState(stateToAnalyze)57 searchedStates.add((stateToAnalyze, stateToAnalyze.depth()))58 stats.nodeExpanded()59 if stateToAnalyze.depth() >= maxDepth:60 continue61 for nextState in stateToAnalyze.getAllStates():62 if (nextState, nextState.depth()) in searchedStates:63 stats.nodeSkipped()64 continue65 states.append(nextState)66def progressiveDepth(ini):67 stats = Stats()68 sol = None69 depth = 070 stats.startTimer()71 while sol is None:72 depth += 173 sol = pSearchStep(ini, stats, depth)74 return sol75def informedSearch(ini):76 stats = Stats()77 states = PriorityQueue()78 states.put(ini)79 searchedStates = set()80 stats.startTimer()81 while True:82 stateToAnalyze = states.get()83 if stateToAnalyze.isFinal():84 stats.endTimer()85 return stats.setState(stateToAnalyze)86 if stateToAnalyze in searchedStates:87 stats.nodeSkipped()88 continue89 searchedStates.add(stateToAnalyze)90 stats.nodeExpanded()91 for nextState in stateToAnalyze.getAllStates():92 if nextState in searchedStates:93 stats.nodeSkipped()94 continue...
States.py
Source:States.py
1from ThoughtXplore.txMisc.models import MiscState ,StateContentType2from ThoughtXplore.CONFIG import LOGGER_MISC3from ThoughtXplore.txMisc.DBFunctions.DatabaseFunctions import DBInsertState4from ThoughtXplore.txMisc.DBFunctions.DBMessages import decode5import logging6class StatesClass():7 def __init__(self):8 self.MiscLogger = logging.getLogger(LOGGER_MISC)9 #CRUD FUNCTIONS10 11 def CreateState(self,name,desc,by,ip):12 try:13 self.MiscLogger.debug('inside CreateState')14 details = {15 'ip':ip,16 'by':by,17 'name':name,18 'desc':desc,19 }20 result = DBInsertState(details)21 self.MiscLogger.debug('[%s] %s,%s'%('CreateState',str(details),str(result)))22 return (result,decode(int(result['result']), result['rescode']))23 except:24 exception_log = ('[%s] %s,%s,%s,%s')%('CreateState',name,desc,by,ip)25 self.MiscLogger.exception(exception_log)26 return (-1,'Exception Occoured at Business Functions while creating group')27 def getAllStates(self):28 try:29 states_list = MiscState.objects.all()30 self.MiscLogger.debug("[%s] length of stateslist is %s"%('getAllStates',str(len(states_list))))31 return (1,states_list)32 except:33 self.MiscLogger.exception('[%s] == Exception =='%('getAllStates'))34 return (-1,[])35 36 def getAllStatesForAContentTypeBYID(self,cid):37 try:38 states_list = MiscState.objects.filter(statecontenttype__StateContentType__id=cid)39 self.MiscLogger.debug("[%s] length of states list is %s"%('getAllStatesForAContentTypeBYID',str(len(states_list))))40 return (1,states_list)41 except:42 self.MiscLogger.exception('[%s] == Exception == cid = %s'%('getAllStatesForAContentTypeBYID',cid))43 return (-1,[])44 45 46 def getAllStatesForAContentTypeBYName(self,app_label_t,model_t):47 try:48 states_list = StateContentType.objects.filter(StateContentType__app_label=app_label_t,StateContentType__model= model_t)49 self.MiscLogger.debug("[%s] length of states list is %s"%('getAllStatesForAContentTypeBYName',str(len(states_list))))50 return (1,states_list)51 except:52 self.MiscLogger.exception('[%s] == Exception == applabel = %s, model = %s'%('getAllStates',app_label_t,model_t))...
Exploration.py
Source:Exploration.py
1import numpy as np2import random3import Game4def getAllStates(state):5 state_list = []6 invalid_list = []7 for i in range(4):8 next_state = Game.transition(state, i)9 state_list.insert(i, next_state)10 invalid_list.insert(i, not next_state.valid)11 return state_list, invalid_list12def softmax(action, allQ, i, epsilon, state):13 original_action = action14 # Boltzman approach15 rand_action = False16 logits = allQ/epsilon(i)17 logits = np.exp(logits)18 logits_sum = np.sum(logits)19 prob = logits/logits_sum20 action = np.random.choice([0, 1, 2, 3], p=prob[0])21 state_list, invalid_list = getAllStates(state)22 nextstate = state_list[action]23 if not nextstate.halt:24 while not nextstate.valid:25 while invalid_list[action]:26 action = np.random.choice([0, 1, 2, 3], p=prob[0])27 nextstate = state_list[action]28 if action != original_action:29 rand_action = True30 return state_list, action, rand_action, invalid_list[original_action]31def egreedy(action, allQ, i, epsilon, state):32 original_action = action33 random_action = False34 policy_action = 035 sorted_action = np.argsort(-np.array(allQ))[0]36 if np.random.rand(1) < epsilon(i):37 action = random.randint(0, 3)38 random_action = True39 state_list, invalid_list = getAllStates(state)40 nextstate = state_list[action]41 if not nextstate.halt:42 while not nextstate.valid:43 if random_action:44 b = action45 while b == action:46 b = random.randint(0, 3)47 action = b48 else: # ignore invalid action49 policy_action += 150 action = sorted_action[policy_action]51 nextstate = state_list[action]52 return state_list, action, random_action, invalid_list[original_action]53def getExplorationFromArgs(args):54 if args == "egreedy":55 return egreedy56 if args == "softmax":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!