Change Dir

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

          統(tǒng)計(jì)

          留言簿(18)

          積分與排名

          “牛”們的博客

          各個(gè)公司技術(shù)

          我的鏈接

          淘寶技術(shù)

          閱讀排行榜

          評(píng)論排行榜

          深入理解動(dòng)態(tài)規(guī)劃的一系列問題(14)

          不知不覺說了這么多問題了,但是很多最優(yōu)化經(jīng)典問題沒有提到,今天就掀起整個(gè)動(dòng)態(tài)規(guī)劃系列的高潮,來個(gè)經(jīng)典:線性規(guī)劃問題(Linear Programming)。維基百科里清晰的描寫了什么是線性規(guī)劃。一個(gè)簡單的描述就是對(duì)于一個(gè)抽象后的問題,假設(shè)目標(biāo)就是c1x1+c2x2,這里x1和x2都是變量,c1和c2是常量系數(shù),給定一系列約束a11x1+a12x2≤b1,a21x1+a22x2≤b1,a31x1+a32x2≤b3;求最優(yōu)的x1和x2使得c1x1+c2x2最大化。我們慣性的用線性代數(shù)來表示這個(gè)問題就是我們要求最大化的cTx,條件是Ax≤b,這里加個(gè)限制就是x是正整數(shù)(因?yàn)橥鶎?shí)際問題中,正數(shù)是有意義的)。對(duì)于這個(gè)問題,其實(shí)關(guān)鍵第一步定義狀態(tài)就很難,這個(gè)問題不像以前很多問題一看就是一個(gè)圖論最短路或者組合優(yōu)化,我們的思路需要擴(kuò)展。我們定義一個(gè)狀態(tài)(j,(y1,y2,…ym)),j表示index號(hào)也就是變量x的index,而后面的m元組y就表示當(dāng)前決策下各個(gè)約束的值。而在決策階段,在階段j做的決策就是從定義域里挑選一個(gè)D賦給x(j+1),所以DPFE就變?yōu)椋篺(j,(y1,…,ym))=0 當(dāng)j=n時(shí),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))} 當(dāng)j<n。定義域D表示為D=D(j,(y1,...,ym)) ={0,…,min{y1/a(1,j),…,ym/a(m,j)}},方程的目標(biāo)就是要求解f(0,(y1,y2,…,ym))。寫到這里,花個(gè)2-3分鐘仔細(xì)理解一下這個(gè)方程,就能看出,其實(shí)又是老套路,線性規(guī)劃這個(gè)看上去復(fù)雜的公式也被動(dòng)態(tài)規(guī)劃的順序擴(kuò)展分析方法表示了,遞歸的選擇xi來尋求最值,復(fù)雜的約束作為遞歸的狀態(tài)不斷傳遞……

          具體拿例子說是,假設(shè)c=(3,5),b=(4,12,18),并且A是約束矩陣A=[(1,0),(0,2),(3,2)]這里矩陣是行表示法即3行兩列矩陣。那么經(jīng)過上面的迭代求解,可以知道最優(yōu)的決策組(x1,x2)=(2,6),最大化后的函數(shù)值為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) 評(píng)論(0)  編輯  收藏 所屬分類: 算法

          主站蜘蛛池模板: 通辽市| 普定县| 抚顺县| 晋中市| 买车| 漯河市| 城口县| 娄烦县| 华池县| 封丘县| 景宁| 民县| 兴义市| 嘉义市| 湘乡市| 香格里拉县| 平舆县| 芮城县| 木兰县| 郸城县| 扶沟县| 遂平县| 梅州市| 贡嘎县| 屏南县| 湖州市| 抚顺市| 平安县| 宁陕县| 上思县| 驻马店市| 平陆县| 隆子县| 鹰潭市| 时尚| 夏津县| 吐鲁番市| 永春县| 青铜峡市| 瑞安市| 衡南县|