Esempio n. 1
0
    def __init__(self, dsl=None):
        if dsl is None:
            dsl = DSL()

        self._dsl = DSL()
        self._non_terminal_symbols = self._dsl._grammar.keys()
        self._rules = set()
        self.generate_rules()
    def get_action(self, state):
        actions = state.available_moves()

        for a in actions:

            if DSL.hasWonColumn(state) and DSL.isStopAction(a):
                self._counter_calls[0] += 1
                return a

        return actions[0]
    def get_action(self, state):
        actions = state.available_moves()
        print("Actions: ", actions)
        for a in actions:
            if DSL.actionWinsColumn(state, a):
                return a
            if DSL.isDoubles(a):
                return a
            if DSL.containsNumber(a, (6,2)):
                return a
            if DSL.isStopAction(a):
                return a

        return actions[0]
def ESZ():
    # parameters
    populationSize = 30
    numOfGene = 30
    numOfTour = 5
    numOfElite = 10
    numOfRounds = 10
    mutateRate = 0.5

    # init population
    dslExample = DSL()
    scripts = dslExample.generateRandomScript(populationSize)
    for i in scripts:
        i.saveFile('')
    scriptIdCounter = populationSize  # show the id of the next script
    numOfWinRule = []

    # evaluate
    for _ in range(numOfGene):
        numOfWinRule.append(count_winning_rule(scripts))
        print(count_winning_rule(scripts))
        Evaluate(scripts, numOfRounds)  # change the fitness numbers of scripts
        # get elites
        nextGene = elite(scripts, numOfElite)
        # clear fitness and call count
        for i in scripts:
            i.clearAttributes()
        while len(nextGene) < len(scripts):
            p1, p2 = tournament(scripts, numOfTour, numOfRounds)
            c = crossoverBackup(p1, p2, scriptIdCounter)
            while len(c.getRules()) == 0:
                c = crossoverBackup(p1, p2, scriptIdCounter)
            mutate(c, mutateRate, dslExample)
            c.saveFile('')
            scriptIdCounter += 1
            nextGene.append(c)
        scripts = deepcopy(nextGene)

    # keep the best scripts
    Evaluate(scripts, numOfRounds)
    sortedScripts = sorted(scripts, key=lambda x: x.getFitness(), reverse=True)
    winnerPath = 'winner/'
    if not os.path.exists(os.path.split(winnerPath)[0]):
        os.makedirs(os.path.split(winnerPath)[0])
    sortedScripts[0].saveFile(winnerPath)

    for i in range(numOfGene):
        print(
            "# of scripts containing winning rule in generation " + str(i) +
            ": ", str(numOfWinRule[i]))
Esempio n. 5
0
    def get_action(self, state):
        actions = state.available_moves()

        if DSL.isYNAction(actions):
            if DSL.hasAvailableNeuralMarker(state):
                return 'y'
            if DSL.hasWonColumn(state):
                return 'n'
            if DSL.continueBecausehighProbNotBust(state):
                return 'y'
            return random.choice(actions)
        else:
            if DSL.hasAvailableNeuralMarker(state):
                actions = deepcopy(DSL.actionsUseLessMarker(state, actions))

            for a in actions:
                if DSL.isDoubles(a):
                    return a

            for a in actions:
                if DSL.actionWinsColumn(state, a):
                    return a

            # should be later than detect the already progressed one (use of neutral marker)
            # newActions = []
            # if DSL.hasAvailableNeuralMarker(state):
            #     for a in actions:
            #         if DSL.actionEasyToPrograss(state, a):
            #             newActions.append(a)
            # if newActions:
            #     actions = deepcopy(newActions)

            return random.choice(actions)
 def get_action(self, state):
     actions = state.available_moves()
     
     for a in actions:
     
         if DSL.containsNumber(a,5):
             self._counter_calls[0] += 1
             return a
                 
         if DSL.isStopAction(a) and DSL.isStopAction(a):
             self._counter_calls[1] += 1
             return a
                 
     return actions[0]
                 
Esempio n. 7
0
    def get_action(self, state):
        actions = state.available_moves()

        for a in actions:
            if DSL.isDoubles(a):
                return a
        return actions[0]
    def get_action(self, state):
        actions = state.available_moves()
        # print("Actions: ", actions)
        for a in actions:
            if DSL.isStopAction(a):
                return a

        return actions[0]
