Skip to content
Snippets Groups Projects
Commit 9fc4007b authored by Radek Puš's avatar Radek Puš
Browse files

reimplemented comparator (as standalone app)

parent 75dc6bef
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core", "Core\Core.csproj",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeuralNetwork", "NeuralNetwork\NeuralNetwork.csproj", "{E949B86A-BE3D-481F-ABA4-0FB1C117837B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImporterCSV", "ImporterCSV\ImporterCSV.csproj", "{D98E5BAE-58A6-43F1-900C-0C8939D089F6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImporterCSV", "ImporterCSV\ImporterCSV.csproj", "{D98E5BAE-58A6-43F1-900C-0C8939D089F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransactionComparator", "TransactionComparator\TransactionComparator.csproj", "{2E637121-011C-4159-AC71-0205AFB80702}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -41,6 +43,14 @@ Global
{D98E5BAE-58A6-43F1-900C-0C8939D089F6}.Release|Any CPU.Build.0 = Release|Any CPU
{D98E5BAE-58A6-43F1-900C-0C8939D089F6}.Release|x64.ActiveCfg = Release|Any CPU
{D98E5BAE-58A6-43F1-900C-0C8939D089F6}.Release|x64.Build.0 = Release|Any CPU
{2E637121-011C-4159-AC71-0205AFB80702}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E637121-011C-4159-AC71-0205AFB80702}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E637121-011C-4159-AC71-0205AFB80702}.Debug|x64.ActiveCfg = Debug|Any CPU
{2E637121-011C-4159-AC71-0205AFB80702}.Debug|x64.Build.0 = Debug|Any CPU
{2E637121-011C-4159-AC71-0205AFB80702}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E637121-011C-4159-AC71-0205AFB80702}.Release|Any CPU.Build.0 = Release|Any CPU
{2E637121-011C-4159-AC71-0205AFB80702}.Release|x64.ActiveCfg = Release|Any CPU
{2E637121-011C-4159-AC71-0205AFB80702}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
......@@ -46,6 +46,10 @@
<None Remove="ClientApp\src\app\registration\registration.service.ts" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NeuralNetwork\NeuralNetwork.csproj" />
</ItemGroup>
<ItemGroup>
<TypeScriptCompile Include="ClientApp\src\app\helpers\jwt.interceptor.ts" />
<TypeScriptCompile Include="ClientApp\src\app\home\components\account-properties\account-properties.module.ts">
......
using System;
using System.Collections.Generic;
using System.Text;
namespace TransactionComparator.Interface
{
interface ICompratorInput
{
List<IElement> First { get; }
List<IElement> Second { get; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TransactionComparator.Interface
{
interface IElement
{
double Value { get; }
bool Repetitive { set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using TransactionComparator.Interface;
using TransactionComparator.Logic;
namespace TransactionComparator
{
class Comparator
{
private List<Vertex> Minor;
private List<Vertex> Major;
private bool Swapped;
private List<Vertex> PairedMinor = new List<Vertex>();
private List<Vertex> PairedMajor = new List<Vertex>();
public Comparator(List<IElement> first, List<IElement> second)
{
if (first.Count < second.Count)
{
Swapped = false;
Minor = GetVertexListFromIElements(first);
Major = GetVertexListFromIElements(second);
}
else
{
Swapped = true;
Minor = GetVertexListFromIElements(second);
Major = GetVertexListFromIElements(first);
}
Minor.Sort();
Major.Sort();
}
public void Compare()
{
while (Minor.Count > 0)
{
int hintPointer = 0;
for (int i = 0; i < Minor.Count; i++)
{
hintPointer = GetNeigbourPointer(i, hintPointer);
Major[hintPointer].NeighbourIndexes.Add(i);
Minor[i].NeighbourIndexes.Add(hintPointer);
}
RemovePairs();
}
}
internal void WritePairs()
{
for (int i = 0; i < PairedMajor.Count; i++)
Console.WriteLine($"{PairedMinor[i].Value} -> {PairedMajor[i].Value}");
}
internal void WriteUndetermined()
{
Console.WriteLine("Minor:");
foreach (IElement e in Minor)
Console.WriteLine(e.Value);
Console.WriteLine("Major:");
foreach (IElement e in Major)
Console.WriteLine(e.Value);
}
private void RemovePairs()
{
List<Vertex> uniqueMinor = new List<Vertex>(Minor.Count);
for (int i = 0; i < Minor.Count; i++)
{
int majorIndex = Minor[i].NeighbourIndexes[0];
if (Major[majorIndex] == null || GetMinLenghtID(Major[majorIndex].NeighbourIndexes, Minor[i].Value) != i)
{
Minor[i].NeighbourIndexes.Clear();
uniqueMinor.Add(Minor[i]);
continue;
}
PairedMinor.Add(Minor[i]);
PairedMajor.Add(Major[majorIndex]);
Major[majorIndex] = null;
}
List<Vertex> uniqueMajor = new List<Vertex>(Major.Count - (Minor.Count - uniqueMinor.Count) + 1);
uniqueMinor.TrimExcess();
Minor = uniqueMinor;
for (int i = 0; i < Major.Count; i++)
{
if (Major[i] == null)
continue;
Major[i].NeighbourIndexes.Clear();
uniqueMajor.Add(Major[i]);
}
Major = uniqueMajor;
}
private int GetNeigbourPointer(int minorPointer, int majorHintPointer)
{
if (Minor[minorPointer].Value == Major[majorHintPointer].Value)
return majorHintPointer;
double increaseLength;
if (majorHintPointer + 1 < Major.Count)
increaseLength = Math.Abs(Major[majorHintPointer].Value - Major[majorHintPointer + 1].Value);
else
return DecreaseLength(minorPointer, majorHintPointer);
double decreaseLength;
if (majorHintPointer > 0)
decreaseLength = Math.Abs(Major[majorHintPointer].Value - Major[majorHintPointer - 1].Value);
else
return IncreaseLength(minorPointer, majorHintPointer);
return increaseLength > decreaseLength ? DecreaseLength(minorPointer, majorHintPointer) : IncreaseLength(minorPointer, majorHintPointer);
}
#region helper methods
private int GetMinLenghtID(List<int> pointers, double value)
{
double min = Math.Abs(Minor[0].Value - value);
int bestPointer = 0;
for (int i = 1; i < pointers.Count; i++)
{
double length = Math.Abs(Minor[i].Value - value);
if (length < min)
{
min = length;
bestPointer = i;
}
}
return bestPointer;
}
private int IncreaseLength(int minorPointer, int majorHintPointer)
{
double minorValue = Minor[minorPointer].Value;
double previousValue = Math.Abs(minorValue - Major[majorHintPointer].Value);
int actual = majorHintPointer;
while (++actual < Major.Count)
{
double actualValue = Math.Abs(Major[actual].Value - minorValue);
if (actualValue < previousValue)
previousValue = actualValue;
else
return actual - 1;
}
return actual - 1;
}
private int DecreaseLength(int minorPointer, int majorHintPointer)
{
double minorValue = Minor[minorPointer].Value;
double previousValue = Math.Abs(minorValue - Major[majorHintPointer].Value);
int actual = majorHintPointer;
while (--actual >= 0)
{
double actualValue = Math.Abs(Major[actual].Value - minorValue);
if (actualValue < previousValue)
previousValue = actualValue;
else
return actual + 1;
}
return actual + 1;
}
private List<Vertex> GetVertexListFromIElements(List<IElement> elements)
{
List<Vertex> vertexList = new List<Vertex>(elements.Count);
foreach (IElement e in elements)
vertexList.Add(new Vertex(e));
return vertexList;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using TransactionComparator.Interface;
namespace TransactionComparator.Logic
{
class Vertex : IElement, IComparable
{
public bool Repetitive { get; set; }
public double Value { get; set; }
public Vertex Neighbour;
public List<int> NeighbourIndexes = new List<int>();
public int NeighbourIndex;
public Vertex(IElement e)
{
Value = e.Value;
}
public int CompareTo(object obj)
{
if (!(obj is IElement))
throw new ArgumentException("Argument not allowed");
IElement another = (IElement) obj;
if (Value < another.Value)
return -1;
if (Value > another.Value)
return 1;
return 0;
}
}
}
using System;
using System.Collections.Generic;
using TransactionComparator.Interface;
namespace TransactionComparator
{
class Program
{
static void Main(string[] args)
{
List<double> first = new List<double>() { -2029.00, -295.39, -193.00, -362.42, -17.00, -451.85, -4500.00, -150.25, -2836.50, -150.00, -357.60, -5000.00, -1.00, 5000.00, -155.60, -144.50, -87.50, -1249.00, -47.38, -189.00, -782.46, -20.00 };
List<double> second = new List<double>() { -25.00, 0.00, -320.38, 0.00, -3.00, 17.37, 0.00, -76.17, -579.00, -5.00, -513.50, -74.50, 80.61, 25.00, -411.33, -380.00, -2276.27, 2276.27, -500.00 };
List<double> testA = new List<double>() { 1, 2, 7, 11, 11 };
List<double> testB = new List<double>() { 1, 3, 8, 6 };
var inputA = ConvertListToPackageList(first).ConvertAll<IElement>(e => e);
var inputB = ConvertListToPackageList(second).ConvertAll<IElement>(e => e);
Console.WriteLine("Create comparator:");
Comparator cmp = new Comparator(inputA, inputB);
Console.WriteLine("Compare:");
cmp.Compare();
Console.WriteLine("Write pairs:");
cmp.WritePairs();
Console.WriteLine("Write undetermined:");
cmp.WriteUndetermined();
Console.WriteLine("Done");
}
class Packager : IElement
{
public double Value { get; private set; }
public bool Repetitive { get; set; }
public Packager(double value)
{
Value = value;
}
}
private static List<Packager> ConvertListToPackageList(List<double> list)
{
List<Packager> returnList = new List<Packager>(list.Count);
foreach (double e in list)
returnList.Add(new Packager(e));
return returnList;
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
</Project>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment