Crack Calculator was added
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Calculators
|
||||
{
|
||||
public class FindParameterCalculator : ICalculator
|
||||
{
|
||||
FindParameterResult result;
|
||||
public string Name { get; set; }
|
||||
public double StartValue { get; set; }
|
||||
public double EndValue { get; set; }
|
||||
public Predicate<double> Predicate { get; set; }
|
||||
public IAccuracy Accuracy {get;set;}
|
||||
public IResult Result => result;
|
||||
|
||||
public FindParameterCalculator()
|
||||
{
|
||||
StartValue = 0d;
|
||||
EndValue = 1d;
|
||||
Accuracy = new Accuracy() { IterationAccuracy = 0.001, MaxIterationCount = 1000 };
|
||||
}
|
||||
public void Run()
|
||||
{
|
||||
result = new() { IsValid = true};
|
||||
try
|
||||
{
|
||||
result.Parameter = FindMinimumValue(StartValue, EndValue, Predicate);
|
||||
result.Description = "Parameter was found succefully";
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
result.IsValid = false;
|
||||
result.Description += ex;
|
||||
}
|
||||
}
|
||||
public object Clone()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private double FindMinimumValue(double start, double end, Predicate<double> predicate)
|
||||
{
|
||||
if (predicate(end) == false)
|
||||
{
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ": pridicate for end value must be true");
|
||||
|
||||
}
|
||||
double precision = Accuracy.IterationAccuracy;
|
||||
int maxIterationCount = Accuracy.MaxIterationCount;
|
||||
double current = start;
|
||||
double step = (end - start) / 2;
|
||||
int iterationNum = 0;
|
||||
while (step > precision)
|
||||
{
|
||||
if (predicate(current))
|
||||
{
|
||||
end = current;
|
||||
}
|
||||
else
|
||||
{
|
||||
start = current;
|
||||
}
|
||||
|
||||
current = (start + end) / 2;
|
||||
step = (end - start) / 2;
|
||||
iterationNum++;
|
||||
if (iterationNum > maxIterationCount)
|
||||
{
|
||||
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ": violation of iteration count");
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Calculators
|
||||
{
|
||||
public class FindParameterResult : IResult
|
||||
{
|
||||
public bool IsValid { get; set; }
|
||||
public string Description { get; set; }
|
||||
public double Parameter { get; set; }
|
||||
}
|
||||
}
|
||||
23
StructureHelperCommon/Models/Calculators/ICalculator.cs
Normal file
23
StructureHelperCommon/Models/Calculators/ICalculator.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using TaskManager;
|
||||
|
||||
namespace StructureHelperCommon.Models.Calculators
|
||||
{
|
||||
public interface ICalculator : ICloneable
|
||||
{
|
||||
string Name { get; set; }
|
||||
/// <summary>
|
||||
/// Method for calculating
|
||||
/// </summary>
|
||||
void Run();
|
||||
/// <summary>
|
||||
/// Result of Calculations
|
||||
/// </summary>
|
||||
IResult Result { get; }
|
||||
}
|
||||
}
|
||||
17
StructureHelperCommon/Models/Calculators/IResult.cs
Normal file
17
StructureHelperCommon/Models/Calculators/IResult.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Calculators
|
||||
{
|
||||
public interface IResult
|
||||
{
|
||||
/// <summary>
|
||||
/// True if result of calculation is valid
|
||||
/// </summary>
|
||||
bool IsValid { get; set; }
|
||||
string Description { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Models.Calculators
|
||||
{
|
||||
public class AccuracyUpdateStrategy : IUpdateStrategy<IAccuracy>
|
||||
{
|
||||
public void Update(IAccuracy targetObject, IAccuracy sourceObject)
|
||||
{
|
||||
targetObject.IterationAccuracy = sourceObject.IterationAccuracy;
|
||||
targetObject.MaxIterationCount = sourceObject.MaxIterationCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user