Esempio n. 9
0
def train_rule(trainingData):
    # init parameters
    newDsl = DSL()
    sampleNumber = 1000
    sampledRule = []

    prevRule = proposal_function_node(newDsl, 'B', 'S')
    prevRule.expand()
    prevScore = 0

    # sample "sampleNumber" times, create less samples than this number
    for i in range(sampleNumber):
        # generate a new sample
        # newRule.generatedRule is the newly sampled rule
        newRule = deepcopy(prevRule)
        error = 0
        allNodes = []
        allNodes += newRule.getAllChildNodes()
        mutateNode = random.choice(allNodes)
        mutateNode.mutate()
        newRule.geneRule()

        # evaluate possible new sample
        never_used = True
        for state_and_action in trainingData:
            state = state_and_action[0]
            chosen_action = state_and_action[1]
            used = False
            for a in state.available_moves():
                if eval(newRule.generatedRule):
                    used = True
                    never_used = False
                    if a != chosen_action:
                        error += 1
                    break
            if not used and state.available_moves()[0] != chosen_action:
                error += 1

        # decide sample or not
        if never_used:
            continue
        score = exp(-0.5 * error)
        if prevScore > 0 and score / prevScore < 1:
            ran_num = random.uniform(0.0, 1.0)
            if ran_num < score / prevScore:
                sampledRule.append((newRule, error))
                prevRule = deepcopy(newRule)
                prevScore = score
        else:
            sampledRule.append((newRule, error))
            prevRule = deepcopy(newRule)
            prevScore = score

    # best sampled rule is the rule with least errors
    bestRule = min(sampledRule, key=lambda x: x[1])
    return bestRule[0].generatedRule
    def get_action(self, state):
        actions = state.available_moves()
        for a in actions:
            if DSL.actionWinsColumn(state, a):
                return a
            if DSL.isDoubles(a):
                return a
            if DSL.containsNumber(a, 6) or DSL.containsNumber(a, 2):
                if np.random.choice([0, 1], p=[0.6, 0.4]) > 0:
                    return a
            if DSL.isStopAction(a):
                # Return 'y' with 70% probability and 'n' with 30% probability.
                if DSL.hasWonColumn(state, a):
                    return 'n'
                else:
                    return 'y'

        return actions[0]
Esempio n. 11
0
    def get_action(self, state):
        actions = state.available_moves()

        for a in actions:

            if DSL.hasWonColumn(state) and DSL.isStopAction(a):
                self._counter_calls[0] += 1
                return a

            if DSL.actionWinsColumn(state, a) and DSL.hasWonColumn(state):
                self._counter_calls[1] += 1
                return a

            if DSL.numberPositionsConquered(
                    state, 5) > 2 and DSL.containsNumber(a, 4):
                self._counter_calls[2] += 1
                return a

        return actions[0]
Esempio n. 12
0
                prev = prev.replace('B1', np.random.choice(dsl._grammar['B1']), 1)
            self.prev = prev  # This is the new rule.
            # print("prev is ->", self.prev)
            while 'SMALL_NUMBER' in prev:
                prev = prev.replace('SMALL_NUMBER', np.random.choice(dsl._grammar['SMALL_NUMBER']), 1)
            while 'NUMBER' in prev:
                prev = prev.replace('NUMBER', np.random.choice(dsl._grammar['NUMBER']), 1)
            updated_script = list()
            updated_script.append(prev)
            # print("updated_script is -> ", updated_script)
        else:
            new_script = self.prev
            # print("previous script is ->", new_script)
            while 'SMALL_NUMBER' in new_script:
                new_script = new_script.replace('SMALL_NUMBER', np.random.choice(dsl._grammar['SMALL_NUMBER']), 1)
            while 'NUMBER' in new_script:
                new_script = new_script.replace('NUMBER', np.random.choice(dsl._grammar['NUMBER']),1)
            updated_script = list()
            updated_script.append(new_script)
            # print("Newly updated script is -> ", updated_script)

        return list(updated_script)


# The following snippet of code is for testing purposes!
# The tree representation of the rules seem to be working fine

dsl = DSL()
tree = Tree(dsl)
for i in range(3):
    tree.change_tree()
