/src/centurio/Child.java

https://github.com/jvandenbroeck/The-Turk · Java · 294 lines · 157 code · 24 blank · 113 comment · 0 complexity · d3307f8abc67a791376d16523e4141a0 MD5 · raw file

  1. package centurio;
  2. import java.util.LinkedList;
  3. import java.util.concurrent.locks.ReentrantReadWriteLock;
  4. import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
  5. import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
  6. /**
  7. * Centurio a General Game Player
  8. *
  9. * Copyright (C) 2008 Felix Maximilian Möller, Marius Schneider and Martin Wegner
  10. *
  11. * This file is part of Centurio.
  12. *
  13. * Centurio is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  14. *
  15. * Centurio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with Centurio. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * This class implements a struct for the combination between a state, a move to this
  21. * state and its heuristics.
  22. *
  23. * @author Felix Maximilian Möller
  24. * @author Marius Schneider
  25. * @author Martin Wegner
  26. * @version 1.0
  27. */
  28. public class Child {
  29. /**
  30. *
  31. */
  32. private final Node child_pointer; //pointer to the node, which will be reached with the move
  33. /**
  34. * a representation of a move
  35. */
  36. private final LinkedList<Object> move;
  37. /**
  38. * heuristic UCT value
  39. */
  40. private double[] Q_UCT = null;
  41. /**
  42. * visits local n(s,a)
  43. */
  44. private int visits_local = 0;
  45. /**
  46. * heuristic RAVE value
  47. */
  48. private double[] Q_RAVE = null;
  49. /**
  50. * m(s,a)
  51. */
  52. private int visits_move = 0;
  53. private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
  54. private final ReadLock readLock = readWriteLock.readLock();
  55. private final WriteLock writeLock = readWriteLock.writeLock();
  56. /**
  57. * Constructor
  58. * @param child_pointer reference to child object
  59. * @param move a available move in its state
  60. * @param NrOfPlayers number of players who play the actual game
  61. */
  62. public Child(Node child_pointer, LinkedList<Object> move, int NrOfPlayers)
  63. {
  64. this.child_pointer = child_pointer;
  65. this.move = move;
  66. // TODO Q_Werte höher initialisieren
  67. Q_UCT = new double[NrOfPlayers];
  68. Q_RAVE = new double[NrOfPlayers];
  69. }
  70. /**
  71. * returns a reference in regard to representing move of this object
  72. * @return a reference in regard to representing move of this object
  73. */
  74. public Node get_child_pointer()
  75. {
  76. return child_pointer;
  77. }
  78. /**
  79. * returns the representing move of this object
  80. * @return the representing move of this object
  81. */
  82. public LinkedList<Object> get_move()
  83. {
  84. return move;
  85. }
  86. /**
  87. * set a UCT-value for one player
  88. * @param Q_UCT
  89. * @param player_index
  90. */
  91. public void set_one_Q_UCT (double Q_UCT, int player_index)
  92. {
  93. try {
  94. writeLock.lock();
  95. this.Q_UCT[player_index] = Q_UCT;
  96. } finally {
  97. writeLock.unlock();
  98. }
  99. }
  100. /**
  101. * get one UCT-value for one player
  102. * @param player_index
  103. * @return one UCT-value for one player
  104. */
  105. public double get_one_Q_UCT (int player_index)
  106. {
  107. try {
  108. readLock.lock();
  109. return Q_UCT[player_index];
  110. } finally {
  111. readLock.unlock();
  112. }
  113. }
  114. /**
  115. * set all UCT-values new
  116. * @param Q_UCT double[]
  117. */
  118. public void set_all_Q_UCT (double[] Q_UCT)
  119. {
  120. try {
  121. writeLock.lock();
  122. this.Q_UCT = Q_UCT;
  123. } finally {
  124. writeLock.unlock();
  125. }
  126. }
  127. /**
  128. * get all UCT-values
  129. * @return all UCT-values
  130. */
  131. public double[] get_all_Q_UCT ()
  132. {
  133. try {
  134. readLock.lock();
  135. return Q_UCT.clone();
  136. } finally {
  137. readLock.unlock();
  138. }
  139. }
  140. /**
  141. * set a RAVE-value for one player
  142. * @param Q_RAVE double
  143. * @param player_index
  144. */
  145. public void set_one_Q_RAVE (double Q_RAVE, int player_index)
  146. {
  147. try {
  148. writeLock.lock();
  149. this.Q_RAVE[player_index] = Q_RAVE;
  150. } finally {
  151. writeLock.unlock();
  152. }
  153. }
  154. /**
  155. * get one RAVE-value for one player
  156. * @param player_index
  157. * @return one RAVE-value for one player
  158. */
  159. public double get_one_Q_RAVE (int player_index)
  160. {
  161. try {
  162. readLock.lock();
  163. return Q_RAVE[player_index];
  164. } finally {
  165. readLock.unlock();
  166. }
  167. }
  168. /**
  169. * set new all RAVE-values
  170. * @param Q_RAVE double[]
  171. */
  172. public void set_all_Q_RAVE (double[] Q_RAVE)
  173. {
  174. try {
  175. writeLock.lock();
  176. this.Q_RAVE = Q_RAVE;
  177. } finally {
  178. writeLock.unlock();
  179. }
  180. }
  181. /**
  182. * get all RAVE-values
  183. * @return all RAVE-values
  184. */
  185. public double [] get_all_Q_RAVE ()
  186. {
  187. try {
  188. readLock.lock();
  189. return Q_RAVE.clone();
  190. } finally {
  191. readLock.unlock();
  192. }
  193. }
  194. /**
  195. * set new the number of runs through this child
  196. * @param visits
  197. */
  198. public void set_visits_local(int visits)
  199. {
  200. try {
  201. writeLock.lock();
  202. visits_local = visits;
  203. } finally {
  204. writeLock.unlock();
  205. }
  206. }
  207. /**
  208. * get the number of runs through this child
  209. * @return the number of runs through this child
  210. */
  211. public int get_visits_local()
  212. {
  213. try {
  214. readLock.lock();
  215. return visits_local;
  216. } finally {
  217. readLock.unlock();
  218. }
  219. }
  220. /**
  221. * increase the number of runs through this child
  222. */
  223. public void inc_visits_local()
  224. {
  225. try {
  226. writeLock.lock();
  227. visits_local++;
  228. } finally {
  229. writeLock.unlock();
  230. }
  231. }
  232. /**
  233. * set new the number of selection of this move in regard to RAVE
  234. * @param visits
  235. */
  236. public void set_visits_move(int visits)
  237. {
  238. try {
  239. writeLock.lock();
  240. visits_move = visits;
  241. } finally {
  242. writeLock.unlock();
  243. }
  244. }
  245. /**
  246. * get the number of selection of this move in regard to RAVE
  247. * @return the number of selection of this move in regard to RAVE
  248. */
  249. public int get_visits_move()
  250. {
  251. try {
  252. readLock.lock();
  253. return visits_move;
  254. } finally {
  255. readLock.unlock();
  256. }
  257. }
  258. /**
  259. * increase the number of selection of this move in regard to RAVE
  260. */
  261. public void inc_visits_move()
  262. {
  263. try {
  264. writeLock.lock();
  265. visits_move++;
  266. } finally {
  267. writeLock.unlock();
  268. }
  269. }
  270. }