NPKTools.Optimizer 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NPKTools.Optimizer --version 1.1.0                
NuGet\Install-Package NPKTools.Optimizer -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NPKTools.Optimizer" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NPKTools.Optimizer --version 1.1.0                
#r "nuget: NPKTools.Optimizer, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NPKTools.Optimizer as a Cake Addin
#addin nuget:?package=NPKTools.Optimizer&version=1.1.0

// Install NPKTools.Optimizer as a Cake Tool
#tool nuget:?package=NPKTools.Optimizer&version=1.1.0                

NPKTools.Optimizer

NPKTools.Optimizer is a tool developed on the .NET platform that helps adjust the ratio of each fertilizer to achieve the desired PPM (parts per million) profile. Additionally, it is capable of calculating the PPM of a mixture of fertilizers.

  • The FertilizerOptimizer is composed of three main components: an adapter, a mapper, and a solver. It takes a nutrient target in NPK values, a list of available fertilizers, and specific precision settings as inputs to calculate the optimal fertilizer mix. Here's how the flow works with these components:
  • Mapper: This component converts the high-level nutrient targets and the list of available fertilizers into a structured optimization problem. It translates real-world data about fertilizers and nutrient requirements into mathematical variables, constraints, and objectives suitable for optimization. After the solver processes these, the mapper also translates the optimized mathematical solution back into practical terms, specifying exact quantities of each fertilizer to use.
  • Solver: Once the optimization problem is defined, this component uses mathematical algorithms to find the best combination of fertilizers. The solver processes the variables and constraints set by the mapper to minimize or maximize the objective function, typically focusing on cost efficiency or nutrient precision.
  • Adapter: Acts as the bridge between the domain-specific data (fertilizers and nutrient targets) and the optimization tools. It takes the problem as defined by the mapper, passes it to the solver for optimization, and then interprets the output from the solver back into a practical solution, detailing the specific amounts of each fertilizer to use to achieve the nutrient targets.
  • Together, these components ensure that the FertilizerOptimizer effectively integrates and processes the inputs to produce an optimized fertilizer mix that meets the specified NPK goals with the required precision.

Developers

This tool was developed by Anatoliy Yermakov.

Special thanks to Artem Frolov for his invaluable assistance and guidance in the development of this project.

License:

This project is licensed under the MIT License.

Installation and Configuration

This section guides you through setting up and configuring the necessary components for the project using Dependency Injection (DI).

Direct Instantiation

To manually instantiate the components without using DI, use the following example:

// Creating instances manually without DI
    IOptimizationProblemSolver solver = new GoogleOrToolsOptimizationSolver();
    IOptimizationProblemMapper mapper = new OptimizationProblemMapper();
    IFertilizerOptimizer optimizer = new FertilizerOptimizationAdapter(solver, mapper);

Using Dependency Injection For integrating these components into a project that supports Dependency Injection, such as an ASP.NET Core application, configure your services in the Startup.cs or a similar configuration file as follows:

public void ConfigureServices(IServiceCollection services)
{
// Registering services in the DI container
    services.AddSingleton<IOptimizationProblemSolver, GoogleOrToolsOptimizationSolver>();
    services.AddSingleton<IOptimizationProblemMapper, OptimizationProblemMapper>();
    services.AddSingleton<FertilizerOptimizationAdapter>();
}

Example usage

// Setup the optimizer
IOptimizationProblemSolver solver = new GoogleOrToolsOptimizationSolver();
IOptimizationProblemMapper mapper = new OptimizationProblemMapper();
IFertilizerOptimizer optimizer = new FertilizerOptimizationAdapter(solver, mapper);

// Define your PPM target for optimization
PpmTarget target = new PpmTargetBuilder()
    .AddN(150)
    .AddP(50)
    .AddK(200)
    .AddMg(60)
    .AddCa(60)
    .AddS(80)
    .Build();

// Configure the solution finder settings
SolutionFinderSettings settings = new SolutionFinderSettingsBuilder()
    .AddN(1)
    .AddP(1)
    .AddK(1)
    .AddCa(1)
    .AddMg(1)
    .AddS(1)
    .AddCl(1)
    .Build();

// Create a list of fertilizers for optimization
IList<FertilizerOptimizationModel> collection = new List<FertilizerOptimizationModel>()
{
    new FertilizerBuilder().AddNo3(11.863).AddCaNonChelated(16.972).Build(),
    new FertilizerBuilder().AddNo3(13.854).AddK(38.672).Build(),
    new FertilizerBuilder().AddNo3(17.499).AddNh4(17.499).Build(),
    new FertilizerBuilder().AddMgNonChelated(9.861).AddS(13.008).Build(),
    new FertilizerBuilder().AddP(22.761).AddK(28.731).Build(),
    new FertilizerBuilder().AddS(18.401).AddK(44.874).Build(),
    new FertilizerBuilder().AddMgNonChelated(9.479).AddNo3(10.925).Build(),
    new FertilizerBuilder().AddS(18.969).AddMnNonChelated(32.506).Build()
};

// Optimize the solution
Solution solution = optimizer.Optimize(target, collection, settings);

Dependencies

NPKOptimizer

NPKOptimizer.Tests

Contact Information:

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NPKTools.Optimizer:

Package Downloads
NPKTools.Optimizer.Preset

NPKTools.Optimizer.Preset is an preconfigured version of the NPKTools.Optimizer. It includes: 17 basic types of macronutrient fertilizers combined into 18 different sets to optimize the selection process. 17 types of micronutrient fertilizers grouped into four main sets: basic, sulfate, nitrate, and chelated. During the selection of macronutrient fertilizers, the service conducts two searches: in the first, sulfur is accounted for as specified, while the second search excludes sulfur coefficients to expand the possible options.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 119 5/14/2024
1.1.2 114 5/14/2024
1.1.1 115 5/3/2024
1.1.0 86 5/3/2024
1.0.0 100 5/2/2024

Combined FertilizerOptimizationModel and Fertilizer to Fertilizer