Esempio n. 13
0
 def __init__(self):
     self.dsl = DSL()
     self.tree = Tree(self.dsl)
     self.path = "C:\\Users\\Rohan\\Desktop\\cant-stop-assignment\\created_scripts\\"
 def get_action(self, state):
     actions = state.available_moves()
     
     for a in actions:
     
         if DSL.isDoubles(a) and DSL.actionWinsColumn(state,a):
             self._counter_calls[0] += 1
             return a
                 
         if DSL.numberPositionsConquered(state,5)>0 and DSL.containsNumber(a,4):
             self._counter_calls[1] += 1
             return a
                 
         if DSL.isStopAction(a) and DSL.numberPositionsConquered(state,5)>1 and DSL.containsNumber(a,6):
             self._counter_calls[2] += 1
             return a
                 
         if DSL.isStopAction(a) and DSL.numberPositionsProgressedThisRoundColumn(state,2)>2 and DSL.isStopAction(a):
             self._counter_calls[3] += 1
             return a
                 
         if DSL.isStopAction(a):
             self._counter_calls[4] += 1
             return a
                 
         if DSL.actionWinsColumn(state,a) and DSL.actionWinsColumn(state,a):
             self._counter_calls[5] += 1
             return a
                 
         if DSL.isStopAction(a):
             self._counter_calls[6] += 1
             return a
                 
     return actions[0]
                 
Esempio n. 15
0
    def synthesize(self):
        dsl = DSL()
        # part3 = PlayerPart3()

        indent_count = 1
        script = dsl.start
        line1 = script.replace(
            'S', np.random.choice(dsl._grammar['S'], p=[0.9, 0.1]))
        # line1 = line1 + ":"
        # print(line1)
        if line1 in ['']:
            line1 = False
            # print(line1)
            return [[' DSL.isDoubles(a) ']]
        while ('S' in line1):
            line1_rep = np.random.choice(dsl._grammar['S'], p=[0.4, 0.6])
            # if line1_rep == dsl._grammar['S'][0]:
            #     line1_rep = ": " + line1_rep
            line1 = line1.replace('S', line1_rep)
            # print(line1)

        # line2 = line1.replace('B', np.random.choice(dsl._grammar['B'], p=[0.5, 0.5]))
        # print(line2)
        line2_list = line1.split(' ')
        # print(line2_list)
        for line2_index in range(len(line2_list)):
            if line2_list[line2_index] == 'B':
                line2_list[line2_index] = np.random.choice(dsl._grammar['B'],
                                                           p=[0.5, 0.5])
        # print(' '.join(line2_list))
        line2 = ' '.join(line2_list)
        # print(line2)
        line3_list = line2.split(' ')
        print(line3_list)
        for line3_index in range(len(line3_list)):
            if line3_list[line3_index] == 'B1':
                line3_list[line3_index] = np.random.choice(dsl._grammar['B1'])
        # print(' '.join(line3_list))
        print(line3_list)
        for i in range(1, len(line3_list)):
            if (line3_list[i] in ['and', 'if']):
                line3_list[i] = ''

        print(' '.join(line3_list))
        line3 = ' '.join(line3_list)
        line4_list = line3.split(' ')
        # print(line4_list)

        for i in range(len(line4_list)):
            if (line4_list[i] == "NUMBER"):
                line4_list[i] = np.random.choice(dsl._grammar['NUMBER'])
        # print(' '.join(line4_list))

        for i in range((len(line4_list))):
            if (line4_list[i] == "SMALL_NUMBER"):
                line4_list[i] = np.random.choice(dsl._grammar['SMALL_NUMBER'])

        line4 = ' '.join(line4_list)
        line4 = line4.replace('if', '')
        # line5_list = line4.split('and')
        print(line4)
        rules_list = []
Esempio n. 16
0
    def get_action(self, state):
        actions = state.available_moves()
        
        for a in actions:

            if DSL.isStopAction(a) and DSL.hasWonColumn(state,a):
                return a

            if DSL.containsNumber(a, 4) and DSL.isDoubles(a):
                return a

            if DSL.containsNumber(a, 2) and DSL.containsNumber(a, 6):
                return a

            if DSL.containsNumber(a, 2) and DSL.containsNumber(a, 4):
                return a

            if DSL.containsNumber(a, 6) and DSL.containsNumber(a, 4):
                return a

            if DSL.containsNumber(a, 2) and DSL.isDoubles(a):
                return a

            if DSL.containsNumber(a, 6) and DSL.isDoubles(a):
                return a

            if DSL.containsNumber(a, 4):
                return a

            if DSL.containsNumber(a, 2):
                return a

            if DSL.containsNumber(a, 6):
                return a
        
        return actions[0]