|
|
|
|
@@ -0,0 +1,186 @@
|
|
|
|
|
using Enigmos.Boards;
|
|
|
|
|
using Enigmos.Modules.InterlayerModules;
|
|
|
|
|
using Enigmos.Ports.DataPorts;
|
|
|
|
|
using Godot;
|
|
|
|
|
using Nocturnis.DataStructures;
|
|
|
|
|
using Nocturnis.DataStructures.ConfigurableParameters;
|
|
|
|
|
using Nocturnis.DataStructures.DataPortGroups;
|
|
|
|
|
using Nocturnis.Enigmos.Modules;
|
|
|
|
|
using Nocturnis.Enigmos.Modules.InterlayerModules;
|
|
|
|
|
using Nocturnis.Enigmos.Ports;
|
|
|
|
|
using Nocturnis.Enigmos.Ports.DataPorts;
|
|
|
|
|
using Nocturnis.Enigmos.Ports.DataPorts.Directions;
|
|
|
|
|
using Nocturnis.GlobalManagement.Constants;
|
|
|
|
|
using Nocturnis.GlobalManagement.Providers;
|
|
|
|
|
|
|
|
|
|
namespace Enigmos.Modules.ProgrammableModules.OptimizationModule;
|
|
|
|
|
|
|
|
|
|
public partial class OptimizationModule : ProgrammableModule,
|
|
|
|
|
IPolymorphismModule,
|
|
|
|
|
IParameterizedModule,
|
|
|
|
|
IOptimizationModule
|
|
|
|
|
{
|
|
|
|
|
public bool OptimizationFinished { get; set; }
|
|
|
|
|
public bool OptimizationStarted { get; set; }
|
|
|
|
|
private double? CachedOptimizeValue { get; set; }
|
|
|
|
|
public IDataInPort? ArrayInput { get; set; }
|
|
|
|
|
public IInterlayerDataInModule[] ExplicitOutputs { get; set; } = Array.Empty<IInterlayerDataInModule>();
|
|
|
|
|
public IInterlayerDataOutModule[] ImplicitInputs { get; set; } = Array.Empty<IInterlayerDataOutModule>();
|
|
|
|
|
public IData[] CachedInput { get; set; } = Array.Empty<IData>();
|
|
|
|
|
public int ProcessingIndex { get; set; }
|
|
|
|
|
private IBoolParameter? UsingMax { get; set; }
|
|
|
|
|
public IData CachedResult { get; set; } =
|
|
|
|
|
GlobalProvider.DataStructureProvider!.NewData(0, EnigmosConstant.DataPortTypes.Null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IDataInPort? InternalIterOut { get; set; }
|
|
|
|
|
public IDataOutPort? InternalArrayIn { get; set; }
|
|
|
|
|
|
|
|
|
|
private IDataPortGroup[] InterlayerGroups { get; set; } = Array.Empty<IDataPortGroup>();
|
|
|
|
|
private IDataPortGroup? OptimizationGroup { get; set; }
|
|
|
|
|
|
|
|
|
|
private InterlayerDataInModule[] ExplicitInputs { get; set; } = Array.Empty<InterlayerDataInModule>();
|
|
|
|
|
private OptimizationSelectorModule? Selector { get; set; }
|
|
|
|
|
private OptimizationOutputModule? Output { get; set; }
|
|
|
|
|
public override IEnumerable<IBasePort> Ports => new[] { ArrayInput! };
|
|
|
|
|
|
|
|
|
|
public new OptimizationModuleBoard UnderlyingBoard
|
|
|
|
|
{
|
|
|
|
|
get => (base.UnderlyingBoard as OptimizationModuleBoard)!;
|
|
|
|
|
set => base.UnderlyingBoard = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HashSet<IConfigurableParameter> ConfigurableParameters { get; set; } = new();
|
|
|
|
|
public HashSet<IDataPortGroup> ConfigurablePortGroups { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<IBasePort> ExplicitPorts =>
|
|
|
|
|
new IBasePort[] { ArrayInput!, Output!.Output! }
|
|
|
|
|
.Union<IBasePort>(ExplicitInputs.Select(c => c.DataIn)!);
|
|
|
|
|
|
|
|
|
|
public override BaseModule[] SubModules =>
|
|
|
|
|
ExplicitInputs
|
|
|
|
|
.Union(new BaseModule[] { Output! })
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<IBasePort> ImplicitPorts =>
|
|
|
|
|
new IBasePort[] { UnderlyingBoard.InputProvider!.Output!, UnderlyingBoard.Selector!.Selector! }
|
|
|
|
|
.Union(UnderlyingBoard.ImplicitDataOuts.Select(c => c.DataOut))!;
|
|
|
|
|
|
|
|
|
|
public override void Init()
|
|
|
|
|
{
|
|
|
|
|
base.Init();
|
|
|
|
|
UnderlyingBoard = GlobalProvider.SceneProvider!
|
|
|
|
|
.AssetMapper<OptimizationModuleBoard>()
|
|
|
|
|
.Instantiate<OptimizationModuleBoard>();
|
|
|
|
|
UnderlyingBoard.Init();
|
|
|
|
|
InterlayerGroups = new IDataPortGroup[3];
|
|
|
|
|
ExplicitInputs = new InterlayerDataInModule[3];
|
|
|
|
|
InternalIterOut = this.GetPort<IDataInPort>("InternalIterOut");
|
|
|
|
|
Output = GetModule<OptimizationOutputModule>("Output");
|
|
|
|
|
Output.Init();
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
|
{
|
|
|
|
|
ExplicitInputs[i] = GetModule<InterlayerDataInModule>($"EI{i + 1}");
|
|
|
|
|
ExplicitInputs[i].DualModule = UnderlyingBoard.ImplicitDataOuts[i];
|
|
|
|
|
UnderlyingBoard.ImplicitDataOuts[i].DualModule = ExplicitInputs[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OptimizationFinished = true;
|
|
|
|
|
CachedInput = Array.Empty<IData>();
|
|
|
|
|
//CachedArray = Array.Empty<DataPackage>();
|
|
|
|
|
ProcessingIndex = 0;
|
|
|
|
|
CachedResult = GlobalProvider.DataStructureProvider!.NullData;
|
|
|
|
|
CachedOptimizeValue = null;
|
|
|
|
|
|
|
|
|
|
ArrayInput = this.GetPort<DataInPort>("ArrayInput");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
|
{
|
|
|
|
|
InterlayerGroups[i] = GlobalProvider.DataStructureProvider.NewDataPortGroup(
|
|
|
|
|
this,
|
|
|
|
|
new IDataPort[] { ExplicitInputs[i].DataIn!, UnderlyingBoard.ImplicitDataOuts[i].DataOut! },
|
|
|
|
|
$"Exterior In -> Interior Out{i}",
|
|
|
|
|
EnigmosConstant.DataPortTypes.Real,
|
|
|
|
|
EnigmosConstant.DataPortTypes.AnyType
|
|
|
|
|
);
|
|
|
|
|
ExplicitInputs[i].Board = Board;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OptimizationGroup = GlobalProvider.DataStructureProvider.NewDataPortGroup(
|
|
|
|
|
this,
|
|
|
|
|
new IDataPort[] { ArrayInput },
|
|
|
|
|
"Array Input Type",
|
|
|
|
|
EnigmosConstant.DataPortTypes.AnyArrayType,
|
|
|
|
|
EnigmosConstant.DataPortTypes.AnyArray
|
|
|
|
|
);
|
|
|
|
|
UsingMax = GlobalProvider.DataStructureProvider.NewBoolParameter(
|
|
|
|
|
"Method",
|
|
|
|
|
"Max",
|
|
|
|
|
"Min",
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
Output.Output!.SetDataType(EnigmosConstant.DataPortTypes.Real);
|
|
|
|
|
Selector!.Selector!.SetDataType(EnigmosConstant.DataPortTypes.Real);
|
|
|
|
|
ConfigurableParameters = new HashSet<IConfigurableParameter> { UsingMax };
|
|
|
|
|
ConfigurablePortGroups =
|
|
|
|
|
InterlayerGroups.Union(new[] { OptimizationGroup }).ToHashSet();
|
|
|
|
|
PostInit();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Inference()
|
|
|
|
|
{
|
|
|
|
|
StringName elementType = GlobalProvider.DataPackageTypeProvider!.ToElement(OptimizationGroup!.SelectedType);
|
|
|
|
|
Output!.Output!.SetDataType(elementType);
|
|
|
|
|
UnderlyingBoard.InputProvider!.Output!.SetDataType(elementType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SoftReset() => UnderlyingBoard.Reset();
|
|
|
|
|
|
|
|
|
|
public void Optimize()
|
|
|
|
|
{
|
|
|
|
|
if (OptimizationFinished)
|
|
|
|
|
{
|
|
|
|
|
CachedOptimizeValue = null;
|
|
|
|
|
CachedInput = InternalArrayIn!.OutData.Get!.Array;
|
|
|
|
|
CachedResult = GlobalProvider.DataStructureProvider!.NewData(0, EnigmosConstant.DataPortTypes.Null);
|
|
|
|
|
ProcessingIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (ProcessingIndex < CachedInput.Length)
|
|
|
|
|
{
|
|
|
|
|
SoftReset();
|
|
|
|
|
UnderlyingBoard.InputProvider!.Output!.OutData.UpdateCalculation(
|
|
|
|
|
cache =>
|
|
|
|
|
(CachedInput[ProcessingIndex].Data, CachedInput[ProcessingIndex].Type)!
|
|
|
|
|
);
|
|
|
|
|
UnderlyingBoard.InputProvider.Output.OutData.Expire();
|
|
|
|
|
double currentValue = UnderlyingBoard.Selector!.Selector!.GetData.Get!.Double;
|
|
|
|
|
IData currentOut = InternalIterOut!.GetData.Get!;
|
|
|
|
|
if (UsingMax!.ParameterValue)
|
|
|
|
|
{
|
|
|
|
|
if (CachedOptimizeValue == null || (CachedOptimizeValue < currentValue))
|
|
|
|
|
{
|
|
|
|
|
CachedOptimizeValue = currentValue;
|
|
|
|
|
CachedResult.Assign(currentOut.Data!, currentOut.Type!);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (CachedOptimizeValue == null || CachedOptimizeValue > currentValue)
|
|
|
|
|
{
|
|
|
|
|
CachedOptimizeValue = currentValue;
|
|
|
|
|
CachedResult.Assign(currentOut.Data!, currentOut.Type!);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ProcessingIndex++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Output!.Define();
|
|
|
|
|
OptimizationFinished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Calculated { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|