Skip to content

Commit

Permalink
Optimize heuristic code slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Mar 15, 2024
1 parent 82ac00d commit 2a0ce93
Showing 1 changed file with 106 additions and 75 deletions.
181 changes: 106 additions & 75 deletions Solver/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,27 @@ public Simulator(ActionType[] actionPool, int maxStepCount, SimulationState? fil
this.maxStepCount = maxStepCount;
}

// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L146
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool CouldUseAction(BaseAction baseAction)
{
if (CalculateSuccessRate(baseAction.SuccessRate(this)) != 1)
return false;

// don't allow quality moves at max quality
if (Quality >= Input.Recipe.MaxQuality && baseAction.IncreasesQuality)
return false;

return baseAction.CouldUse(this);
}

// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L146
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// It's just a bunch of if statements, I would assume this is actually quite simple to follow
#pragma warning disable MA0051 // Method is too long
private bool CouldUseAction(ActionType action, BaseAction baseAction, bool strict)
private bool ShouldUseAction(ActionType action, BaseAction baseAction)
#pragma warning restore MA0051 // Method is too long
{
if (CalculateSuccessRate(baseAction.SuccessRate(this)) != 1)
Expand All @@ -48,86 +63,83 @@ private bool CouldUseAction(ActionType action, BaseAction baseAction, bool stric
if (Quality >= Input.Recipe.MaxQuality && baseAction.IncreasesQuality)
return false;

if (strict)
// always use Trained Eye if it's available
if (action == ActionType.TrainedEye)
return baseAction.CouldUse(this);

// don't allow quality moves under Muscle Memory for difficult crafts
if (Input.Recipe.ClassJobLevel == 90 &&
HasEffect(EffectType.MuscleMemory) &&
baseAction.IncreasesQuality)
return false;

// use First Turn actions if it's available and the craft is difficult
if (IsFirstStep &&
Input.Recipe.ClassJobLevel == 90 &&
baseAction.Category != ActionCategory.FirstTurn &&
CP > 10)
return false;

// don't allow combo actions if the combo is already in progress
if (ActionStates.TouchComboIdx != 0 &&
(action == ActionType.StandardTouchCombo || action == ActionType.AdvancedTouchCombo))
return false;

// don't allow pure quality moves under Veneration
if (HasEffect(EffectType.Veneration) &&
!baseAction.IncreasesProgress &&
baseAction.IncreasesQuality)
return false;

// don't allow pure quality moves when it won't be able to finish the craft
if (baseAction.IncreasesQuality &&
CalculateDurabilityCost(baseAction.DurabilityCost) > Durability)
return false;

if (baseAction.IncreasesProgress)
{
// always use Trained Eye if it's available
if (action == ActionType.TrainedEye)
return baseAction.CouldUse(this);

// don't allow quality moves under Muscle Memory for difficult crafts
if (Input.Recipe.ClassJobLevel == 90 &&
HasEffect(EffectType.MuscleMemory) &&
baseAction.IncreasesQuality)
return false;

// use First Turn actions if it's available and the craft is difficult
if (IsFirstStep &&
Input.Recipe.ClassJobLevel == 90 &&
baseAction.Category != ActionCategory.FirstTurn &&
CP > 10)
return false;

// don't allow combo actions if the combo is already in progress
if (ActionStates.TouchComboIdx != 0 &&
(action == ActionType.StandardTouchCombo || action == ActionType.AdvancedTouchCombo))
return false;

// don't allow pure quality moves under Veneration
if (HasEffect(EffectType.Veneration) &&
!baseAction.IncreasesProgress &&
baseAction.IncreasesQuality)
return false;

// don't allow pure quality moves when it won't be able to finish the craft
if (baseAction.IncreasesQuality &&
CalculateDurabilityCost(baseAction.DurabilityCost) > Durability)
return false;

if (baseAction.IncreasesProgress)
var progressIncrease = CalculateProgressGain(baseAction.Efficiency(this));
var wouldFinish = Progress + progressIncrease >= Input.Recipe.MaxProgress;

if (wouldFinish)
{
var progressIncrease = CalculateProgressGain(baseAction.Efficiency(this));
var wouldFinish = Progress + progressIncrease >= Input.Recipe.MaxProgress;

if (wouldFinish)
{
// don't allow finishing the craft if there is significant quality remaining
if (Quality < Input.Recipe.MaxQuality / 5)
return false;
}
else
{
// don't allow pure progress moves under Innovation, if it wouldn't finish the craft
if (HasEffect(EffectType.Innovation) &&
!baseAction.IncreasesQuality &&
baseAction.IncreasesProgress)
return false;
}
// don't allow finishing the craft if there is significant quality remaining
if (Quality < Input.Recipe.MaxQuality / 5)
return false;
}
else
{
// don't allow pure progress moves under Innovation, if it wouldn't finish the craft
if (HasEffect(EffectType.Innovation) &&
!baseAction.IncreasesQuality &&
baseAction.IncreasesProgress)
return false;
}
}

if (action == ActionType.ByregotsBlessing &&
GetEffectStrength(EffectType.InnerQuiet) <= 1)
return false;
if (action == ActionType.ByregotsBlessing &&
GetEffectStrength(EffectType.InnerQuiet) <= 1)
return false;

if ((action == ActionType.WasteNot || action == ActionType.WasteNot2) &&
(HasEffect(EffectType.WasteNot) || HasEffect(EffectType.WasteNot2)))
return false;
if ((action == ActionType.WasteNot || action == ActionType.WasteNot2) &&
(HasEffect(EffectType.WasteNot) || HasEffect(EffectType.WasteNot2)))
return false;

if (action == ActionType.MastersMend &&
Input.Recipe.MaxDurability - Durability < 25)
return false;
if (action == ActionType.MastersMend &&
Input.Recipe.MaxDurability - Durability < 25)
return false;

if (action == ActionType.Manipulation &&
HasEffect(EffectType.Manipulation))
return false;
if (action == ActionType.Manipulation &&
HasEffect(EffectType.Manipulation))
return false;

if (action == ActionType.GreatStrides &&
HasEffect(EffectType.GreatStrides))
return false;
if (action == ActionType.GreatStrides &&
HasEffect(EffectType.GreatStrides))
return false;

if ((action == ActionType.Veneration || action == ActionType.Innovation) &&
(GetEffectDuration(EffectType.Veneration) > 1 || GetEffectDuration(EffectType.Innovation) > 1))
return false;
}
if ((action == ActionType.Veneration || action == ActionType.Innovation) &&
(GetEffectDuration(EffectType.Veneration) > 1 || GetEffectDuration(EffectType.Innovation) > 1))
return false;

return baseAction.CouldUse(this);
}
Expand All @@ -139,9 +151,28 @@ public ActionSet AvailableActionsHeuristic(bool strict)
return new();

