Skip to content

Commit

Permalink
优化一阶段:内部计算全部迁移到使用int;优化计算过程和数据收集
Browse files Browse the repository at this point in the history
  • Loading branch information
Funny-ppt committed Mar 23, 2024
1 parent add20fc commit 087fb16
Show file tree
Hide file tree
Showing 141 changed files with 502 additions and 516 deletions.
100 changes: 45 additions & 55 deletions InfrastSim/AggregateValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@

namespace InfrastSim;

public class AggregateValue : IJsonSerializable {
double _baseValue;
double _value = new();
readonly Dictionary<string, double> _additionValues = new();
readonly HashSet<string> _disables = new();
public class AggregateValue(int baseValue = 0, int min = int.MinValue, int max = int.MaxValue, bool displayAsPercentage = false) : IJsonSerializable {
int _baseValue = baseValue;
int _value = 0;
readonly Dictionary<string, int> _additionValues = [];
readonly HashSet<string> _disables = [];
bool _upToDate = false;

public AggregateValue(double baseValue = 0.0, double min = double.MinValue, double max = double.MaxValue) {
_baseValue = baseValue;
MinValue = min;
MaxValue = max;
}

public bool UpToDate {
get => _upToDate;
}
public double CalculatedValue {
public int CalculatedValue {
get {
if (_upToDate) return _value;
_upToDate = true;
Expand All @@ -28,69 +19,57 @@ public double CalculatedValue {
return _value = Math.Clamp(_baseValue + additions, MinValue, MaxValue);
}
}
public double BaseValue => _baseValue;
public double MinValue { get; set; }
public double MaxValue { get; set; }
public int BaseValue => _baseValue;
public int MinValue { get; set; } = min;
public int MaxValue { get; set; } = max;

public IEnumerable<KeyValuePair<string, double>> EnumerateValues() {
public IEnumerable<KeyValuePair<string, int>> EnumerateValues() {
return _additionValues
.ExceptBy(_disables, p => p.Key);
}

public double GetValue(string name)
=> _additionValues.GetValueOrDefault(name);
public double SetValue(string name, double value) {
public int GetValue(string name) => _additionValues.GetValueOrDefault(name);
public int SetValue(string name, int value) {
var oldValue = GetValue(name);
if (Util.Equals(value, oldValue)) {
if (value == oldValue)
return oldValue;
}
if (!_disables.Contains(name)) _upToDate = false;
if (!_disables.Contains(name))
_upToDate = false;
return _additionValues[name] = value;
}
public double AddValue(string name, double value) {
public int AddValue(string name, int value) {
return SetValue(name, value + GetValue(name));
}
public double SetIfGreater(string name, double value) {
public int SetIfGreater(string name, int value) {
return SetValue(name, Math.Max(value, GetValue(name)));
}
public double SetIfLesser(string name, double value) {
public int SetIfLesser(string name, int value) {
return SetValue(name, Math.Min(value, GetValue(name)));
}
public double SetValue(NamedValue namedValue) {
return SetValue(namedValue.Name, namedValue.Value);
}
public double AddValue(NamedValue namedValue) {
return AddValue(namedValue.Name, namedValue.Value);
}
public double SetIfGreater(NamedValue namedValue) {
return SetIfGreater(namedValue.Name, namedValue.Value);
}
public double SetIfLesser(NamedValue namedValue) {
return SetIfLesser(namedValue.Name, namedValue.Value);
}
public int SetValue(int value) => SetValue("common", value);
public int AddValue(int value) => AddValue("common", value);
public int SetIfGreater(int value) => SetIfGreater("common", value);
public int SetIfLesser(int value) => SetIfLesser("common", value);

public void Disable(string name) {
if (!_disables.Contains(name)) {
if (!Util.Equals(GetValue(name), 0)) _upToDate = false;
_disables.Add(name);
if (_disables.Add(name)) {
if (GetValue(name) != 0) _upToDate = false;
}
}
public void Enable(string name) {
if (_disables.Contains(name)) {
if (!Util.Equals(GetValue(name), 0)) _upToDate = false;
_disables.Remove(name);
if (_disables.Remove(name)) {
if (GetValue(name) != 0) _upToDate = false;
}
}

public void Remove(string name) {
if (!Util.Equals(GetValue(name), 0)) {
if (GetValue(name) != 0) {
_upToDate = false;
}
_additionValues.Remove(name);
}
public void Clear() {
_disables.Clear();
foreach (var key in _additionValues.Keys) {
foreach (var key in _additionValues.Keys) { // BENCHMARK REQUIRED: use Clear() or manually set value to 0?
_additionValues[key] = 0;
}
_upToDate = true;
Expand All @@ -99,17 +78,28 @@ public void Clear() {

public void ToJson(Utf8JsonWriter writer, bool detailed = false) {
writer.WriteStartObject();
writer.WriteNumber("value", CalculatedValue);
writer.WriteNumber("base-value", BaseValue);
writer.WriteNumber("min-value", MinValue);
writer.WriteNumber("max-value", MaxValue);
if (displayAsPercentage) {
writer.WriteNumber("value", CalculatedValue / 100.0);
writer.WriteNumber("base-value", BaseValue / 100.0);
writer.WriteNumber("min-value", MinValue / 100.0);
writer.WriteNumber("max-value", MaxValue / 100.0);
} else {
writer.WriteNumber("value", CalculatedValue);
writer.WriteNumber("base-value", BaseValue);
writer.WriteNumber("min-value", MinValue);
writer.WriteNumber("max-value", MaxValue);
}

writer.WritePropertyName("details");
writer.WriteStartArray();
foreach (var kvp in _additionValues) {
writer.WriteStartObject();
writer.WriteString("tag", kvp.Key);
writer.WriteNumber("value", kvp.Value);
if (displayAsPercentage) {
writer.WriteNumber("value", kvp.Value / 100.0);
} else {
writer.WriteNumber("value", kvp.Value);
}
writer.WriteBoolean("disabled", _disables.Contains(kvp.Key));
writer.WriteEndObject();
}
Expand All @@ -118,7 +108,7 @@ public void ToJson(Utf8JsonWriter writer, bool detailed = false) {
writer.WriteEndObject();
}

public static implicit operator double(AggregateValue aggregateValue) {
public static implicit operator int(AggregateValue aggregateValue) {
return aggregateValue.CalculatedValue;
}
}
10 changes: 5 additions & 5 deletions InfrastSim/Algorithms/Combination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ public Combination(IList<T> values, int n) {
return arr;
}

public IEnumerator<IEnumerable<T>> GetEnumerator() {
public IEnumerator<IEnumerable<T>?> GetEnumerator() {
return new Enumerator(this);
}

public IEnumerable<IEnumerable<T>> ToEnumerable() {
IEnumerable<T>? cur = null;
IEnumerable<T>? cur;
while ((cur = Enumerate()) != null) {
yield return cur;
}
}

class Enumerator : IEnumerator<IEnumerable<T>> {
class Enumerator : IEnumerator<IEnumerable<T>?> {
Combination<T> _combination;
IEnumerable<T>? _cur = null;

public Enumerator(Combination<T> combination) {
_combination = combination;
}

public IEnumerable<T> Current => _cur;
object IEnumerator.Current => _cur;
public IEnumerable<T>? Current => _cur;
object? IEnumerator.Current => _cur;

public bool MoveNext() {
_cur = _combination.Enumerate();
Expand Down
2 changes: 1 addition & 1 deletion InfrastSim/EventDriven/OperatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace InfrastSim.EventDriven;
internal abstract class OperatorBase : IEventSource
{
public double Mood { get; private set; }
public AggregateValue MoodConsumeRate { get; private set; } = new(1.0);
public AggregateValue MoodConsumeRate { get; private set; } = new(100);

public DateTime TriggerTime => throw new NotImplementedException();

Expand Down
4 changes: 0 additions & 4 deletions InfrastSim/InfrastSim.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<PublishTrimmed>true</PublishTrimmed>
<EnforceExtendedAnaylzerRules>true</EnforceExtendedAnaylzerRules>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.0-rc.2.23479.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\InfrastSimSourceGenerator\InfrastSimSourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\RandomEx\RandomEx.csproj" />
Expand Down
15 changes: 5 additions & 10 deletions InfrastSim/NamedValue.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
namespace InfrastSim;
public struct NamedValue {
public string Name;
public double Value;
public struct NamedValue(string name, int value) {
public string Name = name ?? throw new ArgumentNullException(nameof(name));
public int Value = value;

public NamedValue(string name, double value) {
Name = name ?? throw new ArgumentNullException(nameof(name));
Value = value;
}

public static implicit operator NamedValue(double value) {
public static implicit operator NamedValue(int value) {
return new("common", value);
}
public static implicit operator double(NamedValue value) {
public static implicit operator int(NamedValue value) {
return value.Value;
}
}
6 changes: 4 additions & 2 deletions InfrastSim/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace InfrastSim;

public record Order(int RequiredLevel, TimeSpan ProduceTime, Material Consumes, Material Earns) : IJsonSerializable {
public readonly static Order[] Gold = {
public int ProduceTicks { get; init; } = ProduceTime.ToSimuTicks();

public readonly static Order[] Gold = [
new(1, TimeSpan.FromMinutes(144), new Material("赤金", 2), new Material("龙门币", 1000)),
new(2, TimeSpan.FromMinutes(210), new Material("赤金", 3), new Material("龙门币", 1500)),
new(3, TimeSpan.FromMinutes(276), new Material("赤金", 4), new Material("龙门币", 2000)),
};
];
public readonly static Order OriginStone = new(3, TimeSpan.FromMinutes(120), new Material("源石碎片", 2), new Material("合成玉", 20));

public void ToJson(Utf8JsonWriter writer, bool detailed = false) {
Expand Down
1 change: 1 addition & 0 deletions InfrastSim/Product.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace InfrastSim;
public record Product(
string Name, int Volume, int RequiredLevel, TimeSpan ProduceTime, Material[]? Consumes = null) {
public int ProduceTicks { get; init; } = ProduceTime.ToSimuTicks();

public readonly static Product[] CombatRecords = [
new("基础作战记录", 2, 1, TimeSpan.FromMinutes(45)),
Expand Down
15 changes: 15 additions & 0 deletions InfrastSim/TicksHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace InfrastSim;
internal static class TicksHelper {
public const long TimeSpanTicksPerTick = 100000L;
public const int TicksPerSecond = 100;
public const int TicksPerMinute = TicksPerSecond * 60;
public const int TicksPerHours = TicksPerMinute * 24;
public const int TicksPerDrone = TicksPerMinute * 3; // 3 minute
public const int TicksToProduceDrone = TicksPerMinute * 6; // 6 minute
public const int TicksToProducerefresh = TicksPerHours * 12; // 12 hours

public static int ToSimuTicks(this TimeSpan ts) => (int)(ts.Ticks / TimeSpanTicksPerTick);
public static int TotalSeconds(this TimeSpan ts) => (int)(ts.Ticks / TimeSpan.TicksPerSecond);
public static int HoursNotExceed(this TimeSpan ts, int n) => Math.Min(n, (int)(ts.Ticks / TimeSpan.TicksPerHour));
public static TimeSpan SimuTicksToTimeSpan(this int simuTicks) => TimeSpan.FromTicks(simuTicks * TimeSpanTicksPerTick);
}
4 changes: 2 additions & 2 deletions InfrastSim/TimeDriven/ControlCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class ControlCenter : FacilityBase {
public override int PowerConsumes => 0;

public override int AcceptOperatorNums => Level;
public override double MoodConsumeModifier => -WorkingOperatorsCount * 0.05;
public override double EffiencyModifier => 0.0;
public override int MoodConsumeModifier => -WorkingOperatorsCount * 5;
public override int EffiencyModifier => 0;
public AggregateValue ExtraMoodModifier { get; } = new();

public override void Reset() {
Expand Down
4 changes: 2 additions & 2 deletions InfrastSim/TimeDriven/Crafting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Crafting : FacilityBase {

public override bool IsWorking => false;

public override double MoodConsumeModifier => 0;
public override int MoodConsumeModifier => 0;

public override double EffiencyModifier => 0;
public override int EffiencyModifier => 0;
}
16 changes: 8 additions & 8 deletions InfrastSim/TimeDriven/Dormitory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ public class Dormitory : FacilityBase {
public override bool IsWorking => true; // force operators to calculate mood change
public override int AcceptOperatorNums => 5;

public override double MoodConsumeModifier {
public override int MoodConsumeModifier {
get {
return -(1 + 1.5 + 0.1 * Level);
return -(100 + 150 + 10 * Level); // 宿舍等级提供 1.6-2 每小时心情回复,其中100为抵消角色基础消耗100
}
}


public OperatorBase? Vip { get; private set; }
public double VipMoodModifier { get; private set; }
public double DormMoodModifier { get; private set; }
public void SetVipMoodModifier(double value) {
public int VipMoodModifier { get; private set; }
public int DormMoodModifier { get; private set; }
public void SetVipMoodModifier(int value) {
VipMoodModifier = Math.Min(VipMoodModifier, value);
}
public void SetDormMoodModifier(double value) {
public void SetDormMoodModifier(int value) {
DormMoodModifier = Math.Min(DormMoodModifier, value);
}
public override double EffiencyModifier => 0.0;
public override int EffiencyModifier => 0;
public int Atmosphere => Level * 1000;

public override void Reset() {
Expand Down Expand Up @@ -66,7 +66,7 @@ public override void Resolve(Simulator simu) {

simu.Delay((simu) => {
foreach (var op in Operators) {
op.MoodConsumeRate.SetValue("dorm-atmosphere", -0.0004 * Atmosphere);
op.MoodConsumeRate.SetValue("dorm-atmosphere", (int)(-0.04 * Atmosphere));
op.MoodConsumeRate.SetValue("dorm-extra", DormMoodModifier);
op.MoodConsumeRate.Disable("control-center");
op.MoodConsumeRate.Disable("control-center-extra");
Expand Down
8 changes: 4 additions & 4 deletions InfrastSim/TimeDriven/FacilityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public void SetLevel(int level) {
}
}

public abstract double MoodConsumeModifier { get; }
public abstract double EffiencyModifier { get; }
public double TotalEffiencyModifier => WorkingOperators.Sum(op => op.EfficiencyModifier) + EffiencyModifier;
public abstract int MoodConsumeModifier { get; }
public abstract int EffiencyModifier { get; }
public int TotalEffiencyModifier => WorkingOperators.Sum(op => op.EfficiencyModifier) + EffiencyModifier;

public virtual void Reset() {
foreach (var op in Operators) {
Expand Down Expand Up @@ -162,7 +162,7 @@ public void ToJson(Utf8JsonWriter writer, bool detailed = false) {
WriteDerivedContent(writer, detailed);

if (detailed) {
writer.WriteNumber("base-efficiency", 1 + EffiencyModifier);
writer.WriteNumber("base-efficiency", (100 + EffiencyModifier) / 100.0);
writer.WriteNumber("operators-efficiency", WorkingOperators.Sum(op => op.EfficiencyModifier));
}

Expand Down
4 changes: 2 additions & 2 deletions InfrastSim/TimeDriven/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public static int GetRealGoldProductionLine(this Simulator simu)
}
public static int GetGoldProductionLine(this Simulator simu)
{
return (int)simu.ExtraGoldProductionLine + simu.GetRealGoldProductionLine();
return simu.ExtraGoldProductionLine + simu.GetRealGoldProductionLine();
}

public static int PowerStationsCount(this Simulator simu)
{
return (int)simu.ExtraPowerStation + simu.PowerStations.Count();
return simu.ExtraPowerStation + simu.PowerStations.Count();
}
public static bool IsProduceGold(this ManufacturingStation manufacturing) {
return manufacturing.Product == Product.Gold;
Expand Down
Loading

0 comments on commit 087fb16

Please sign in to comment.