在管理信息系统的开发过程中,往往会涉及到一些线性规划数学模型,例如资源配置优化。微软的Microsoft.Solver.Foundation是一个数学库,可以很好的对线性规划问题进行求解。关于它的细节,可以自行百度,话不多说,以例题来学习如何用Microsoft.Solver.Foundation进行线性规划:
题目(来自网络),如下图:
为了解决上述线性规划问题,先要下载并安装Microsoft.Solver.Foundation库,关于安装细节这里不赘述。
1、VS2012建立一个WPF应用程序WpfLPDemo(WinForm也是可以的),新建一个libs文件夹和images文件夹,并将Microsoft.Solver.Foundation.dll拷贝到libs(注意添加dll引用),如下图:
images下放的图片为题目截图。
2、编辑MainWindow.xaml文件,在设计界面上放一个Image展示例题截图、TextBlock用于显示优化结果、Button用于触发计算事件,代码如下:
1 <Window x:Class="WpfLPDemo.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="424.03" Width="711.269"> 5 <Grid> 6 <Image HorizontalAlignment="Left" Height="290" Margin="4,10,0,0" VerticalAlignment="Top" Width="695" Source="images/1.png" Stretch="Fill" /> 7 <TextBlock Name="answer" HorizontalAlignment="Left" Margin="33,352,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="线性规划答案为:" FontFamily="Verdana" FontSize="14" /> 8 <Button Content="计算" HorizontalAlignment="Left" Margin="604,305,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_calc" FontSize="15" /> 9 10 </Grid> 11 </Window>
3、编辑MainWindow.xaml.cs文件,注意添加using Microsoft.SolverFoundation.Services; using Microsoft.SolverFoundation.Solvers;代码如下(核心代码已经做了注释,可了解一下用法):
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 using Microsoft.SolverFoundation; 16 namespace WpfLPDemo 17 { 18 using Microsoft.SolverFoundation.Services; 19 using Microsoft.SolverFoundation.Solvers; 20 /// <summary> 21 /// MainWindow.xaml 的交互逻辑 22 /// </summary> 23 public partial class MainWindow : Window 24 { 25 public MainWindow() 26 { 27 InitializeComponent(); 28 } 29 public object OPT() 30 { 31 SolverContext context = SolverContext.GetContext(); 32 //创建模型 33 Model model = context.CreateModel(); 34 //优化决策因子,变量为实数,Domain.Integer|Domain.IntegerNonnegative为整数优化 35 Decision x = new Decision(Domain.Real, "x"); 36 Decision y = new Decision(Domain.Real, "y"); 37 //添加 38 model.AddDecisions(x, y); 39 //x,y变量范围 40 // model.AddConstraints("变量范围", 41 // double.NegativeInfinity < x <= double.PositiveInfinity, 42 //double.NegativeInfinity < y <= double.PositiveInfinity); 43 44 model.AddConstraints("约束", 45 double.NegativeInfinity < x <= double.PositiveInfinity, 46 double.NegativeInfinity < y <= double.PositiveInfinity, 47 2*x + y - 2 >=0, 48 x - 2*y + 4 >=0, 49 3*x - y -3 <= 0); 50 //目标函数 min z=x * x + y * y , GoalKind.Minimize最小值 51 Goal gmin= model.AddGoal("zmin", GoalKind.Minimize, x * x + y * y); 52 53 //优化 54 Solution solution = context.Solve(); 55 //优化报告 56 // Report report = solution.GetReport(); 57 58 string s = string.Format("min [ x={0:N2},y={1:N2}", x.ToDouble().ToString("0.00"), y.ToDouble().ToString("0.00")); 59 s += string.Format(",min={0} ] " ,solution.Goals.First<Goal>().ToDouble().ToString("0.00")); 60 61 //================================================================= 62 model.RemoveGoal(gmin); 63 Goal gmax = model.AddGoal("zmax", GoalKind.Maximize, x * x + y * y); 64 //优化 65 solution = context.Solve(); 66 s += string.Format("| max[ x={0:N2},y={1:N2}", x.ToDouble().ToString("0.00"), y.ToDouble().ToString("0.00")); 67 s += string.Format(",max={0} ] " ,solution.Goals.First<Goal>().ToDouble().ToString("0.00")); 68 //-------------------------------------------------------------------------------------------------------------------------------- 69 70 context.ClearModel(); 71 return s; 72 73 } 74 private void Button_Click_calc(object sender, RoutedEventArgs e) 75 { 76 this.answer.Text = OPT().ToString(); 77 } 78 79 80 } 81 }
4、保存并运行程序,成功的话应该如下图:
5、单击计算按钮,即可输出结构,如下图:
可见计算的答案和例题给出的答案是一致的。为了构建根据通用的程序,可以从UI上动态传入模型以及模型的值进行优化求解,从而更好的具有实用性。