BestCombination
This commit is contained in:
104
SawCut/BestCombination.cs
Normal file
104
SawCut/BestCombination.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
|
||||
namespace SawCut
|
||||
{
|
||||
public static class BestCombination
|
||||
{
|
||||
public static BestComboResult FindFrom2(double length1, double length2, double stockLength)
|
||||
{
|
||||
var result = new BestComboResult();
|
||||
result.Item1Length = length1;
|
||||
result.Item2Length = length2;
|
||||
|
||||
stockLength += Tolerance.Epsilon;
|
||||
|
||||
if (length1 > stockLength)
|
||||
{
|
||||
if (length2 > stockLength)
|
||||
{
|
||||
//throw new Exception($"Either {nameof(length1)} or {nameof(length2)} must be less than or equal to the {nameof(stockLength)}");
|
||||
return null;
|
||||
}
|
||||
|
||||
result.Item1Count = 0;
|
||||
result.Item2Count = (int)Math.Floor(stockLength / length2);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (length2 > stockLength)
|
||||
{
|
||||
result.Item1Count = (int)Math.Floor(stockLength / length1);
|
||||
result.Item2Count = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
var maxCountLength1 = (int)Math.Floor(stockLength / length1);
|
||||
|
||||
result.Item1Count = maxCountLength1;
|
||||
result.Item2Count = 0;
|
||||
|
||||
var remnant = stockLength - maxCountLength1 * length1;
|
||||
|
||||
if (remnant.IsEqualTo(0))
|
||||
return result;
|
||||
|
||||
for (int countLength1 = 0; countLength1 <= maxCountLength1; ++countLength1)
|
||||
{
|
||||
var remnant1 = stockLength - countLength1 * length1;
|
||||
|
||||
if (remnant1 >= length2)
|
||||
{
|
||||
var countLength2 = (int)Math.Floor(remnant1 / length2);
|
||||
var remnant2 = remnant1 - length2 * countLength2;
|
||||
|
||||
if (!(remnant2 < remnant))
|
||||
continue;
|
||||
|
||||
result.Item1Count = countLength1;
|
||||
result.Item2Count = countLength2;
|
||||
|
||||
if (remnant2.IsEqualTo(0))
|
||||
break;
|
||||
|
||||
remnant = remnant2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(remnant1 < remnant))
|
||||
continue;
|
||||
|
||||
result.Item1Count = countLength1;
|
||||
result.Item2Count = 0;
|
||||
|
||||
if (remnant1.IsEqualTo(0))
|
||||
break;
|
||||
|
||||
remnant = remnant1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class BestComboResult
|
||||
{
|
||||
public double Item1Length { get; set; }
|
||||
|
||||
public int Item1Count { get; set; }
|
||||
|
||||
public double Item1TotalLength
|
||||
{
|
||||
get { return Item1Length * Item1Count; }
|
||||
}
|
||||
|
||||
public double Item2Length { get; set; }
|
||||
|
||||
public int Item2Count { get; set; }
|
||||
|
||||
public double Item2TotalLength
|
||||
{
|
||||
get { return Item2Length * Item2Count; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ArchUnits.cs" />
|
||||
<Compile Include="BestCombination.cs" />
|
||||
<Compile Include="Bin.cs" />
|
||||
<Compile Include="BinItem.cs" />
|
||||
<Compile Include="Fraction.cs" />
|
||||
@@ -52,6 +53,7 @@
|
||||
<Compile Include="Nesting\Result.cs" />
|
||||
<Compile Include="Nesting\Engine2.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Tolerance.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
14
SawCut/Tolerance.cs
Normal file
14
SawCut/Tolerance.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace SawCut
|
||||
{
|
||||
public static class Tolerance
|
||||
{
|
||||
public const double Epsilon = 0.00001;
|
||||
|
||||
public static bool IsEqualTo(this double a, double b, double tolerance = Epsilon)
|
||||
{
|
||||
return Math.Abs(b - a) <= tolerance;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user