Skip to content

Commit

Permalink
Merge branch 'condition-furcation' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Nov 14, 2023
2 parents 4bf7397 + f5b417f commit 12359ed
Show file tree
Hide file tree
Showing 20 changed files with 552 additions and 187 deletions.
2 changes: 1 addition & 1 deletion Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static async Task RunOther()
MaxStepCount = 30,
};

var sim = new SimulatorNoRandom(new(input));
var sim = new SimulatorNoRandom();
(_, var state) = sim.Execute(new(input), ActionType.MuscleMemory);
(_, state) = sim.Execute(state, ActionType.PrudentTouch);
//(_, state) = sim.Execute(state, ActionType.Manipulation);
Expand Down
1 change: 1 addition & 0 deletions Craftimizer/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class Configuration : IPluginConfiguration
private List<Macro> macros { get; set; } = new();
[JsonIgnore]
public IReadOnlyList<Macro> Macros => macros;
public int ReliabilitySimulationCount { get; set; } = 500;
public bool ConditionRandomness { get; set; } = true;
public SolverConfig SimulatorSolverConfig { get; set; } = SolverConfig.SimulatorDefault;
public SolverConfig SynthHelperSolverConfig { get; set; } = SolverConfig.SynthHelperDefault;
Expand Down
1 change: 1 addition & 0 deletions Craftimizer/Craftimizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

<ItemGroup>
<PackageReference Include="DalamudPackager" Version="2.1.12" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion Craftimizer/ImGuiExtras.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Runtime.InteropServices;
using System.Text;

namespace Craftimizer;
namespace Craftimizer.Plugin;

internal static unsafe class ImGuiExtras
{
Expand Down
146 changes: 113 additions & 33 deletions Craftimizer/ImGuiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
using Dalamud.Interface;
using Dalamud.Interface.Utility.Raii;
using ImGuiNET;
using ImPlotNET;
using MathNet.Numerics.Statistics;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -130,44 +134,14 @@ public static void EndGroupPanel()
ImGui.PopID();
}

private struct EndUnconditionally : ImRaii.IEndObject, IDisposable
{
private Action EndAction { get; }

public bool Success { get; }

public bool Disposed { get; private set; }

public EndUnconditionally(Action endAction, bool success)
{
EndAction = endAction;
Success = success;
Disposed = false;
}

public void Dispose()
{
if (!Disposed)
{
EndAction();
Disposed = true;
}
}
}

public static ImRaii.IEndObject GroupPanel(string name, float width, out float internalWidth)
{
internalWidth = BeginGroupPanel(name, width);
return new EndUnconditionally(EndGroupPanel, true);
}

private static Vector2 UnitCircle(float theta)
{
var (s, c) = MathF.SinCos(theta);
// SinCos positive y is downwards, but we want it upwards for ImGui
return new Vector2(c, -s);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Lerp(float a, float b, float t) =>
MathF.FusedMultiplyAdd(b - a, t, a);

Expand Down Expand Up @@ -251,6 +225,72 @@ public static void ArcProgress(float value, float radiusInner, float radiusOuter
Arc(MathF.PI / 2, MathF.PI / 2 - MathF.Tau * Math.Clamp(value, 0, 1), radiusInner, radiusOuter, backgroundColor, filledColor);
}

public sealed class ViolinData
{
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public float X, Y, Y2;

public Point(float x, float y, float y2)
{
X = x;
Y = y;
Y2 = y2;
}
}

public ReadOnlySpan<Point> Data => (DataArray ?? Array.Empty<Point>()).AsSpan();
private Point[]? DataArray { get; set; }
public readonly float Min;
public readonly float Max;

public ViolinData(IEnumerable<int> samples, float min, float max, int resolution, double bandwidth)
{
Min = min;
Max = max;
bandwidth *= Max - Min;
var samplesList = samples.AsParallel().Select(s => (double)s).ToArray();
_ = Task.Run(() => {
var s = Stopwatch.StartNew();
var data = ParallelEnumerable.Range(0, resolution + 1)
.Select(n => Lerp(min, max, n / (float)resolution))
.Select(n => (n, (float)KernelDensity.EstimateGaussian(n, bandwidth, samplesList)))
.Select(n => new Point(n.n, n.Item2, -n.Item2));
DataArray = data.ToArray();
s.Stop();
Log.Debug($"Violin plot processing took {s.Elapsed.TotalMilliseconds:0.00}ms");
});
}
}

public static void ViolinPlot(in ViolinData data, Vector2 size)
{
using var padding = ImRaii2.PushStyle(ImPlotStyleVar.PlotPadding, Vector2.Zero);
using var plotBg = ImRaii2.PushColor(ImPlotCol.PlotBg, Vector4.Zero);
using var fill = ImRaii2.PushColor(ImPlotCol.Fill, Vector4.One.WithAlpha(.5f));

using var plot = ImRaii2.Plot("##violin", size, ImPlotFlags.CanvasOnly | ImPlotFlags.NoInputs | ImPlotFlags.NoChild | ImPlotFlags.NoFrame);
if (plot)
{
ImPlot.SetupAxes(null, null, ImPlotAxisFlags.NoDecorations, ImPlotAxisFlags.NoDecorations | ImPlotAxisFlags.AutoFit);
ImPlot.SetupAxisLimits(ImAxis.X1, data.Min, data.Max, ImPlotCond.Always);
ImPlot.SetupFinish();

if (data.Data is { } points && !points.IsEmpty)
{
unsafe
{
var label_id = stackalloc byte[] { (byte)'\0' };
fixed (ViolinData.Point* p = points)
{
ImPlotNative.ImPlot_PlotShaded_FloatPtrFloatPtrFloatPtr(label_id, &p->X, &p->Y, &p->Y2, points.Length, ImPlotShadedFlags.None, 0, sizeof(ViolinData.Point));
}
}
}
}
}

