Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 How to add an element such as an average to a multi-dimensional array? xhughesey Programmer named Tim Posts: 6 Threads: 1 Joined: Jan 2019 Reputation: 0 Likes received: 0 #1 Jan-06-2019, 07:30 PM I am working on my first project with machine learning and training AI with rewards an loses on a game of Tic Tac Toe. I have everything but this problem solved on paper. I can't figure out how I can add the AI's moves to an array and give them a reward. The way I wanted to do this was use a huge array that stores all the information in a format of whether they started or their opponent started (1 or 2) then they move number (1, 2, 3, 4, and sometimes 5) and then add which square they chose and at the end either add 1 for a win or subtract 1 for a loss. This is how I thought I should format my code. ```#1, 2, 3 #4, 5, 6 #7, 8, 9 oBoard = [] #Sets 1 for when they start and 2 for when opponent starts for sTurnNum in range(2): oBoard.append([sTurnNum+1]) for turnNum in range(5): #Sets an empty set for every possible turn 1-5 oBoard[sTurnNum].append([turnNum+1]) ```When I print this I get ``Output:[[1, [1], [2], [3], [4], [5]], [2, [1], [2], [3], [4], [5]]]`` and I can add what block they choose. If I simulate the game I get where the bot starts first: ``````Output: [[1, [1, [5]], [2], [3], [4], [5]]] Bot chooses space 5, player chooses space 1 [[1, [1, [5]], [2, [5, [7]]], [3], [4], [5]]] Bot chooses space 7, player chooses space 8 [[1, [1, [5]], [2, [5, [7]]], [3, [5, [7, [3]]]], [4], [5]]] Bot gets 3 in a row, 3-5-7. Set of moves should get rewarded 1 so [[1, [1, [5, [total="1"]], [2, [5, [7, [total="1"]]]], [3, [5, [7, [3, [total="1"]]]]], [4], [5]]] Then be able to get that total to see if the bot should pick that space if it is a good total or not once it gets past its learning trail `````` micseydel Involuntary Spiderweb Collector Posts: 2,158 Threads: 55 Joined: Sep 2016 Reputation: 59 Likes received: 644 #2 Jan-06-2019, 07:34 PM What is your question? Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more. Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :) xhughesey Programmer named Tim Posts: 6 Threads: 1 Joined: Jan 2019 Reputation: 0 Likes received: 0 #3 Jan-06-2019, 07:46 PM (This post was last modified: Jan-06-2019, 08:19 PM by xhughesey. Edited 2 times in total.) (Jan-06-2019, 07:34 PM)micseydel Wrote: What is your question? It is at the end of the output. I accidentally included it in there. I want to add an element or tag to the end of the array of numbers for each value so I can then go back and gather that information so I can use it to determine what the best move would be. Here is my github repository: https://github.com/xhughesey/TicTacToe Gribouillis Posts: 2,198 Threads: 22 Joined: Jan 2018 Reputation: 205 Likes received: 528 #4 Jan-06-2019, 08:19 PM (This post was last modified: Jan-06-2019, 08:20 PM by Gribouillis. Edited 2 times in total.) Why don't you store the game in a simple form such as `[5, -1, 7, -8, 3, "bot wins"]`? xhughesey Programmer named Tim Posts: 6 Threads: 1 Joined: Jan 2019 Reputation: 0 Likes received: 0 #5 Jan-06-2019, 08:34 PM (This post was last modified: Jan-06-2019, 08:34 PM by xhughesey. Edited 1 time in total.) (Jan-06-2019, 08:19 PM)Gribouillis Wrote: Why don't you store the game in a simple form such as `[5, -1, 7, -8, 3, "bot wins"]`? ...I always over complicate things. The only thing with that is I need to be able to see how many times it won and lost at starting move 5. So if I had: ```[5, -1, 7, -8, 3, "bot wins"] [5, -1, 3, -8, 7, "bot wins"] [5, -2, 4, -1, 6, "bot wins"] [5, -1, 7, -2, 3, -3, "bot loses"] ```and I was determining if it was a good spot I need to pull that data into something like this: ```# [Move number, move position, reward] [1, 5, 3/4] ```So I guess my question would be; How can I pull something from the end of multiple arrays with the same starting integer and put that result into an array? Gribouillis Posts: 2,198 Threads: 22 Joined: Jan 2018 Reputation: 205 Likes received: 528 #6 Jan-06-2019, 09:35 PM (This post was last modified: Jan-06-2019, 09:36 PM by Gribouillis. Edited 1 time in total.) xhughesey Wrote:So I guess my question would be; How can I pull something from the end of multiple arrays with the same starting integer and put that result into an array? The question is surprising because it looks like something that is much easier than the rest of the code in the git repo. Why not ```games = [ [5, -1, 7, -8, 3, "bot wins"], [5, -1, 3, -8, 7, "bot wins"], [5, -2, 4, -1, 6, "bot wins"], [5, -1, 7, -2, 3, -3, "bot loses"],] wons = sum(x[-1] == 'bot wins' for g in games) result = [1, 5, wons/len(games)] ``` xhughesey Programmer named Tim Posts: 6 Threads: 1 Joined: Jan 2019 Reputation: 0 Likes received: 0 #7 Jan-06-2019, 10:47 PM (This post was last modified: Jan-06-2019, 10:48 PM by xhughesey. Edited 1 time in total.) (Jan-06-2019, 09:35 PM)Gribouillis Wrote: xhughesey Wrote:So I guess my question would be; How can I pull something from the end of multiple arrays with the same starting integer and put that result into an array? The question is surprising because it looks like something that is much easier than the rest of the code in the git repo. Why not ` games = [ [5, -1, 7, -8, 3, "bot wins"], [5, -1, 3, -8, 7, "bot wins"], [5, -2, 4, -1, 6, "bot wins"], [5, -1, 7, -2, 3, -3, "bot loses"],] wons = sum(x[-1] == 'bot wins' for g in games) result = [1, 5, wons/len(games)] ` That was my fault. I was kind of asking myself then I was going to answer myself on here. I figured it out and implemented it. I just haven't committed it yet. Thanks a lot though I was thinking way to hard about the previous question. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post I am trying to change the value of an element in a record array ingu 1 97 Jan-14-2020, 01:30 PM Last Post: perfringo TensorFlow get error - array with more than one element is ambiguous vokoyo 3 327 Nov-07-2019, 01:12 PM Last Post: ThomasL How convert multidimensional array to two dimensional array tkkhan44 1 535 Feb-20-2019, 05:00 AM Last Post: scidam Three-dimensional Contour Plots minifizikus 1 1,086 Sep-13-2018, 10:56 PM Last Post: Larz60+ Convert element of list to integer(Two dimentional array) zorro_phu 3 1,007 Jun-12-2018, 04:49 AM Last Post: zorro_phu 2 Dimensional NumPy for beginners Jack_Sparrow 2 982 May-08-2018, 05:21 PM Last Post: killerrex Recurse through n-dimensional array ColdDeath 2 996 Apr-05-2018, 11:20 AM Last Post: KenniT ValueError: The truth value of an array with more than one element is ambiguous. Eliza5 1 11,049 Apr-02-2018, 12:03 AM Last Post: scidam Two Dimensional Chart from CSV File srini1995 0 841 Nov-27-2017, 07:10 AM Last Post: srini1995 How to sum elements of same position in n dimensional array Felipe 2 1,728 May-11-2017, 10:33 AM Last Post: Felipe

Forum Jump:

Users browsing this thread: 1 Guest(s)