This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BuildOptimizer.cs
164 lines (138 loc) · 6.68 KB
/
BuildOptimizer.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using ShogunOptimizer.ArtifactSources;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace ShogunOptimizer
{
public class BuildOptimizer
{
public Build FindBestBuild(
ICollection<Weapon> weapons, ArtifactSource artifactSource, ReadOnlyDictionary<Type, object> configs,
Func<Build, double> evaluateBuild, Func<Build, bool> filterBuild = null)
{
Console.WriteLine($" - Creating {weapons.Count * artifactSource.Flowers.Count * artifactSource.Plumes.Count * artifactSource.Sands.Count * artifactSource.Goblets.Count * artifactSource.Circlets.Count} builds");
var builds = new List<Build>();
foreach (var w in weapons)
foreach (var f in artifactSource.Flowers)
foreach (var p in artifactSource.Plumes)
foreach (var s in artifactSource.Sands)
foreach (var g in artifactSource.Goblets)
foreach (var c in artifactSource.Circlets)
builds.Add(new Build(w, f, p, s, g, c, configs));
Console.WriteLine($" - Evaluating builds");
#if DEBUG
foreach (var build in builds)
build.Value = evaluateBuild(build);
#else
Parallel.For(0, builds.Count, index =>
{
var build = builds[index];
if (filterBuild == null || filterBuild(build))
build.Value = evaluateBuild(build);
});
#endif
Console.WriteLine($" - Ranking builds");
var bestBuild = (Build)null;
foreach (var build in builds)
if (bestBuild == null || bestBuild.Value < build.Value)
bestBuild = build;
return bestBuild;
}
public Build FindIdealArtifacts(ICollection<Weapon> weapons, BuildTarget buildTarget, ReadOnlyDictionary<Type, object> configs, int generations, double rollFactor,
Func<Build, double> evaluateBuild, Func<Build, bool> filterBuild = null)
{
var random = new Random();
var bestValue = 0.0;
var bestBuilds = Enumerable.Empty<Build>();
for (var i = 0; i < generations; i++)
{
var artifactSource = buildTarget.GenerateArtifacts(rollFactor);
var add = true;
foreach (var b in bestBuilds)
{
addAndMutate(b.Artifacts[0], artifactSource.Flowers, buildTarget, rollFactor, random, add);
addAndMutate(b.Artifacts[1], artifactSource.Plumes, buildTarget, rollFactor, random, add);
addAndMutate(b.Artifacts[2], artifactSource.Sands, buildTarget, rollFactor, random, add);
addAndMutate(b.Artifacts[3], artifactSource.Goblets, buildTarget, rollFactor, random, add);
addAndMutate(b.Artifacts[4], artifactSource.Circlets, buildTarget, rollFactor, random, add);
add = false;
}
Console.WriteLine($" - Creating {weapons.Count * artifactSource.Flowers.Count * artifactSource.Plumes.Count * artifactSource.Sands.Count * artifactSource.Goblets.Count * artifactSource.Circlets.Count} builds");
var builds = new List<Build>();
foreach (var w in weapons)
foreach (var f in artifactSource.Flowers)
foreach (var p in artifactSource.Plumes)
foreach (var s in artifactSource.Sands)
foreach (var g in artifactSource.Goblets)
foreach (var c in artifactSource.Circlets)
builds.Add(new Build(w, f, p, s, g, c, configs));
Console.WriteLine($" - Evaluating builds");
#if DEBUG
foreach (var build in builds)
build.Value = evaluateBuild(build);
#else
Parallel.For(0, builds.Count, index =>
{
var build = builds[index];
if (filterBuild == null || filterBuild(build))
build.Value = evaluateBuild(build);
});
#endif
bestBuilds = builds.OrderByDescending(b => b.Value).Take(5);
var topValue = bestBuilds.First().Value;
if (topValue > bestValue)
{
Console.WriteLine($" ({topValue:#.#})");
bestValue = topValue;
}
}
return bestBuilds.First();
}
private static void addAndMutate(Artifact artifact, List<Artifact> artifacts, BuildTarget buildTarget, double rollFactor, Random random, bool add)
{
if (add)
addUnique(artifact, artifacts);
switch (random.Next(3))
{
case 0:
// Change set
{
var currentSet = artifact.Set.GetType();
var setType = buildTarget.UsefulSets.Except(Enumerable.Repeat(currentSet, 1)).PickOne(random, currentSet);
var set = (ArtifactSet)Activator.CreateInstance(setType);
addUnique(new Artifact
{
Set = set,
Stats = artifact.Stats,
}, artifacts);
}
break;
case 1:
case 2:
// Change 1 substat and reroll substats
{
var mainstat = artifact.Stats[0].Item1;
var substats = artifact.Stats.Skip(1).Select(s => s.Item1).ToArray();
if (random.NextDouble() > .5f)
{
var slot = random.Next(substats.Length);
substats[slot] = buildTarget.UsefulSubStats.Except(artifact.Stats.Select(s => s.Item1)).PickOne(random, substats[slot]);
}
addUnique(new Artifact
{
Set = artifact.Set,
Stats = ArtifactGenerator.GenerateStats(artifact.Stats[0].Item1, substats, rollFactor, random).ToArray(),
}, artifacts);
}
break;
}
}
private static void addUnique(Artifact artifact, List<Artifact> artifacts)
{
if (!artifacts.Contains(artifact))
artifacts.Add(artifact);
}
}
}