优化算法

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
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 errors.Is(err, lp.ErrInfeasible) {
@@ -291,8 +291,8 @@ func (r *recipeGenerateManagerImpl) GenerateRecipe(ctx context.Context, pigType
}
recipe := &models.Recipe{
Name: fmt.Sprintf("%s-%s - 自动计算配方", pigType.Breed.Name, pigType.AgeStage.Name),
Description: fmt.Sprintf("基于 %d 种原料计算的最优成本配方。计算时预估成本: %.2f元/kg", actualMaterialCount, optVal),
Name: fmt.Sprintf("%s-%s - 自动计算配方", pigType.Breed.Name, pigType.AgeStage.Name), // 提供一个默认的名称
Description: fmt.Sprintf("基于 %d 种原料计算的最优成本配方。", actualMaterialCount), // 提供一个默认的描述
RecipeIngredients: []models.RecipeIngredient{},
}
@@ -327,12 +327,6 @@ func (r *recipeGenerateManagerImpl) GenerateRecipe(ctx context.Context, pigType
if totalPercentage > 1.0+1e-3 {
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
}