天天看點

用Microsoft.Solver.Foundation進行線性規劃,為WPF應用添加智能

原文: 用Microsoft.Solver.Foundation進行線性規劃,為WPF應用添加智能

  在管理資訊系統的開發過程中,往往會涉及到一些線性規劃數學模型,例如資源配置優化。微軟的Microsoft.Solver.Foundation是一個數學庫,可以很好的對線性規劃問題進行求解。關于它的細節,可以自行百度,話不多說,以例題來學習如何用Microsoft.Solver.Foundation進行線性規劃:

  題目(來自網絡),如下圖:

  

用Microsoft.Solver.Foundation進行線性規劃,為WPF應用添加智能

  為了解決上述線性規劃問題,先要下載下傳并安裝Microsoft.Solver.Foundation庫,關于安裝細節這裡不贅述。

1、VS2012建立一個WPF應用程式WpfLPDemo(WinForm也是可以的),建立一個libs檔案夾和images檔案夾,并将Microsoft.Solver.Foundation.dll拷貝到libs(注意添加dll引用),如下圖:

用Microsoft.Solver.Foundation進行線性規劃,為WPF應用添加智能

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、儲存并運作程式,成功的話應該如下圖:

用Microsoft.Solver.Foundation進行線性規劃,為WPF應用添加智能

5、單擊計算按鈕,即可輸出結構,如下圖:

用Microsoft.Solver.Foundation進行線性規劃,為WPF應用添加智能

可見計算的答案和例題給出的答案是一緻的。為了建構根據通用的程式,可以從UI上動态傳入模型以及模型的值進行優化求解,進而更好的具有實用性。