You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

高性价比化学混合物配比计算算法开发技术问询

Solution for Cost-Effective Chemical Mixture Calculation

Approach

This is a classic linear programming (LP) problem where we need to minimize the cost of a mixture while satisfying component constraints. Generating all possible combinations isn't feasible here because we’re dealing with continuous variables (weights can be any non-negative value summing to 1). Instead, we use a linear programming solver to find the optimal weights of each chemical that meet the requirements at the lowest cost.

We’ll use math.js for its built-in LP solver, which simplifies setting up and solving the problem.

Step-by-Step Implementation

1. Add Dependencies

First, add math.js to your project (in CodeSandbox, search for it in the dependencies panel and install it).

2. Full Code Implementation

import * as math from 'mathjs';

// Define chemicals with component ratios (as decimals) and unit prices
const chemicals = [
  { name: 'AZS', components: { ZrO2: 0.30, Al2O3: 0.50, SiO2: 0.13, Na2O: 0.018 }, price: 328.95 },
  { name: 'Fine salt', components: { ZrO2: 0.25, Al2O3: 0.30, SiO2: 0.43, Na2O: 0.002 }, price: 203.95 },
  { name: 'ZDCF', components: { ZrO2: 0.63, Al2O3: 0.01, SiO2: 0.33, Na2O: 0.004 }, price: 789.47 },
  { name: 'AB fines', components: { ZrO2: 0, Al2O3: 0.95, SiO2: 0.01, Na2O: 0.04 }, price: 263.16 },
  { name: 'HJO', components: { ZrO2: 0.90, Al2O3: 0.01, SiO2: 0.01, Na2O: 0.002 }, price: 1315.79 },
];

// Define component requirements (min/max as decimals)
const requirements = {
  ZrO2: { min: 0.32, max: 0.34 },
  Al2O3: { min: 0.49, max: 0.52 },
  SiO2: { min: 0.15, max: 0.17 },
  Na2O: { min: 0.013, max: 0.02 },
};

// Set up linear programming problem
const variableCount = chemicals.length;

// Objective: Minimize total cost (coefficients are chemical prices)
const objective = chemicals.map(c => c.price);

// Constraints for the LP solver
const constraints: math.LinearProgramConstraint[] = [];

// 1. Sum of all weights equals 1 (we're calculating per unit of mixture)
constraints.push({
  coefficients: Array(variableCount).fill(1),
  equal: 1,
});

// 2. Each component's weighted average meets min/max requirements
const componentNames = Object.keys(requirements) as Array<keyof typeof requirements>;
componentNames.forEach(component => {
  const componentCoeffs = chemicals.map(c => c.components[component]);
  const req = requirements[component];
  
  // Minimum ratio constraint
  constraints.push({
    coefficients: componentCoeffs,
    min: req.min,
  });
  
  // Maximum ratio constraint
  constraints.push({
    coefficients: componentCoeffs,
    max: req.max,
  });
});

// 3. All weights are non-negative (can't use negative amounts of any chemical)
for (let i = 0; i < variableCount; i++) {
  const coeffs = Array(variableCount).fill(0);
  coeffs[i] = 1;
  constraints.push({
    coefficients: coeffs,
    min: 0,
  });
}

// Solve the LP problem
const result = math.minimize(objective, constraints);

// Display results
if (result.feasible) {
  console.log("Optimal Mixture Found:");
  console.log("------------------------");
  result.solution.forEach((weight, index) => {
    if (weight > 1e-6) { // Ignore negligible weights (floating point precision)
      console.log(`${chemicals[index].name}: ${(weight * 100).toFixed(2)}%`);
    }
  });
  console.log("------------------------");
  console.log(`Total Cost per Unit: $${result.objectiveValue.toFixed(2)}`);
  
  // Verify component ratios of the final mixture
  console.log("\nVerified Component Ratios:");
  componentNames.forEach(component => {
    const ratio = result.solution.reduce((sum, weight, index) => {
      return sum + weight * chemicals[index].components[component];
    }, 0);
    console.log(`${component}: ${(ratio * 100).toFixed(2)}%`);
  });
} else {
  console.log("No feasible solution exists that meets all requirements.");
}

Explanation

  1. Data Definition: We convert component percentages to decimals for easier calculations and define both the chemical properties and mixture requirements clearly.
  2. LP Setup:
    • Objective Function: We aim to minimize total cost, so the coefficients are the unit prices of each chemical.
    • Constraints:
      • Sum of weights equals 1 (we’re calculating for 1 unit of mixture).
      • Each component’s weighted average falls within its specified min/max range.
      • All weights are non-negative (we can’t use negative amounts of any chemical).
  3. Solving: The math.minimize function solves the LP problem and returns the optimal weights and total cost.
  4. Result Display: We print the optimal mixture ratios, total cost, and verify that the resulting component ratios meet all requirements.

内容的提问来源于stack exchange,提问作者Jacobdo

火山引擎 最新活动