var ret = new ActionSet();
foreach (var (data, action) in actionPoolObjects)
if (CouldUseAction(action, data, strict))
ret.AddAction(action);
if (strict)
{
foreach (var (data, action) in actionPoolObjects)
{
if (ShouldUseAction(action, data))
ret.AddAction(action);
}

// If Trained Eye is possible, *always* use Trained Eye
if (ret.HasAction(ActionType.TrainedEye))
{
ret = new();
ret.AddAction(ActionType.TrainedEye);
}
}
else
{
foreach (var (data, action) in actionPoolObjects)
if (CouldUseAction(data))
ret.AddAction(action);
}

return ret;
}
}

1 comment on commit 2a0ce93

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2a0ce93 Previous: 24c8634 Ratio
Craftimizer.Benchmark.Bench.Solve(State: 79A13333, Config: DCD9242F) 1232428166.6666667 ns (± 7157950.888713758)
Craftimizer.Benchmark.Bench.Solve(State: 79A13333, Config: DCD9242F) 1305102276.9230769 ns (± 4219606.138640952)
Craftimizer.Benchmark.Bench.Solve(State: A0BD529D, Config: DCD9242F) 836949661.5384616 ns (± 3163239.663050752)
Craftimizer.Benchmark.Bench.Solve(State: A0BD529D, Config: DCD9242F) 814157678.5714285 ns (± 3296733.989217107)

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.