使用Microsoft Solver Foundation求解非线性系统遇模型非凸问题求助
Hey there! Let's unpack what's happening with your Microsoft Solver Foundation code.
First, the error makes total sense: the default solver in MS Solver Foundation is built for convex optimization problems, and your objective function (a0*a1) is non-convex. The product of two variables creates a saddle-shaped function—its underlying mathematical structure (the Hessian matrix) has both positive and negative eigenvalues, meaning it’s neither convex nor concave. Convex-focused solvers can’t handle this type of non-convex nonlinearity, hence the error message you’re seeing.
Solutions to Your Problem
1. Manual Optimal Solution (Simplest for This Case)
Since your variables are non-negative and your constraints are just upper bounds, the maximum value of a0*a1 is trivial to calculate: set each variable to its maximum allowed limit. That means a0=10, a1=20, giving a product of 200. No solver required here!
2. Use MS Solver Foundation's Nonlinear Programming Solver
If you want to use the solver anyway (say, for more complex variations of this problem), you can explicitly call the library’s nonlinear programming solver, which supports non-convex problems. Here’s how to modify your code:
using System; using Microsoft.SolverFoundation.Services; using Microsoft.SolverFoundation.Solvers; // Add this namespace for the nonlinear solver namespace SolverFoundationDemo { class Program { static void Main(string[] args) { Console.WriteLine("\nBegin Solver demo\n"); var solver = SolverContext.GetContext(); var model = solver.CreateModel(); var decision1 = new Decision(Domain.RealNonnegative, "a0"); model.AddDecision(decision1); var decision2 = new Decision(Domain.RealNonnegative, "a1"); model.AddDecision(decision2); model.AddConstraint("Constraint0", "a0 <=10"); model.AddConstraint("Constraint1", "a1 <=20"); model.AddGoal("Goal", GoalKind.Maximize, "a0*a1"); // Explicitly use the nonlinear programming solver var solution = solver.Solve(new NonlinearProgrammingSolver()); // Print results to verify the solution if (solution.Quality == SolutionQuality.Optimal) { Console.WriteLine($"Optimal a0: {decision1.GetDoubleValue()}"); Console.WriteLine($"Optimal a1: {decision2.GetDoubleValue()}"); Console.WriteLine($"Maximum product: {decision1.GetDoubleValue() * decision2.GetDoubleValue()}"); } else { Console.WriteLine($"Solution quality: {solution.Quality}"); } Console.WriteLine("\nEnd Solver demo\n"); Console.ReadLine(); } } }
This should resolve the convexity error and find the optimal solution. Note that for non-convex problems, nonlinear solvers can sometimes get stuck in local optima—but in your case, there’s only one global optimum at the boundary of your constraints, so it will work perfectly.
内容的提问来源于stack exchange,提问作者jbf