private sealed class SearchableComboData<T> where T : class
{
public readonly ImmutableArray<T> items;
Expand Down Expand Up @@ -467,10 +507,50 @@ public static bool InputTextMultilineWithHint(string label, string hint, ref str
return ImGuiExtras.InputTextEx(label, hint, ref input, maxLength, size, flags | Multiline, callback, user_data);
}

public static bool IconButtonSized(FontAwesomeIcon icon, Vector2 size)
private static Vector2 GetIconSize(FontAwesomeIcon icon)
{
using var font = ImRaii.PushFont(UiBuilder.IconFont);
var ret = ImGui.Button(icon.ToIconString(), size);
return ImGui.CalcTextSize(icon.ToIconString());
}

private static void DrawCenteredIcon(FontAwesomeIcon icon, Vector2 offset, Vector2 size)
{
var iconSize = GetIconSize(icon);

float scale;
Vector2 iconOffset;
if (iconSize.X > iconSize.Y)
{
scale = size.X / iconSize.X;
iconOffset = new(0, (size.Y - (iconSize.Y * scale)) / 2f);
}
else if (iconSize.Y > iconSize.X)
{
scale = size.Y / iconSize.Y;
iconOffset = new((size.X - (iconSize.X * scale)) / 2f, 0);
}
else
{
scale = size.X / iconSize.X;
iconOffset = Vector2.Zero;
}

ImGui.GetWindowDrawList().AddText(UiBuilder.IconFont, UiBuilder.IconFont.FontSize * scale, offset + iconOffset, ImGui.GetColorU32(ImGuiCol.Text), icon.ToIconString());
}

public static bool IconButtonSquare(FontAwesomeIcon icon, float size = -1)
{
var ret = false;

var buttonSize = new Vector2(size == -1 ? ImGui.GetFrameHeight() : size);
var pos = ImGui.GetCursorScreenPos();
var spacing = new Vector2(ImGui.GetStyle().FramePadding.Y);

if (ImGui.Button($"###{icon.ToIconString()}", buttonSize))
ret = true;

DrawCenteredIcon(icon, pos + spacing, buttonSize - spacing * 2);

return ret;
}

Expand Down
92 changes: 92 additions & 0 deletions Craftimizer/ImRaii2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Dalamud.Interface.Utility.Raii;
using ImPlotNET;
using System;
using System.Numerics;

namespace Craftimizer.Plugin;

public static class ImRaii2
{
private struct EndUnconditionally : ImRaii.IEndObject, IDisposable
{
private Action EndAction { get; }

public bool Success { get; }

public bool Disposed { get; private set; }

public EndUnconditionally(Action endAction, bool success)
{
EndAction = endAction;
Success = success;
Disposed = false;
}

public void Dispose()
{
if (!Disposed)
{
EndAction();
Disposed = true;
}
}
}

private struct EndConditionally : ImRaii.IEndObject, IDisposable
{
public bool Success { get; }

public bool Disposed { get; private set; }

private Action EndAction { get; }

public EndConditionally(Action endAction, bool success)
{
EndAction = endAction;
Success = success;
Disposed = false;
}

public void Dispose()
{
if (!Disposed)
{
if (Success)
{
EndAction();
}

Disposed = true;
}
}
}

public static ImRaii.IEndObject GroupPanel(string name, float width, out float internalWidth)
{
internalWidth = ImGuiUtils.BeginGroupPanel(name, width);
return new EndUnconditionally(ImGuiUtils.EndGroupPanel, true);
}

public static ImRaii.IEndObject Plot(string title_id, Vector2 size, ImPlotFlags flags)
{
return new EndConditionally(new Action(ImPlot.EndPlot), ImPlot.BeginPlot(title_id, size, flags));
}

public static ImRaii.IEndObject PushStyle(ImPlotStyleVar idx, Vector2 val)
{
ImPlot.PushStyleVar(idx, val);
return new EndUnconditionally(ImPlot.PopStyleVar, true);
}

public static ImRaii.IEndObject PushStyle(ImPlotStyleVar idx, float val)
{
ImPlot.PushStyleVar(idx, val);
return new EndUnconditionally(ImPlot.PopStyleVar, true);
}

public static ImRaii.IEndObject PushColor(ImPlotCol idx, Vector4 col)
{
ImPlot.PushStyleColor(idx, col);
return new EndUnconditionally(ImPlot.PopStyleColor, true);
}
}
4 changes: 2 additions & 2 deletions Craftimizer/Windows/MacroClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override void Draw()
private void DrawMacro(int idx, string macro)
{
using var id = ImRaii.PushId(idx);
using var panel = ImGuiUtils.GroupPanel($"Macro {idx + 1}", -1, out var availWidth);
using var panel = ImRaii2.GroupPanel($"Macro {idx + 1}", -1, out var availWidth);

var cursor = ImGui.GetCursorPos();

Expand All @@ -49,7 +49,7 @@ private void DrawMacro(int idx, string macro)
ImGui.SetCursorPos(buttonCursor);
{
using var color = ImRaii.PushColor(ImGuiCol.Button, ImGui.GetColorU32(buttonActive ? ImGuiCol.ButtonActive : ImGuiCol.ButtonHovered), buttonHovered);
ImGuiUtils.IconButtonSized(FontAwesomeIcon.Paste, new(ImGui.GetFrameHeight()));
ImGuiUtils.IconButtonSquare(FontAwesomeIcon.Paste);
if (buttonClicked)
{
ImGui.SetClipboardText(macro);
Expand Down
Loading

1 comment on commit 12359ed

@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: 12359ed Previous: a8c3f34 Ratio
Craftimizer.Benchmark.Bench.Solve(State: 5372D31C98FA4C357F54029912394B0F5ECBE94AEC9D12C1C2B7F453C62ACD2F, Config: E1C895829E5BB1533C41D3C71AD65B6B3E1A60B5EE2D35A845744C251E71EFA5) 1787148186.6666667 ns (± 29859865.600258883)
Craftimizer.Benchmark.Bench.Solve(State: 5372D31C98FA4C357F54029912394B0F5ECBE94AEC9D12C1C2B7F453C62ACD2F, Config: E1C895829E5BB1533C41D3C71AD65B6B3E1A60B5EE2D35A845744C251E71EFA5) 1421138653.3333333 ns (± 20497906.774207342)
Craftimizer.Benchmark.Bench.Solve(State: 99B0F1AD46A18B4D8262F9BA75ABE23507217C2F20FBF895A49282DDFEF50190, Config: E1C895829E5BB1533C41D3C71AD65B6B3E1A60B5EE2D35A845744C251E71EFA5) 1720543280 ns (± 10731279.112429103)
Craftimizer.Benchmark.Bench.Solve(State: 99B0F1AD46A18B4D8262F9BA75ABE23507217C2F20FBF895A49282DDFEF50190, Config: E1C895829E5BB1533C41D3C71AD65B6B3E1A60B5EE2D35A845744C251E71EFA5) 1398625128.5714285 ns (± 7012174.725809015)

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

Please sign in to comment.