Change Dir

          先知cd——熱愛生活是一切藝術的開始

          導航

          <2014年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          公告

          寫下來的都是資源,分享給互聯網~~均屬原創隨筆。
          轉載引用請注明作者changedi。
          喜歡應用研究,熱愛編程,歡迎交流。

          隨筆分類(125)

          隨筆檔案(123)

          統計

          留言簿(18)

          積分與排名

          “牛”們的博客

          各個公司技術

          我的鏈接

          淘寶技術

          閱讀排行榜

          評論排行榜

          深入理解動態規劃的一系列問題(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 閱讀(2478) 評論(0)  編輯  收藏 所屬分類: 算法

          主站蜘蛛池模板: 罗江县| 响水县| 日喀则市| 荆州市| 永清县| 茶陵县| 巩义市| 旌德县| 伊吾县| 阳江市| 肥西县| 宁强县| 宜城市| 敖汉旗| 巴塘县| 定安县| 临武县| 塘沽区| 河源市| 遵义县| 固原市| 黔东| 梁山县| 涪陵区| 裕民县| 乌苏市| 喀喇| 文化| 瓦房店市| 分宜县| 邳州市| 肇源县| 东港市| 庄浪县| 息烽县| 南通市| 宁河县| 英德市| 苏尼特右旗| 武穴市| 哈尔滨市|