Bonjour,
J'ai essayer d'implémenter MinMax et NegaMax mais aucun des deux ne fonctionne.
Je viens voir si vous auriez la possibilité de revoir mes codes.
Si vous avez besoin de précision, je reste derrière mon pc.
C'est écrit avec Ada, j'ai suivi le pseudocode de Wikipedia et d'autres.
Un MinMax avec élagage alpha béta non récursif de ma conception.
jovi est le joueur adverse, jova le joueur courant, MINIMAX appelle jovi qui lui appelle jova et lui même jovi jusqu'a la profondeur voulue.
Code ada : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 function jovi (Game : in Game_Type; Depth : in Natural; Advers : in Game_Type; Alpha : in Float; Beta : in float) return Float is V_Min : Float := Beta; Hcost : Float := game.Heuristic(Game.Usr, Game.pred); begin if Game.Depth > Depth then declare V_Fils : Float := Float'Last; Successors : Successors_Type := advers.Successors(game.Usr); New_Game : Game_Type := Game; begin New_Game.Pred := Game.Usr; if Successors.Successor /= 0 then for Succ in 1..Successors.Successor loop New_Game.Usr := Successors.Plan(Succ); V_Fils := Float'Min (V_Fils, jova(New_Game, Depth, Advers, Alpha, V_min)); if Alpha >= V_Fils Then return V_Fils; else V_Min := Float'Min (V_Min, V_Fils); end if; end loop; end if; end; else V_Min := Hcost; end if; return V_Min; end; function jova (Game : in Game_Type; Depth : in Natural; Advers : in Game_Type; Alpha : in Float; Beta : in float) return Float is V_Max : Float := Alpha; Hcost : Float := advers.Heuristic(Game.Usr, Game.pred); begin if game.Depth > Depth then declare V_Fils : float := Float'First; Successors : Successors_Type := game.Successors(Game.Usr); New_Game : Game_Type := game; begin New_Game.Pred := Game.Usr; if Successors.Successor /= 0 then for Succ in 1..Successors.Successor loop New_Game.Usr := Successors.Plan(Succ); V_Fils := Float'Max(V_Fils, jovi(New_Game, Depth+1, Advers, V_Max, beta)); if V_Fils >= Beta then return V_Fils; else V_Max := Float'Max(V_Max,V_Fils); end if; end loop; end if; end; else V_Max := Hcost; end if; return V_Max; end; function MINIMAX (player : in Game_Type; Advers : in Game_Type) return Usr_Type is Maxime : Float := Float'First; Successors : Successors_Type := Player.Successors(Player.Usr); Coup : Usr_Type; New_Game : Game_Type := Player; begin New_Game.pred := Advers.Usr; if Successors.Successor /= 0 then for Succ in 1..Successors.Successor loop New_Game.Usr := Successors.Plan(Succ); declare Val_Fils : Float := Jovi(New_Game, 0, Advers, Float'First, Float'last); begin if Val_Fils > Maxime then Put_Line("MINIMAX : " & Float'Image(Val_fils)); Maxime := Val_Fils; Coup := Successors.Plan(Succ); end if; end; end loop; end if; return coup; end;
NegaMax, tout bètement l'algo de Wikipedia version english. jovijova appelle NégaMax, peut-être que mon erreur est là.
Code ada : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 function NegaMax (player : in Game_Type; Advers: in Game_Type; Depth : in Natural; A, B : in float) return float is Alpha : Float := A; Beta : Float := B; begin if player.Depth <= Depth then return player.Heuristic(Player.Usr, player.pred); else declare V_Fils : Float := 0.0; Best : Float := Float'First; New_Game : Game_Type := Advers; Successors : Successors_Type := player.Successors(Player.Usr); begin New_Game.Pred := Player.Usr; for Succ in 1..Successors.Successor loop New_Game.Usr := Successors.Plan(Succ); V_Fils := -NegaMax(New_Game, Player, Depth+1, -Beta, -alpha); if V_Fils >= beta then return V_Fils; elsif V_fils >= Alpha then Alpha := V_Fils; end if; end loop; return alpha; end; end if; end negaMax; function jovijova(player : in Game_Type; Advers : in Game_Type) return Usr_Type is Maxime : Float := Float'First; Successors : Successors_Type := Player.Successors(Player.Usr); Coup : Usr_Type; New_Game : Game_Type := Player; begin New_Game.pred := Advers.Usr; if Successors.Successor /= 0 then for Succ in 1..Successors.Successor loop New_Game.usr := Successors.Plan(Succ); declare Val_Fils : Float := negamax(New_Game, player, 0, Float'First, Float'last); begin if Val_Fils > Maxime then Put_Line("NegaMax : " & Float'Image(Val_fils)); Maxime := Val_Fils; Coup := Successors.Plan(Succ); end if; end; end loop; end if; return coup; end Jovijova;
Pour les deux fonction un type Game_Type fournit les opérations "successeurs" et "heuristic".
Mon problème est un non respect des regles du jeu d'échecs lors d'un echec.
Merci pour votre aide, s'il vous plaît.
Partager