优化算法

This commit is contained in:
2025-11-27 20:03:14 +08:00
parent 33cdf7278e
commit da8e1d0191
3 changed files with 67 additions and 17 deletions

View File

@@ -266,7 +266,7 @@ func (r *recipeGenerateManagerImpl) GenerateRecipe(ctx context.Context, pigType
// --------------------------------------------------------- // ---------------------------------------------------------
// lp.Simplex 求解: minimize c^T * x subject to A * x = b, x >= 0 // lp.Simplex 求解: minimize c^T * x subject to A * x = b, x >= 0
optVal, x, err := lp.Simplex(c, A, b, 1e-8, nil) _, x, err := lp.Simplex(c, A, b, 1e-8, nil)
if err != nil { if err != nil {
if errors.Is(err, lp.ErrInfeasible) { if errors.Is(err, lp.ErrInfeasible) {
@@ -291,8 +291,8 @@ func (r *recipeGenerateManagerImpl) GenerateRecipe(ctx context.Context, pigType
} }
recipe := &models.Recipe{ recipe := &models.Recipe{
Name: fmt.Sprintf("%s-%s - 自动计算配方", pigType.Breed.Name, pigType.AgeStage.Name), Name: fmt.Sprintf("%s-%s - 自动计算配方", pigType.Breed.Name, pigType.AgeStage.Name), // 提供一个默认的名称
Description: fmt.Sprintf("基于 %d 种原料计算的最优成本配方。计算时预估成本: %.2f元/kg", actualMaterialCount, optVal), Description: fmt.Sprintf("基于 %d 种原料计算的最优成本配方。", actualMaterialCount), // 提供一个默认的描述
RecipeIngredients: []models.RecipeIngredient{}, RecipeIngredients: []models.RecipeIngredient{},
} }
@@ -327,12 +327,6 @@ func (r *recipeGenerateManagerImpl) GenerateRecipe(ctx context.Context, pigType
if totalPercentage > 1.0+1e-3 { if totalPercentage > 1.0+1e-3 {
return nil, fmt.Errorf("计算结果异常:实际原料总量超过 100%% (计算值: %.2f),请检查算法或数据配置", totalPercentage) return nil, fmt.Errorf("计算结果异常:实际原料总量超过 100%% (计算值: %.2f),请检查算法或数据配置", totalPercentage)
} }
// 如果 totalPercentage 小于 1.0,说明填充料被使用,这是符合预期的。
// 此时需要在描述中说明需要添加的廉价填充料的百分比。
if totalPercentage < 1.0-1e-4 { // 允许微小的浮点误差
fillerPercentage := (1.0 - totalPercentage) * 100.0
recipe.Description = fmt.Sprintf("%s。注意配方中实际原料占比 %.2f%%,需额外补充 %.2f%% 廉价填充料", recipe.Description, totalPercentage*100.0, fillerPercentage)
}
return recipe, nil return recipe, nil
} }

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" "git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
) )
@@ -60,8 +61,10 @@ func NewRecipeService(
// pigTypeID: 目标猪类型的ID。 // pigTypeID: 目标猪类型的ID。
// 返回: 生成的配方对象指针和可能的错误。 // 返回: 生成的配方对象指针和可能的错误。
func (r *recipeServiceImpl) GenerateRecipeWithAllRawMaterials(ctx context.Context, pigTypeID uint32) (*models.Recipe, error) { func (r *recipeServiceImpl) GenerateRecipeWithAllRawMaterials(ctx context.Context, pigTypeID uint32) (*models.Recipe, error) {
serviceCtx, logger := logs.Trace(ctx, r.ctx, "GenerateRecipeWithAllRawMaterials")
// 1. 获取猪只类型信息,确保包含了营养需求 // 1. 获取猪只类型信息,确保包含了营养需求
pigType, err := r.GetPigTypeByID(ctx, pigTypeID) pigType, err := r.GetPigTypeByID(serviceCtx, pigTypeID)
if err != nil { if err != nil {
return nil, fmt.Errorf("获取猪类型信息失败: %w", err) return nil, fmt.Errorf("获取猪类型信息失败: %w", err)
} }
@@ -69,23 +72,54 @@ func (r *recipeServiceImpl) GenerateRecipeWithAllRawMaterials(ctx context.Contex
// 2. 获取所有原料 // 2. 获取所有原料
// 我们通过传递一个非常大的 pageSize 来获取所有原料,这在大多数情况下是可行的。 // 我们通过传递一个非常大的 pageSize 来获取所有原料,这在大多数情况下是可行的。
// 对于超大规模系统,可能需要考虑分页迭代,但目前这是一个简单有效的策略。 // 对于超大规模系统,可能需要考虑分页迭代,但目前这是一个简单有效的策略。
materials, _, err := r.ListRawMaterials(ctx, repository.RawMaterialListOptions{}, 1, 9999) materials, _, err := r.ListRawMaterials(serviceCtx, repository.RawMaterialListOptions{}, 1, 9999)
if err != nil { if err != nil {
return nil, fmt.Errorf("获取所有原料列表失败: %w", err) return nil, fmt.Errorf("获取所有原料列表失败: %w", err)
} }
// 3. 调用生成器生成配方 // 3. 调用生成器生成配方
recipe, err := r.GenerateRecipe(ctx, *pigType, materials) recipe, err := r.GenerateRecipe(serviceCtx, *pigType, materials)
if err != nil { if err != nil {
return nil, fmt.Errorf("生成配方失败: %w", err) return nil, fmt.Errorf("生成配方失败: %w", err)
} }
// 4. 保存新生成的配方到数据库 // 4. 丰富配方描述:计算并添加参考价格信息
// CreateRecipe 会处理配方及其成分的保存
if recipe, err = r.CreateRecipe(ctx, recipe); err != nil { // 填充 RecipeIngredients 中的 RawMaterial 字段,以便后续计算成本
return nil, fmt.Errorf("保存生成的配方失败: %w", err) rawMaterialMap := make(map[uint32]models.RawMaterial)
for _, mat := range materials {
rawMaterialMap[mat.ID] = mat
}
for i := range recipe.RecipeIngredients {
if rawMat, ok := rawMaterialMap[recipe.RecipeIngredients[i].RawMaterialID]; ok {
recipe.RecipeIngredients[i].RawMaterial = rawMat
} else {
// 理论上 GenerateRecipe 应该只使用传入的 materials 中的 RawMaterialID
// 如果出现此情况,说明 GenerateRecipe 生成了不在当前 materials 列表中的 RawMaterialID
// 这可能是一个数据不一致或逻辑错误,记录警告以便排查
logger.Warnf("未找到 RecipeIngredient (RawMaterialID: %d) 对应的 RawMaterial成本计算可能不准确", recipe.RecipeIngredients[i].RawMaterialID)
}
} }
// 5. 返回创建的配方 (现在它应该已经有了ID) referencePrice := recipe.CalculateReferencePricePerKilogram() / 100
recipe.Description = fmt.Sprintf("%s 计算时预估成本: %.2f元/kg。", recipe.Description, referencePrice)
// 如果 totalPercentage 小于 100%,说明填充料被使用,这是符合预期的。
// 此时需要在描述中说明需要添加的廉价填充料的百分比。
totalPercentage := recipe.CalculateTotalRawMaterialProportion()
if totalPercentage < 99.99 { // 允许微小的浮点误差
fillerPercentage := 100 - totalPercentage
recipe.Description = fmt.Sprintf("%s 注意:配方中实际原料占比 %.2f%%,需额外补充 %.2f%% 廉价填充料", recipe.Description, totalPercentage, fillerPercentage)
}
// 5. 保存新生成的配方到数据库
// CreateRecipe 会处理配方及其成分的保存
if recipe, err = r.CreateRecipe(serviceCtx, recipe); err != nil {
return nil, fmt.Errorf("保存生成的配方失败: %w", err)
}
logger.Infof("成功生成配方: %+v", recipe)
// 6. 返回创建的配方 (现在它应该已经有了ID)
return recipe, nil return recipe, nil
} }

View File

@@ -13,6 +13,28 @@ func (Recipe) TableName() string {
return "recipes" return "recipes"
} }
// CalculateTotalRawMaterialProportion 计算配方中所有原料的总比例
func (r Recipe) CalculateTotalRawMaterialProportion() float32 {
var totalPercentage float32
for _, ingredient := range r.RecipeIngredients {
totalPercentage += ingredient.Percentage
}
return totalPercentage
}
// CalculateReferencePricePerKilogram 根据原料参考价计算配方每公斤的成本
func (r Recipe) CalculateReferencePricePerKilogram() float32 {
var totalCost float32
for _, ingredient := range r.RecipeIngredients {
// 确保 RawMaterial 已经被加载
if ingredient.RawMaterial.ID == 0 {
return 0.0
}
totalCost += ingredient.RawMaterial.ReferencePrice * ingredient.Percentage
}
return totalCost
}
// RecipeIngredient 配方原料组成模型 // RecipeIngredient 配方原料组成模型
type RecipeIngredient struct { type RecipeIngredient struct {
Model Model