CSharp: Iterative Algorithms
项目结构/* # 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() { Dictionarystring, double colorMap new() { {D,100},{E,96},{F,92},{G,88},{H,84},{I,78} }; Dictionarystring, 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 : IBaseRepositoryDiamond { private readonly ListDiamond _storage new() { new Diamond{StoneIdD001,Carat0.52,ColorH,ClarityVS1,CostPrice24800}, new Diamond{StoneIdD002,Carat0.48,ColorG,ClarityVS2,CostPrice22100}, new Diamond{StoneIdD003,Carat0.55,ColorI,ClaritySI1,CostPrice21300}, }; public ListDiamond ListAll() { return new ListDiamond(_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 nameT/typeparam public interface IBaseRepositoryT { ListT 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 : IBaseRepositoryJadeBlank { private readonly ListJadeBlank _storage new() { new JadeBlank{BlankIdJ001,Length32.5,Width21.2,Thick7.3,CostPrice6800,UsableRate0.86}, new JadeBlank{BlankIdJ002,Length30.1,Width19.8,Thick6.9,CostPrice6200,UsableRate0.91}, new JadeBlank{BlankIdJ003,Length36.0,Width24.1,Thick8.2,CostPrice7500,UsableRate0.79}, }; public ListJadeBlank ListAll() { return new ListJadeBlank(_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 : IBaseRepositoryRingMount { private readonly ListRingMount _storage new() { new RingMount{MountIdM001,Material18K,CostPrice4200}, new RingMount{MountIdM002,Material铂金,CostPrice5600}, }; public ListRingMount ListAll() { return new ListRingMount(_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 nameBudget/param /// param nameMaterial/param public record DiamondRecommendRequest(double Budget, string Material); /// summary /// /// /summary /// param nameMinL/param /// param nameMaxL/param /// param nameMinW/param /// param nameMaxW/param /// param nameMinT/param /// param nameMaxT/param /// param namePriceMin/param /// param namePriceMax/param public record JadeMatchRequest( double MinL, double MaxL, double MinW, double MaxW, double MinT, double MaxT, double PriceMin, double PriceMax); /// summary /// /// /summary /// param nameBaseCost/param /// param nameMinCoeff/param /// param nameMaxCoeff/param /// param nameStep/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 nameDiamond/param /// param nameMount/param /// param nameTotalCost/param /// param nameScore/param public record DiamondComboResult(Diamond Diamond, RingMount Mount, double TotalCost, double Score); /// summary /// /// /summary /// param nameBlank/param public record JadeMatchResult(JadeBlank Blank); /// summary /// /// /summary /// param nameBestCoeff/param /// param nameBestSellPrice/param /// param nameMaxGrossProfit/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 IBaseRepositoryDiamond _diaRepo; private readonly IBaseRepositoryRingMount _mountRepo; /// summary /// /// /summary /// param namediaRepo/param /// param namemountRepo/param public DiamondRecommendService(IBaseRepositoryDiamond diaRepo, IBaseRepositoryRingMount mountRepo) { _diaRepo diaRepo; _mountRepo mountRepo; } /// summary /// /// /summary /// param namereq/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 IBaseRepositoryJadeBlank _jadeRepo; private readonly IterativeMatcher _matcher; /// summary /// /// /summary /// param namejadeRepo/param public JadeCustomService(IBaseRepositoryJadeBlank jadeRepo) { _jadeRepo jadeRepo; _matcher new IterativeMatcher(); } /// summary /// /// /summary /// param namereq/param /// returns/returns public JadeMatchResult? MatchOptimalBlank(JadeMatchRequest req) { var blanks _jadeRepo.ListAll(); FuncJadeBlank, 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; }; FuncJadeBlank, 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 namereq/param /// returns/returns public PriceOptResult CalcOptimalPrice(PriceOptRequest req) { double baseCost req.BaseCost; Funcdouble, 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 nameT实体类型/typeparam /// param namecandidates候选集合/param /// param namefilter过滤条件/param /// param namescore评分函数越高越优/param /// returns最优实体无匹配返回null/returns public T? FindOptimalT(IEnumerableT candidates, FuncT, bool filter, FuncT, double score) where T : class { int bestIndex -1; double bestScore -1e18; ListT 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 namestart起始参数/param /// param nameend终止参数/param /// param namestep步长/param /// param nameeval评估函数/param /// returns(最优参数,最大目标值)/returns public (double bestParam, double maxTarget) Optimize(double start, double end, double step, Funcdouble, 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)); } } }调用/* # 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(); } } }输出