Python Forum
How to add an element such as an average to a multi-dimensional array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add an element such as an average to a multi-dimensional array?
#1
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
Reply
#2
What is your question?
Reply
#3
(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
Reply
#4
Why don't you store the game in a simple form such as
[5, -1, 7, -8, 3, "bot wins"]
?
Reply
#5
(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?
Reply
#6
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)]
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  RandomForest --ValueError: setting an array element with a sequence JaneTan 0 1,707 Sep-08-2021, 02:12 AM
Last Post: JaneTan
  I am trying to change the value of an element in a record array ingu 1 2,130 Jan-14-2020, 01:30 PM
Last Post: perfringo
  TensorFlow get error - array with more than one element is ambiguous vokoyo 3 5,497 Nov-07-2019, 01:12 PM
Last Post: ThomasL
  How convert multidimensional array to two dimensional array tkkhan44 1 2,732 Feb-20-2019, 05:00 AM
Last Post: scidam
  Three-dimensional Contour Plots minifizikus 1 3,243 Sep-13-2018, 10:56 PM
Last Post: Larz60+
  Convert element of list to integer(Two dimentional array) zorro_phu 3 4,625 Jun-12-2018, 04:49 AM
Last Post: zorro_phu
  2 Dimensional NumPy for beginners Jack_Sparrow 2 3,066 May-08-2018, 05:21 PM
Last Post: killerrex
  Recurse through n-dimensional array ColdDeath 2 2,925 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 14,279 Apr-02-2018, 12:03 AM
Last Post: scidam
  Two Dimensional Chart from CSV File srini1995 0 2,204 Nov-27-2017, 07:10 AM
Last Post: srini1995

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020