CSharp: Iterative Algorithms

项目结构:

cs 复制代码
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : Settings.cs
 
 */
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Config
{
 
    /// <summary>
    ///
    /// </summary>
    public static class Settings
    {
        //钻石评分权重
        public const double DiamondWeightCarat = 0.5;
        public const double DiamondWeightColor = 0.3;
        public const double DiamondWeightClarity = 0.2;
 
        public const double PriceIterStep = 0.01;
        public const double MinMarkupCoeff = 1.3;
        public const double MaxMarkupCoeff = 2.2;
    }
}
 
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : Diamond.cs
 
 */
 
using CSharpAlgorithms.Iterative.Config;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Models
{
 
 
    /// <summary>
    ///
    /// </summary>
    public class Diamond
    {
        public string StoneId { get; set; } = string.Empty;
        public double Carat { get; set; }
        public string Color { get; set; } = string.Empty;
        public string Clarity { get; set; } = string.Empty;
        public double CostPrice { get; set; }
 
        public double CalcComprehensiveScore()
        {
            Dictionary<string, double> colorMap = new()
        {
            {"D",100},{"E",96},{"F",92},{"G",88},{"H",84},{"I",78}
        };
            Dictionary<string, double> clarityMap = new()
        {
            {"FL",100},{"VVS1",95},{"VVS2",90},{"VS1",85},{"VS2",80},{"SI1",70}
        };
 
            double cScore = Carat * 100;
            double colScore = colorMap.GetValueOrDefault(Color, 60);
            double claScore = clarityMap.GetValueOrDefault(Clarity, 60);
 
            return cScore * Settings.DiamondWeightCarat
                   + colScore * Settings.DiamondWeightColor
                   + claScore * Settings.DiamondWeightClarity;
        }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : JadeBlank.cs
 
 */
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Models
{
 
    /// <summary>
    ///
    /// </summary>
    public class JadeBlank
    {
        public string BlankId { get; set; } = string.Empty;
        public double Length { get; set; }
        public double Width { get; set; }
        public double Thick { get; set; }
        public double CostPrice { get; set; }
        public double UsableRate { get; set; }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : Product.cs
 
 */
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Models
{
 
    /// <summary>
    ///
    /// </summary>
    public class Product
    {
        public string ProductId { get; set; } = string.Empty;
        public string Name { get; set; } = string.Empty;
        public double Cost { get; set; }
    }
}
 
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : RingMount.cs
 
 */
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Models
{
 
    /// <summary>
    ///
    /// </summary>
    public class RingMount
    {
        public string MountId { get; set; } = string.Empty;
        public string Material { get; set; } = string.Empty;
        public double CostPrice { get; set; }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : DiamondRepository.cs
 
 */
 
using CSharpAlgorithms.Iterative.Models;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Repository
{
 
    /// <summary>
    ///
    /// </summary>
    public class DiamondRepository : IBaseRepository<Diamond>
    {
        private readonly List<Diamond> _storage = new()
    {
        new Diamond{StoneId="D001",Carat=0.52,Color="H",Clarity="VS1",CostPrice=24800},
        new Diamond{StoneId="D002",Carat=0.48,Color="G",Clarity="VS2",CostPrice=22100},
        new Diamond{StoneId="D003",Carat=0.55,Color="I",Clarity="SI1",CostPrice=21300},
    };
 
        public List<Diamond> ListAll()
        {
            return new List<Diamond>(_storage);
        }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : IBaseRepository.cs
 
 */
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Repository
{
 
    /// <summary>
    ///
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IBaseRepository<T>
    {
        List<T> ListAll();
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : JadeRepository.cs
 
 */
using CSharpAlgorithms.Iterative.Models;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Repository
{
 
    /// <summary>
    ///
    /// </summary>
    public class JadeRepository : IBaseRepository<JadeBlank>
    {
        private readonly List<JadeBlank> _storage = new()
    {
        new JadeBlank{BlankId="J001",Length=32.5,Width=21.2,Thick=7.3,CostPrice=6800,UsableRate=0.86},
        new JadeBlank{BlankId="J002",Length=30.1,Width=19.8,Thick=6.9,CostPrice=6200,UsableRate=0.91},
        new JadeBlank{BlankId="J003",Length=36.0,Width=24.1,Thick=8.2,CostPrice=7500,UsableRate=0.79},
    };
 
        public List<JadeBlank> ListAll()
        {
            return new List<JadeBlank>(_storage);
        }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : RingMountRepository.cs
 
 */
 
using CSharpAlgorithms.Iterative.Models;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Repository
{
 
    /// <summary>
    ///
    /// </summary>
    public class RingMountRepository : IBaseRepository<RingMount>
    {
        private readonly List<RingMount> _storage = new()
    {
        new RingMount{MountId="M001",Material="18K",CostPrice=4200},
        new RingMount{MountId="M002",Material="铂金",CostPrice=5600},
    };
 
        public List<RingMount> ListAll()
        {
            return new List<RingMount>(_storage);
        }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : Request.cs
 
 */
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Schemas
{
 
    /// <summary>
    ///
    /// </summary>
    /// <param name="Budget"></param>
    /// <param name="Material"></param>
    public record DiamondRecommendRequest(double Budget, string Material);
    /// <summary>
    ///
    /// </summary>
    /// <param name="MinL"></param>
    /// <param name="MaxL"></param>
    /// <param name="MinW"></param>
    /// <param name="MaxW"></param>
    /// <param name="MinT"></param>
    /// <param name="MaxT"></param>
    /// <param name="PriceMin"></param>
    /// <param name="PriceMax"></param>
    public record JadeMatchRequest(
        double MinL, double MaxL,
        double MinW, double MaxW,
        double MinT, double MaxT,
        double PriceMin, double PriceMax);
 
 
    /// <summary>
    ///
    /// </summary>
    /// <param name="BaseCost"></param>
    /// <param name="MinCoeff"></param>
    /// <param name="MaxCoeff"></param>
    /// <param name="Step"></param>
    public record PriceOptRequest(double BaseCost, double MinCoeff, double MaxCoeff, double Step);
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : Response.cs
 
 */
 
using CSharpAlgorithms.Iterative.Models;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Schemas
{
 
    /// <summary>
    ///
    /// </summary>
    /// <param name="Diamond"></param>
    /// <param name="Mount"></param>
    /// <param name="TotalCost"></param>
    /// <param name="Score"></param>
    public record DiamondComboResult(Diamond Diamond, RingMount Mount, double TotalCost, double Score);
    /// <summary>
    ///
    /// </summary>
    /// <param name="Blank"></param>
    public record JadeMatchResult(JadeBlank Blank);
    /// <summary>
    ///
    /// </summary>
    /// <param name="BestCoeff"></param>
    /// <param name="BestSellPrice"></param>
    /// <param name="MaxGrossProfit"></param>
    public record PriceOptResult(double BestCoeff, double BestSellPrice, double MaxGrossProfit);
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : DiamondRecommendService.cs
 
 */
using CSharpAlgorithms.Iterative.Models;
using CSharpAlgorithms.Iterative.Repository;
using CSharpAlgorithms.Iterative.Schemas;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Services
{
 
    /// <summary>
    ///
    /// </summary>
    public class DiamondRecommendService
    {
        private readonly IBaseRepository<Diamond> _diaRepo;
        private readonly IBaseRepository<RingMount> _mountRepo;
        /// <summary>
        ///
        /// </summary>
        /// <param name="diaRepo"></param>
        /// <param name="mountRepo"></param>
        public DiamondRecommendService(IBaseRepository<Diamond> diaRepo, IBaseRepository<RingMount> mountRepo)
        {
            _diaRepo = diaRepo;
            _mountRepo = mountRepo;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public DiamondComboResult? FindBestCombo(DiamondRecommendRequest req)
        {
            var diamonds = _diaRepo.ListAll();
            var mounts = _mountRepo.ListAll();
 
            DiamondComboResult? best = null;
            double bestScore = -1.0;
 
            foreach (var dia in diamonds)
            {
                foreach (var mount in mounts)
                {
                    if (mount.Material != req.Material) continue;
                    double total = dia.CostPrice + mount.CostPrice;
                    if (total > req.Budget) continue;
 
                    double score = dia.CalcComprehensiveScore();
                    if (score > bestScore)
                    {
                        bestScore = score;
                        best = new DiamondComboResult(dia, mount, total, score);
                    }
                }
            }
            return best;
        }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : JadeCustomService.cs
 
 */
using CSharpAlgorithms.Iterative.Algorithms;
using CSharpAlgorithms.Iterative.Models;
using CSharpAlgorithms.Iterative.Repository;
using CSharpAlgorithms.Iterative.Schemas;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Services
{
 
    /// <summary>
    ///
    /// </summary>
    public class JadeCustomService
    {
        private readonly IBaseRepository<JadeBlank> _jadeRepo;
        private readonly IterativeMatcher _matcher;
        /// <summary>
        ///
        /// </summary>
        /// <param name="jadeRepo"></param>
        public JadeCustomService(IBaseRepository<JadeBlank> jadeRepo)
        {
            _jadeRepo = jadeRepo;
            _matcher = new IterativeMatcher();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public JadeMatchResult? MatchOptimalBlank(JadeMatchRequest req)
        {
            var blanks = _jadeRepo.ListAll();
 
            Func<JadeBlank, bool> filter = item =>
            {
                bool condSize = req.MinL <= item.Length && item.Length <= req.MaxL
                                && req.MinW <= item.Width && item.Width <= req.MaxW
                                && req.MinT <= item.Thick && item.Thick <= req.MaxT;
 
                bool condPrice = req.PriceMin <= item.CostPrice && item.CostPrice <= req.PriceMax;
                return condSize && condPrice;
            };
 
            Func<JadeBlank, double> score = item => item.UsableRate;
 
            var best = _matcher.FindOptimal(blanks, filter, score);
            return best == null ? null : new JadeMatchResult(best);
        }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : ProductPricingService.cs
 
 */
 
using CSharpAlgorithms.Iterative.Algorithms;
using CSharpAlgorithms.Iterative.Schemas;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Services
{
 
    /// <summary>
    ///
    /// </summary>
    public class ProductPricingService
    {
        private readonly IterativeOptimizer _optimizer;
        /// <summary>
        /// /
        /// </summary>
        public ProductPricingService()
        {
            _optimizer = new IterativeOptimizer();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public PriceOptResult CalcOptimalPrice(PriceOptRequest req)
        {
            double baseCost = req.BaseCost;
 
            Func<double, double> eval = coeff =>
            {
                double sellPrice = baseCost * coeff;
                double volume = Math.Max(0, 120 - sellPrice / 45);
                double profit = (sellPrice - baseCost) * volume;
                return profit;
            };
 
            var (bestCoeff, maxProfit) = _optimizer.Optimize(req.MinCoeff, req.MaxCoeff, req.Step, eval);
            double bestPrice = Math.Round(baseCost * bestCoeff, 2);
 
            return new PriceOptResult(bestCoeff, bestPrice, maxProfit);
        }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : IterativeMatcher.cs
 
 */
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Algorithms
{
    /// <summary>
    /// 通用迭代匹配器 Iterative Algorithms
    /// </summary>
    public class IterativeMatcher
    {
        /// <summary>
        /// 迭代查找最优对象,返回副本,外部修改不会污染原始集合
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="candidates">候选集合</param>
        /// <param name="filter">过滤条件</param>
        /// <param name="score">评分函数,越高越优</param>
        /// <returns>最优实体;无匹配返回null</returns>
        public T? FindOptimal<T>(IEnumerable<T> candidates, Func<T, bool> filter, Func<T, double> score) where T : class
        {
            int bestIndex = -1;
            double bestScore = -1e18;
            List<T> list = candidates.ToList();
 
            for (int i = 0; i < list.Count; i++)
            {
                T item = list[i];
                if (!filter(item)) continue;
 
                double s = score(item);
                if (s > bestScore)
                {
                    bestScore = s;
                    bestIndex = i;
                }
            }
 
            if (bestIndex < 0) return null;
            // 返回独立副本(如需深度克隆可扩展,当前简单场景浅拷贝足够)
            return list[bestIndex];
        }
    }
}
 
 
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : IterativeOptimizer.cs
 
 */
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Iterative.Algorithms
{
 
    /// <summary>
    ///
    /// </summary>
    public class IterativeOptimizer
    {
        /// <summary>
        /// 连续区间迭代寻优
        /// </summary>
        /// <param name="start">起始参数</param>
        /// <param name="end">终止参数</param>
        /// <param name="step">步长</param>
        /// <param name="eval">评估函数</param>
        /// <returns>(最优参数,最大目标值)</returns>
        public (double bestParam, double maxTarget) Optimize(double start, double end, double step, Func<double, double> eval)
        {
            double bestParam = start;
            double maxTarget = double.MinValue;
            double current = start;
 
            while (current <= end)
            {
                double val = eval(current);
                if (val > maxTarget)
                {
                    maxTarget = val;
                    bestParam = current;
                }
                current += step;
            }
            return (Math.Round(bestParam, 2), Math.Round(maxTarget, 2));
        }
    }
}
  

调用:

cs 复制代码
/*
 # encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Iterative Algorithms
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs2026 c# .net 10
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/07/12 22:22
# User      :  geovindu
# Product   : Visual Studio 2026
# Project   : CSharpDesignPattern
# File      : IterativeBll.cs
 
 */
 
 
using CSharpAlgorithms.Iterative.Config;
using CSharpAlgorithms.Iterative.Repository;
using CSharpAlgorithms.Iterative.Schemas;
using CSharpAlgorithms.Iterative.Services;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CSharpAlgorithms.Bll
{
 
    /// <summary>
    ///
    /// </summary>
    public class IterativeBll
    {
        /// <summary>
        /// 演示:钻戒推荐
        /// </summary>
        void DemoRecommend()
        {
            var diaRepo = new DiamondRepository();
            var mountRepo = new RingMountRepository();
            var svc = new DiamondRecommendService(diaRepo, mountRepo);
            var req = new DiamondRecommendRequest(30000, "18K");
            var res = svc.FindBestCombo(req);
 
            if (res != null)
            {
                Console.WriteLine("==== 钻戒最优组合 ====");
                Console.WriteLine($"主石:{res.Diamond.StoneId}, {res.Diamond.Carat:F2}ct");
                Console.WriteLine($"戒托:{res.Mount.MountId}");
                Console.WriteLine($"合计成本:{res.TotalCost:F2},综合评分:{res.Score:F2}");
            }
        }
 
        /// <summary>
        /// 演示:翡翠毛坯匹配
        /// </summary>
        void DemoJadeMatch()
        {
            var repo = new JadeRepository();
            var svc = new JadeCustomService(repo);
            var req = new JadeMatchRequest(
                MinL: 28, MaxL: 33,
                MinW: 18, MaxW: 22,
                MinT: 6, MaxT: 8,
                PriceMin: 5000, PriceMax: 7000);
 
            var res = svc.MatchOptimalBlank(req);
            if (res != null)
            {
                Console.WriteLine("\n==== 匹配翡翠毛料 ====");
                Console.WriteLine($"毛料ID:{res.Blank.BlankId},利用率:{res.Blank.UsableRate:F2}");
            }
        }
 
        /// <summary>
        /// 演示:动态定价演算
        /// </summary>
        void DemoPricing()
        {
            var svc = new ProductPricingService();
            var req = new PriceOptRequest(
                BaseCost: 1680,
                MinCoeff: Settings.MinMarkupCoeff,
                MaxCoeff: Settings.MaxMarkupCoeff,
                Step: Settings.PriceIterStep);
 
            var res = svc.CalcOptimalPrice(req);
            Console.WriteLine("\n==== 动态定价结果 ====");
            Console.WriteLine($"最优加价系数:{res.BestCoeff:F2}");
            Console.WriteLine($"建议售价:{res.BestSellPrice:F2}");
            Console.WriteLine($"预期最大毛利:{res.MaxGrossProfit:F2}");
        }
        /// <summary>
        ///
        /// </summary>
        public void Demo()
        {
            DemoRecommend();
            DemoJadeMatch();
            DemoPricing();
 
        }
    }
}
  

输出:

相关推荐
一只月月鸟呀1 小时前
移动端tap与click的区别 && 点透事件
开发语言·前端·javascript
雨落在了我的手上1 小时前
Java数据结构(六):链表的介绍
java·开发语言·数据结构
影寂ldy1 小时前
C# HttpClient 分片下载、断点续传、暂停取消(完整项目知识点)
开发语言·c#
雪隐1 小时前
AI股票小助手10-我用代码管住自己的手:一个普通人的市场观察笔记
前端·人工智能·后端
独隅1 小时前
DevEco Code Plan+Build 双 Agent 协同开发效率实测
java·开发语言
道影子2 小时前
《黄帝内经》021章|津凝精晶 沉降为患
人工智能·深度学习·神经网络·算法·机器学习
道影子2 小时前
《黄帝内经》022章|调和双旋 伏风自消
人工智能·深度学习·神经网络·算法·机器学习
艾斯特_2 小时前
混合检索与重排:提升召回与排序质量
后端·python·ai
KaMeidebaby2 小时前
卡梅德生物技术快报|抗体筛选:抗体筛选中的噬菌体展示四参数调优——从流程设计到数据验证
开发语言·前端·javascript