python:Iterative Algorithms

python:Iterative Algorithms

项目结构:

# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:09 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : settings.py """业务全局配置,统一管理阈值、权重,避免代码硬编码""" # 钻石综合评分权重 DIAMOND_WEIGHT_CARAT = 0.5 DIAMOND_WEIGHT_COLOR = 0.3 DIAMOND_WEIGHT_CLARITY = 0.2 # 定价迭代默认步长 PRICE_ITER_STEP = 0.01 # 加价区间默认边界 MIN_MARKUP_COEFF = 1.3 MAX_MARKUP_COEFF = 2.2 # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:10 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : diamond.py from dataclasses import dataclass @dataclass(frozen=False) class Diamond: """ 钻石裸石领域模型 """ stone_id: str carat: float color: str clarity: str cost_price: float # 成本价 def calc_comprehensive_score(self) -> float: """ 计算钻石综合品级分数 :return: """ from Iterative.config.settings import DIAMOND_WEIGHT_CARAT, DIAMOND_WEIGHT_COLOR, DIAMOND_WEIGHT_CLARITY color_map = {"D": 100, "E":96, "F":92, "G":88, "H":84, "I":78} clarity_map = {"FL":100, "VVS1":95, "VVS2":90, "VS1":85, "VS2":80, "SI1":70} c_score = self.carat * 100 col_score = color_map.get(self.color, 60) cla_score = clarity_map.get(self.clarity, 60) total = (c_score * DIAMOND_WEIGHT_CARAT + col_score * DIAMOND_WEIGHT_COLOR + cla_score * DIAMOND_WEIGHT_CLARITY) return round(total, 2) # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:12 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : ring_mount.py from dataclasses import dataclass @dataclass class RingMount: """ 戒托模型 """ mount_id: str material: str # "18K", "铂金" cost_price: float # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:13 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : jade_blank.py from dataclasses import dataclass @dataclass class JadeBlank: """ 翡翠毛料实体 """ blank_id: str length: float width: float thick: float cost_price: float usable_rate: float # 成品利用率 # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:14 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : request.py from dataclasses import dataclass @dataclass class DiamondRecommendRequest: """ 钻戒推荐入参 """ budget: float material: str @dataclass class JadeMatchRequest: """ 翡翠定制匹配入参 """ min_l: float max_l: float min_w: float max_w: float min_t: float max_t: float price_min: float price_max: float @dataclass class PriceOptRequest: """ 定价演算入参 """ base_cost: float min_coeff: float max_coeff: float step: float # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:14 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : response.py from dataclasses import dataclass from Iterative.models.diamond import Diamond from Iterative.models.ring_mount import RingMount from Iterative.models.jade_blank import JadeBlank @dataclass class DiamondComboResult: """ """ diamond: Diamond mount: RingMount total_cost: float score: float @dataclass class JadeMatchResult: """ """ blank: JadeBlank @dataclass class PriceOptResult: """ """ best_coeff: float best_sell_price: float max_gross_profit: float # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:35 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : iterative_matcher.py from typing import Callable, Optional, TypeVar T = TypeVar("T") class IterativeMatcher: """ 通用迭代匹配器:迭代遍历候选集,筛选并保留最优对象 """ @staticmethod def find_optimal( candidates: list[T], filter_func: Callable[[T], bool], score_func: Callable[[T], float] ) -> Optional[T]: """ :param candidates: :param filter_func: :param score_func: :return: """ best_item: Optional[T] = None best_value = -1.0 for item in candidates: if not filter_func(item): continue current_score = score_func(item) if current_score > best_value: best_value = current_score best_item = item return best_item # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:36 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : iterative_optimizer.py from typing import Callable, Tuple class IterativeOptimizer: """ 连续数值区间迭代寻优通用组件 """ @staticmethod def optimize( start: float, end: float, step: float, eval_func: Callable[[float], float] ) -> Tuple[float, float]: """ :return: (最优参数, 最大目标值) """ best_param = start max_target = -float("inf") current = start while current <= end: value = eval_func(current) if value > max_target: max_target = value best_param = current current += step return round(best_param, 2), round(max_target, 2) # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:36 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : base_repo.py from abc import ABC, abstractmethod from typing import List, TypeVar T = TypeVar("T") class BaseRepository(ABC): """ """ @abstractmethod def list_all(self) -> List[T]: pass # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:37 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : diamond_repo.py from Iterative.repository.base_repo import BaseRepository from Iterative.models.diamond import Diamond from typing import List class DiamondRepository(BaseRepository): """ """ def __init__(self): # 模拟数据库,生产环境替换为SQL查询 self._storage: List[Diamond] = [ Diamond("D001", 0.52, "H", "VS1", 24800), Diamond("D002", 0.48, "G", "VS2", 22100), Diamond("D003", 0.55, "I", "SI1", 21300), ] def list_all(self) -> List[Diamond]: return self._storage.copy() # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:38 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : ring_mount_repo.py from Iterative.repository.base_repo import BaseRepository from Iterative.models.ring_mount import RingMount from typing import List class RingMountRepository(BaseRepository): """ """ def __init__(self): self._storage: List[RingMount] = [ RingMount("M001", "18K", 4200), RingMount("M002", "铂金", 5600), ] def list_all(self) -> List[RingMount]: return self._storage.copy() # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:39 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : jade_repo.py from Iterative.repository.base_repo import BaseRepository from Iterative.models.jade_blank import JadeBlank from typing import List class JadeRepository(BaseRepository): """ """ def __init__(self): self._storage: List[JadeBlank] = [ JadeBlank("J001", 32.5, 21.2, 7.3, 6800, 0.86), JadeBlank("J002", 30.1, 19.8, 6.9, 6200, 0.91), JadeBlank("J003", 36.0, 24.1, 8.2, 7500, 0.79), ] def list_all(self) -> List[JadeBlank]: return self._storage.copy() # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:40 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : recommend_service.py from typing import Optional from Iterative.models.diamond import Diamond from Iterative.models.ring_mount import RingMount from Iterative.repository.diamond_repo import DiamondRepository from Iterative.repository.ring_mount_repo import RingMountRepository from Iterative.schemas.request import DiamondRecommendRequest from Iterative.schemas.response import DiamondComboResult class DiamondRecommendService: """ 钻戒组合推荐服务 """ def __init__( self, diamond_repo: DiamondRepository, mount_repo: RingMountRepository ): self.diamond_repo = diamond_repo self.mount_repo = mount_repo def find_best_combo(self, req: DiamondRecommendRequest) -> Optional[DiamondComboResult]: """ :param req: :return: """ diamonds = self.diamond_repo.list_all() mounts = self.mount_repo.list_all() best_result: Optional[DiamondComboResult] = None best_score = -1.0 # 业务迭代逻辑 for dia in diamonds: for mount in mounts: if mount.material != req.material: continue total = dia.cost_price + mount.cost_price if total > req.budget: continue score = dia.calc_comprehensive_score() if score > best_score: best_score = score best_result = DiamondComboResult( diamond=dia, mount=mount, total_cost=total, score=score ) return best_result # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:41 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : jade_custom_service.py from typing import Optional from Iterative.repository.jade_repo import JadeRepository from Iterative.schemas.request import JadeMatchRequest from Iterative.schemas.response import JadeMatchResult from Iterative.models.jade_blank import JadeBlank from Iterative.algorithms.iterative_matcher import IterativeMatcher class JadeCustomService: """ 翡翠毛料匹配服务 """ def __init__(self, jade_repo: JadeRepository): self.jade_repo = jade_repo def match_optimal_blank(self, req: JadeMatchRequest) -> Optional[JadeMatchResult]: """ :param req: :return: """ blanks = self.jade_repo.list_all() def filter_rule(item: JadeBlank) -> bool: cond_size = (req.min_l <= item.length <= req.max_l and req.min_w <= item.width <= req.max_w and req.min_t <= item.thick <= req.max_t) cond_price = req.price_min <= item.cost_price <= req.price_max return cond_size and cond_price def score_rule(item: JadeBlank) -> float: return item.usable_rate best = IterativeMatcher.find_optimal(blanks, filter_rule, score_rule) if not best: return None return JadeMatchResult(blank=best) # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Iterative Algorithms # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/24 22:42 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : pricing_service.py from Iterative.schemas.request import PriceOptRequest from Iterative.schemas.response import PriceOptResult from Iterative.algorithms.iterative_optimizer import IterativeOptimizer class ProductPricingService: """ 动态定价演算服务 """ def calc_optimal_price(self, req: PriceOptRequest) -> PriceOptResult: """ :param req: :return: """ base_cost = req.base_cost def profit_evaluator(coeff: float) -> float: sell_price = base_cost * coeff volume = max(0, round(120 - sell_price / 45)) profit = (sell_price - base_cost) * volume return profit best_coeff, max_profit = IterativeOptimizer.optimize( start=req.min_coeff, end=req.max_coeff, step=req.step, eval_func=profit_evaluator ) best_price = round(base_cost * best_coeff, 2) return PriceOptResult( best_coeff=best_coeff, best_sell_price=best_price, max_gross_profit=max_profit )

输出: