在管理信息系统的开发过程中,往往会涉及到一些线性规划数学模型,例如资源配置优化。微软的Microsoft.Solver.Foundation是一个数学库,可以很好的对线性规划问题进行求解。关于它的细节,可以自行百度,话不多说,以例题来学习如何用Microsoft.Solver.Foundation进行线性规划:

  题目(来自网络),如下图:

  用Microsoft.Solver.Foundation进行线性规划,为WPF应用添加智能-LMLPHP

  为了解决上述线性规划问题,先要下载并安装Microsoft.Solver.Foundation库,关于安装细节这里不赘述。

1、VS2012建立一个WPF应用程序WpfLPDemo(WinForm也是可以的),新建一个libs文件夹和images文件夹,并将Microsoft.Solver.Foundation.dll拷贝到libs(注意添加dll引用),如下图:

用Microsoft.Solver.Foundation进行线性规划,为WPF应用添加智能-LMLPHP

images下放的图片为题目截图。

2、编辑MainWindow.xaml文件,在设计界面上放一个Image展示例题截图、TextBlock用于显示优化结果、Button用于触发计算事件,代码如下:

 <Window x:Class="WpfLPDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="424.03" Width="711.269">
<Grid>
<Image HorizontalAlignment="Left" Height="290" Margin="4,10,0,0" VerticalAlignment="Top" Width="695" Source="images/1.png" Stretch="Fill" />
<TextBlock Name="answer" HorizontalAlignment="Left" Margin="33,352,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="线性规划答案为:" FontFamily="Verdana" FontSize="14" />
<Button Content="计算" HorizontalAlignment="Left" Margin="604,305,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_calc" FontSize="15" /> </Grid>
</Window>

3、编辑MainWindow.xaml.cs文件,注意添加using Microsoft.SolverFoundation.Services; using Microsoft.SolverFoundation.Solvers;代码如下(核心代码已经做了注释,可了解一下用法):

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.SolverFoundation;
namespace WpfLPDemo
{
using Microsoft.SolverFoundation.Services;
using Microsoft.SolverFoundation.Solvers;
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public object OPT()
{
SolverContext context = SolverContext.GetContext();
//创建模型
Model model = context.CreateModel();
//优化决策因子,变量为实数,Domain.Integer|Domain.IntegerNonnegative为整数优化
Decision x = new Decision(Domain.Real, "x");
Decision y = new Decision(Domain.Real, "y");
//添加
model.AddDecisions(x, y);
//x,y变量范围
// model.AddConstraints("变量范围",
// double.NegativeInfinity < x <= double.PositiveInfinity,
//double.NegativeInfinity < y <= double.PositiveInfinity); model.AddConstraints("约束",
double.NegativeInfinity < x <= double.PositiveInfinity,
double.NegativeInfinity < y <= double.PositiveInfinity,
*x + y - >=,
x - *y + >=,
*x - y - <= );
//目标函数 min z=x * x + y * y , GoalKind.Minimize最小值
Goal gmin= model.AddGoal("zmin", GoalKind.Minimize, x * x + y * y); //优化
Solution solution = context.Solve();
//优化报告
// Report report = solution.GetReport(); string s = string.Format("min [ x={0:N2},y={1:N2}", x.ToDouble().ToString("0.00"), y.ToDouble().ToString("0.00"));
s += string.Format(",min={0} ] " ,solution.Goals.First<Goal>().ToDouble().ToString("0.00")); //=================================================================
model.RemoveGoal(gmin);
Goal gmax = model.AddGoal("zmax", GoalKind.Maximize, x * x + y * y);
//优化
solution = context.Solve();
s += string.Format("| max[ x={0:N2},y={1:N2}", x.ToDouble().ToString("0.00"), y.ToDouble().ToString("0.00"));
s += string.Format(",max={0} ] " ,solution.Goals.First<Goal>().ToDouble().ToString("0.00"));
//-------------------------------------------------------------------------------------------------------------------------------- context.ClearModel();
return s; }
private void Button_Click_calc(object sender, RoutedEventArgs e)
{
this.answer.Text = OPT().ToString();
} }
}

4、保存并运行程序,成功的话应该如下图:
用Microsoft.Solver.Foundation进行线性规划,为WPF应用添加智能-LMLPHP

5、单击计算按钮,即可输出结构,如下图:

用Microsoft.Solver.Foundation进行线性规划,为WPF应用添加智能-LMLPHP

可见计算的答案和例题给出的答案是一致的。为了构建根据通用的程序,可以从UI上动态传入模型以及模型的值进行优化求解,从而更好的具有实用性。

05-08 14:53