深入理解動態規劃的一系列問題(14)
不知不覺說了這么多問題了,但是很多最優化經典問題沒有提到,今天就掀起整個動態規劃系列的高潮,來個經典:線性規劃問題(Linear Programming)。維基百科里清晰的描寫了什么是線性規劃。一個簡單的描述就是對于一個抽象后的問題,假設目標就是c1x1+c2x2,這里x1和x2都是變量,c1和c2是常量系數,給定一系列約束a11x1+a12x2≤b1,a21x1+a22x2≤b1,a31x1+a32x2≤b3;求最優的x1和x2使得c1x1+c2x2最大化。我們慣性的用線性代數來表示這個問題就是我們要求最大化的cTx,條件是Ax≤b,這里加個限制就是x是正整數(因為往往實際問題中,正數是有意義的)。對于這個問題,其實關鍵第一步定義狀態就很難,這個問題不像以前很多問題一看就是一個圖論最短路或者組合優化,我們的思路需要擴展。我們定義一個狀態(j,(y1,y2,…ym)),j表示index號也就是變量x的index,而后面的m元組y就表示當前決策下各個約束的值。而在決策階段,在階段j做的決策就是從定義域里挑選一個D賦給x(j+1),所以DPFE就變為:f(j,(y1,…,ym))=0 當j=n時,f(j,(y1,…,ym))=max x(j+1)∈D{c(j+1)x(j+1)+f(j+1,(y1-a(1,j+1)x(j+1),…,ym-a(m,j+1)x(j+1))} 當j<n。定義域D表示為D=D(j,(y1,...,ym)) ={0,…,min{y1/a(1,j),…,ym/a(m,j)}},方程的目標就是要求解f(0,(y1,y2,…,ym))。寫到這里,花個2-3分鐘仔細理解一下這個方程,就能看出,其實又是老套路,線性規劃這個看上去復雜的公式也被動態規劃的順序擴展分析方法表示了,遞歸的選擇xi來尋求最值,復雜的約束作為遞歸的狀態不斷傳遞……
具體拿例子說是,假設c=(3,5),b=(4,12,18),并且A是約束矩陣A=[(1,0),(0,2),(3,2)]這里矩陣是行表示法即3行兩列矩陣。那么經過上面的迭代求解,可以知道最優的決策組(x1,x2)=(2,6),最大化后的函數值為c1x1+c2x2=3*2+5*6=36。
最后我們附上解題源碼:
1: /*
2: * Copyright (C) 2013 changedi
3: *
4: * Licensed under the Apache License, Version 2.0 (the "License");
5: * you may not use this file except in compliance with the License.
6: * You may obtain a copy of the License at
7: *
8: * http://www.apache.org/licenses/LICENSE-2.0
9: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package com.jybat.dp;
17:
18: import java.util.HashSet;
19: import java.util.Set;
20:
21: //IntegerLinearProgramming;
22: public class ILP {
23: // objective function coefficients
24: private static int[] c = { 3, 5 };
25: // right hand side of constraints vector
26: private static int[] b = { 4, 12, 18 };
27: // constraint matrix
28: private static int[][] a = { { 1, 0 }, { 0, 2 }, { 3, 2 } };
29: private static int n = c.length;
30: private static int m = b.length;
31: private static final int infty = Integer.MAX_VALUE;
32:
33: private static Set<Integer> calculateDecisionSet(int stage, int y1, int y2,
34: int y3) {
35: Set<Integer> result = new HashSet<Integer>();
36: // maxPossibleChoiceBecauseOfResourceiRestriction, i=1,2,3
37: int mpc1 = infty;
38: int mpc2 = infty;
39: int mpc3 = infty;
40: if (a[0][stage] != 0) {
41: mpc1 = y1 / a[0][stage];
42: }
43: if (a[1][stage] != 0) {
44: mpc2 = y2 / a[1][stage];
45: }
46: if (a[2][stage] != 0) {
47: mpc3 = y3 / a[2][stage];
48: }
49: for (int i = 0; i <= Math.min(mpc1, Math.min(mpc2, mpc3)); i++) {
50: result.add(new Integer(i));
51: }
52: return result;
53: }
54:
55: // here: yi denotes how much of resource i is still available,
56: // (in other words how much slack is still available)
57: public static double f(int stage, int y1, int y2, int y3) {
58: if (stage == n) {
59: return 0.0;
60: }
61: double max = Double.MIN_VALUE;
62: for (int d : calculateDecisionSet(stage, y1, y2, y3)) {
63: double t = c[stage]
64: * d
65: + f(stage + 1, y1 - a[0][stage] * d, y2 - a[1][stage] * d,
66: y3 - a[2][stage] * d);
67: if (t > max)
68: max = t;
69: }
70: return max;
71: }
72:
73: /**
74: * @param args
75: */
76: public static void main(String[] args) {
77: System.out.println(ILP.f(0, b[0], b[1], b[2]));
78: }
79:
80: }
posted on 2014-06-09 11:22 changedi 閱讀(2488) 評論(0) 編輯 收藏 所屬分類: 算法