Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Projet_info
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
TRAN Alain
Projet_info
Commits
7947fda7
Commit
7947fda7
authored
Apr 21, 2020
by
CRESCENCE Cassandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
b1ca7639
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
129 additions
and
0 deletions
+129
-0
AlgoRechercheAleatoire.java
src/mcts_cassandre/AlgoRechercheAleatoire.java
+129
-0
No files found.
src/mcts_cassandre/AlgoRechercheAleatoire.java
0 → 100644
View file @
7947fda7
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package
tictactoecodingame
;
import
java.lang.Math
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Random
;
import
tictactoecodingame.Plateau
;
import
tictactoecodingame.Coup
;
/**
*
* @author franck.tempet
*/
public
class
AlgoRechercheAleatoire
extends
AlgoRecherche
{
Random
rnd
;
public
AlgoRechercheAleatoire
()
{
rnd
=
new
Random
();
}
// PHASE DE SELECTION
public
static
double
uctValue
(
int
totalVisit
,
double
nodeWinScore
,
int
nodeVisit
)
{
// méthode pour calculer UCT du noeud enfant d'un noeud parent donné
if
(
nodeVisit
==
0
){
return
Integer
.
MAX_VALUE
;
}
return
nodeWinScore
/((
double
)
nodeVisit
)
+
1.41
*
Math
.
sqrt
((
double
)
Math
.
log
(
(
double
)
(
totalVisit
/
nodeVisit
)));
}
public
Node
selectNodeWithBestUCT
(
Node
parent
){
// méthode pour sélectionner le noeud enfant qui a le plus grand UCT
double
max
=
Integer
.
MIN_VALUE
;
int
totalVisit
=
parent
.
getState
().
getVisitCount
();
for
(
Node
child
:
parent
.
getChildArray
()){
double
uctValue
=
uctValue
(
totalVisit
,
child
.
getState
().
getWinScore
(),
child
.
getState
().
getVisitCount
()
);
int
i
=
0
;
if
(
uctValue
>
max
){
// sélection du noeud possédant le meilleur uct et paramètrage de celui-ci
max
=
uctValue
;
Node
selectedNode
=
child
;
selectedNode
.
setParent
(
parent
);
parent
.
getChildArray
().
remove
(
child
);
selectedNode
.
setChildArray
(
parent
.
getChildArray
());
parent
.
getChildArray
().
add
(
child
);
selectedNode
.
setCoupAssocie
(
parent
.
getState
().
getAllPossibleStates
().
get
(
i
));
i
=
i
+
1
;
}
}
return
selectedNode
;
}
// PHASE D'EXPANSION
public
static
void
expandNode
(
Node
node
,
Tree
tree
){
State
newState
=
new
State
(
node
.
getState
().
getPlateau
(),
node
.
getParent
().
getState
().
getJoueur
(),
0
,
0
);
node
.
setState
(
newState
);
tree
.
getTree
().
add
(
node
);
}
// PHASE DE SIMULATION
public
boolean
simulationFromNode
(
Node
node
,
boolean
_ponder
){
// A FINIR
State
state
=
node
.
getState
();
Joueur
joueur
=
state
.
getJoueur
();
state
.
getPlateau
().
joueCoup
(
node
.
getCoupAssocie
());
state
.
getPlateau
().
sauvegardePosition
(
0
);
int
i
=
1
;
if
(
state
.
getPlateau
().
partieTerminee
()
!=
true
){
ArrayList
<
Coup
>
coups
=
state
.
getPlateau
().
getListeCoups
(
joueur
);
Coup
coupAleatoire
=
coups
.
get
(
rnd
.
nextInt
(
coups
.
size
()));
state
.
getPlateau
().
joueCoup
(
coupAleatoire
);
state
.
getPlateau
().
sauvegardePosition
(
i
);
i
=
i
+
1
;
}
else
{
if
(
state
.
getPlateau
().
vainqueur
()
==
joueur
){
return
true
;
}
if
(
state
.
getPlateau
().
vainqueur
()
!=
joueur
){
return
false
;
}
for
(
int
k
=
1
;
k
<=
i
;
k
--){
state
.
getPlateau
().
restaurePosition
(
k
);
}
}
}
// PHASE DE BACKPROPAGATION
public
static
void
backpropagation
(
Tree
tree
,
boolean
resultatSimulation
){
for
(
Node
node:
tree
.
getTree
()
){
int
a
=
node
.
getState
().
getVisitCount
();
node
.
getState
().
setVisitCount
(
a
+
1
);
if
(
resultatSimulation
==
true
){
node
.
getState
().
setWinScore
(
node
.
getState
().
getWinScore
()
+
1
);
}
}
}
@Override
public
Coup
meilleurCoup
(
Plateau
_plateau
,
Joueur
_joueur
,
boolean
_ponder
)
{
ArrayList
<
Coup
>
coups
=
_plateau
.
getListeCoups
(
_joueur
);
return
coups
.
get
(
rnd
.
nextInt
(
coups
.
size
()));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment