{"id":1024,"date":"2016-12-12T22:45:09","date_gmt":"2016-12-12T14:45:09","guid":{"rendered":"http:\/\/yaochenkun.site\/?p=1024"},"modified":"2024-11-23T15:53:52","modified_gmt":"2024-11-23T07:53:52","slug":"ant2_article","status":"publish","type":"post","link":"http:\/\/yaochenkun.site\/index.php\/2016\/12\/12\/ant2_article\/","title":{"rendered":"\u8681\u7fa4\u7b97\u6cd5\u89e3 TSP2-\u6838\u5fc3\u4ee3\u7801"},"content":{"rendered":"<h3>\u5f15\u8a00<\/h3>\n<p style=\"text-align: justify;\">\u6309\u7167\u4e0a\u4e00\u7ae0\u7684\u7b97\u6cd5\u6d41\u7a0b\uff0c\u672c\u7ae0\u7ed9\u51fa\u4e00\u4e2a\u81ea\u5df1\u7528Java\u4ee3\u7801\u53ca\u9762\u5411\u5bf9\u8c61\u601d\u8def\u5b9e\u73b0\u7684\u8681\u7fa4\u7b97\u6cd5\u3002\u5c3d\u91cf\u8ffd\u6c42\u4ee3\u7801\u7684\u8d28\u91cf\u3001\u53ef\u8bfb\u6027\u548c\u4f18\u96c5\u6027\uff0c\u4f46\u4e5f\u96be\u514d\u4f1a\u6709\u5199\u5f97\u4e0d\u8fbe\u6807\u7684\u5730\u65b9\uff0c\u5e0c\u671b\u5927\u5bb6\u80fd\u53bb\u7c97\u53d6\u7cbe\uff0c\u83b7\u53d6\u5230\u5bf9\u81ea\u5df1\u6709\u76ca\u7684\u90e8\u5206\u5373\u53ef\u3002<\/p>\n<h3>\u9053\u8def\u7c7b-Road<\/h3>\n<p style=\"text-align: justify;\">\u6bcf\u6761\u9053\u8def\u6709\u5b83\u7684\u8ddd\u79bb\u548c\u4fe1\u606f\u7d20\u6d53\u5ea6\uff0c\u6240\u4ee5\u9700\u8981\u62bd\u8c61\u51fa\u4e00\u4e2a\u8fd9\u4e2aRoad\u7c7b\uff0c\u52bf\u5fc5\u80fd\u4e3a\u540e\u7eed\u64cd\u4f5c\u5e26\u6765\u5f88\u5927\u4fbf\u5229~\u5c31\u50cf\u4e0b\u9762\u4e00\u6837\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Road\r\n{\r\n    float distance; \/\/\u8ddd\u79bb\r\n    float pheromone; \/\/\u4fe1\u606f\u7d20 \r\n\r\n    \/\/\u6784\u9020\u51fd\u6570\r\n    ......\r\n}<\/pre>\n<h3>\u57ce\u5e02\u7c7b-City<\/h3>\n<p style=\"text-align: justify;\">\u4e3a\u65b9\u4fbf\u8682\u8681k\u9009\u51fa\u4e0b\u4e00\u6b65\u8981\u8d70\u7684\u57ce\u5e02j\uff0c\u6211\u4eec\u8fd8\u9700\u5efa\u7acb\u4e00\u4e2aCity\u7c7b\uff0c\u4e34\u65f6\u5b58\u50a8\u4e0e\u8682\u8681k\u5f53\u524d\u6240\u5904\u57ce\u5e02i\u76f8\u90bb\u7684\u6240\u6709\u57ce\u5e02j\u7684\u9009\u62e9\u6982\u7387\u3001\u80fd\u89c1\u5ea6\u4e0e\u4fe1\u606f\u7d20\u6d53\u5ea6\u548c\uff08\u5b58\u50a8\u8be5\u503c\u662f\u4e3a\u65b9\u4fbf\u540e\u7eed\u8ba1\u7b97\u6240\u6709\u76f8\u90bb\u57ce\u5e02\u7684\u603b\u548c\u7528\uff09\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class NeighborCity \r\n{\r\n    int id;\r\n    float rate; \/\/\u88ab\u9009\u62e9\u6982\u7387\r\n    float vap; \/\/\u80fd\u89c1\u5ea6\u548c\u4fe1\u606f\u7d20\u6d53\u5ea6\u603b\u548c\r\n    \r\n    \/\/\u6784\u9020\u51fd\u6570\r\n    ......\r\n}<\/pre>\n<h3>\u8682\u8681\u7c7b-Ant<\/h3>\n<p style=\"text-align: justify;\">\u4e3a\u523b\u753b\u8682\u8681\u5bfb\u5f84\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u884c\u4e3a\uff0c\u5982\u968f\u673a\u5316\u521d\u59cb\u51fa\u751f\u57ce\u5e02\u3001\u9009\u62e9\u57ce\u5e02\u3001\u5230\u8fbe\u57ce\u5e02\u7b49\uff0c\u5efa\u7acbAnt\u7c7b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Ant \r\n{\r\n    int[] passed;\/\/\u5df2\u7ecf\u8fc7\u7684\u57ce\u5e02\u5217\u8868\uff08\u7981\u5fcc\u8868\uff09\r\n    float passedLength;\/\/\u73af\u6e38\u957f\u5ea6\r\n    int curCity;\/\/\u8682\u8681\u5f53\u524d\u6240\u5728\u57ce\u5e02\r\n    int curIndex;\/\/\u4e0b\u4e00\u57ce\u5e02\u9700\u52a0\u5165\u7981\u5fcc\u8868\u7684\u5bf9\u5e94\u4f4d\u7f6e\r\n    \r\n    \/\/\u521d\u59cb\u5316\u8682\u8681\u6570\u636e\r\n    void init()\r\n    {\r\n        initPassed();\r\n        passedLength=0.0f;\r\n        curIndex=0;\r\n        initBeginCity();\r\n    }\r\n    \r\n    \/\/\u521d\u59cb\u5316\u7981\u5fcc\u8868\r\n    void initPassed()\r\n    {\r\n        passed=new int[Constant.CITY_NUM+1];\r\n        for(int i=0;i&lt;passed.length;i++)\r\n            passed[i]=Integer.MIN_VALUE;\r\n    }\r\n    \r\n    \/\/\u521d\u59cb\u5316\u8682\u8681\u6240\u5728\u57ce\u5e02\r\n    void initBeginCity()\r\n    {\r\n        Random rand=new Random();\r\n        int beginCity=rand.nextInt(Constant.CITY_NUM);\r\n        reachNextCity(beginCity);\r\n    }\r\n    \r\n    \/\/\u5230\u8fbe\u4e0b\u4e00\u4e2a\u57ce\u5e02\r\n    void reachNextCity(int nextCity)\r\n    {\r\n        \/\/\u7d2f\u52a0\u5468\u6e38\u8ddd\u79bb\r\n        passedLength += Constant.roads[curCity][nextCity].distance;\r\n        \r\n        \/\/\u524d\u8fdb\r\n        curCity=nextCity;\r\n        passed[curIndex++]=nextCity+1;\r\n    }\r\n    \r\n    \/\/\u5224\u65ad\u57ce\u5e02nCity\u662f\u5426\u5728\u7981\u5fcc\u8868\u4e2d\r\n    boolean isPassedCity(int nCity)\r\n    {\r\n        for(int i=0;passed[i] != Integer.MIN_VALUE;i++)\r\n        {\r\n            if(passed[i] == nCity) \/\/\u5b58\u5728\u7684\u57ce\u5e02\r\n                return true;\r\n        }\r\n        return false;\r\n    }\r\n}<\/pre>\n<h3>\u8681\u7fa4\u7b97\u6cd5\u7c7b-AntAlgorithm<\/h3>\n<p style=\"text-align: justify;\">\u8be5\u7c7b\u5bf9\u8681\u7fa4\u7b97\u6cd5\u7684\u5404\u73af\u8282\u8fdb\u884c\u603b\u63a7\u8c03\u5ea6\uff0c\u6a21\u62df\u8682\u8681\u51fa\u751f\u3001\u8682\u8681\u9009\u62e9\u8def\u7ebf\u3001\u8682\u8681\u73af\u6e38\u3001\u4fe1\u606f\u7d20\u66f4\u65b0\u3001\u591a\u8f6e\u73af\u6e38\u7b49\u8fc7\u7a0b\uff0c\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class AntAlgorithm \r\n{\t\r\n    private Ant[] ants;\/\/\u8681\u7fa4\t\r\n    float minLength = Float.MAX_VALUE;\/\/\u5f53\u524d\u6700\u77ed\u8ddd\u79bb\r\n    int[] minRoute; \/\/\u5f53\u524d\u6700\u77ed\u8def\u7ebf\r\n    \r\n    \/\/\u6784\u9020\u51fd\u6570\r\n    AntAlgorithm()\r\n    {\r\n        ants=new Ant[Constant.ANT_NUM];\r\n        for(int i=0;i&lt;Constant.ANT_NUM;i++)\r\n            ants[i]=new Ant();\r\n            \r\n        minRoute=new int[Constant.CITY_NUM];\r\n    }\r\n    \r\n    \/\/\u7b97\u6cd5\u6d41\u7a0b\u5f00\u59cb\r\n    void run()\r\n    {\r\n        for(int nc = 1; nc &lt;= Constant.NC; nc++) \/\/\u8fed\u4ee3\u6b21\u6570\r\n        {\r\n            \/\/\u521d\u59cb\u5316\u8682\u8681\u6570\u636e\r\n            for(int k = 0; k &lt; Constant.ANT_NUM; k++)\r\n                ants[k].init();\r\n            \r\n            \/\/\u904d\u5386\u6240\u6709\u57ce\u5e02\r\n            for(int look = 1; look &lt; Constant.CITY_NUM; look++)\r\n            {\r\n                for(int k = 0; k &lt; Constant.ANT_NUM; k++)\/\/\u6bcf\u53ea\u8682\u8681\r\n                {\r\n                    int nextCity = select(ants[k]);\/\/\u9009\u62e9\u4e0b\u4e00\u4e2a\u57ce\u5e02\t\t\t\t\t\r\n                    ants[k].reachNextCity(nextCity);\/\/\u5230\u8fbe\u4e0b\u4e00\u4e2a\u57ce\u5e02\r\n                }\r\n            }\r\n    \r\n            \/\/\u8fd4\u56de\u8d77\u70b9\u57ce\u5e02\u5e76\u8ba1\u7b97\u6700\u4f18\u8def\u5f84\r\n            for(int k = 0; k &lt; Constant.ANT_NUM; k++)\/\/\u6bcf\u53ea\u8682\u8681\r\n            {\r\n                ants[k].reachNextCity(ants[k].passed[0]-1);\r\n                if(minLength &gt; ants[k].passedLength)\r\n                {\r\n                    minLength = ants[k].passedLength; \/\/\u8bb0\u5f55\u6700\u77ed\u8ddd\u79bb\r\n                    writeRoute(ants[k].passed); \/\/\u8bb0\u5f55\u6700\u77ed\u8def\u7ebf\r\n                }\r\n            }\r\n            \r\n            \/\/\u5bf9roads\u8fdb\u884c\u4fe1\u606f\u7d20\u66f4\u65b0\r\n            for(int i = 0; i &lt; Constant.CITY_NUM; i++)\r\n            {\r\n                for(int j = 0; j &lt; Constant.CITY_NUM; j++)\r\n                {\r\n                    \/\/\u6240\u6709\u8def\u5f84\u7684\u4fe1\u606f\u7d20\u5747\u84b8\u53d1\r\n                    Constant.roads[i][j].pheromone *= Contant.p;\r\n                    \r\n                    for(int k = 0; k &lt; Constant.ANT_NUM; k++)\r\n                    {\r\n                        for(int n = 0; n &lt; Constant.CITY_NUM; n++)\r\n                        {\r\n                            int curCity = ants[k].passed[n]-1;\r\n                            int nextCity = ants[k].passed[(n+1) % Constant.CITY_NUM]-1;\r\n                            \r\n                            if(curCity == i &amp;&amp; nextCity == j)\/\/\u51fa\u73b0\u8fc7\u8fd9\u6bb5\u8def\u5f84\r\n                            {\r\n                                \/\/\u66f4\u65b0\u8def\u5f84curCity,nextCity\u4fe1\u606f\u7d20\r\n                                float dp = Constant.Q \/ ants[k].passedLength;\/\/\u4fe1\u606f\u7d20\u589e\u91cf\r\n                                Constant.roads[i][j].pheromone += dp;\t\r\n                            }\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n        }\t\r\n    }\r\n    \r\n    \/\/\u8ba1\u7b97\u9009\u62e9\u6982\u7387+\u8f6e\u8d4c\r\n    int select(Ant ant)\r\n    {\r\n        float totalVap = 0.0f;\r\n        List&lt;NeighborCity&gt; neighborCityList = new LinkedList&lt;NeighborCity&gt;();\r\n        NeighborCity neighborCity;\r\n        for(int nextCity = 0; nextCity &lt; Constant.CITY_NUM; nextCity++)\r\n        {\r\n            if(!ant.isPassedCity(nextCity+1))\/\/\u53ef\u9009\u62e9\u57ce\u5e02\r\n            {\r\n                double pheromone = Constant.roads[ant.curCity][nextCity].pheromone;\r\n                pheromone = Math.pow(pheromone, Constant.alpha);\r\n                \r\n                double visibility=1.0f \/ Constant.roads[ant.curCity][nextCity].distance;\/\/\u80fd\u89c1\u5ea6\r\n                visibility=Math.pow(visibility, Constant.beta);\r\n                \r\n                float vap = (float) visibility + (float) pheromone;\r\n                totalVap += vap; \/\/\u7d2f\u52a0VAP\r\n\r\n                neighborCity = new NeighborCity(nextCity, vap);\r\n\r\n                neighborCityList.add(neighborCity);\/\/\u6dfb\u52a0\r\n            }\r\n        }\r\n\r\n\r\n        \/\/\u8ba1\u7b97\u6bcf\u4e2a\u57ce\u5e02\u88ab\u9009\u4e2d\u7684\u6982\u7387\r\n        ListIterator&lt;NeighborCity&gt; iterator = neighborCityList.listIterator(); \/\/\u83b7\u53d6\u8fed\u4ee3\u5668\r\n        while (iterator.hasNext())\r\n        {\r\n            neighborCity = iterator.next(); \/\/\u53d6\u57ce\u5e02\r\n            neighborCity.rate = neighborCity.vap \/ totalVap; \/\/\u8ba1\u7b97\u6982\u7387\r\n        }\r\n        \r\n        \/\/\u8d4c\u8f6e\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u57ce\u5e02\r\n        float rate = (float) Math.random();\r\n        iterator = neighborCityList.listIterator(); \/\/ \u83b7\u53d6\u8fed\u4ee3\u5668\r\n        while (iterator.hasNext())\r\n        {\r\n            neighborCity = iterator.next();\r\n            if(rate &lt;= neighborCity.rate)\r\n                return neighborCity.id; \/\/\u9009\u62e9\u8be5\u57ce\u5e02\uff01\r\n            else\r\n                rate -= neighborCity.rate;\r\n        }\r\n        \r\n        \/\/\u6982\u7387\u7cbe\u5ea6\u6240\u81f4\uff0c\u4eba\u4e3a\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u57ce\u5e02\r\n        iterator = neighborCityList.listIterator();\r\n        while (iterator.hasNext())\r\n        {\r\n            neighborCity = iterator.next();\r\n            if(iterator.hasNext() == false) \/\/\u9009\u62e9\u6700\u540e\u4e00\u4e2a\u57ce\u5e02!\r\n                return neighborCity.id;\r\n        }\r\n        \r\n        return neighborCity.id;\r\n    }\r\n    \r\n    \/\/\u8bb0\u5f55\u6700\u77ed\u8def\u7ebf\r\n    void writeRoute(int[] route)\r\n    {\r\n        for(int i = 0; i &lt; minRoute.length; i++)\r\n            minRoute[i] = route[i];\r\n    }\r\n}<\/pre>\n<h3>\u7ed3\u8bed<\/h3>\n<p style=\"text-align: justify;\">\u6838\u5fc3\u4ee3\u7801\u5df2\u7ecf\u8d34\u5b8c\u3002\u4e0b\u4e00\u7ae0\u5bf9\u8681\u7fa4\u7b97\u6cd5\u6240\u6d89\u53ca\u7684\u53c2\u6570\u8fdb\u884c\u4e00\u4e9b\u4ee3\u7801\u4e0a\u7684\u8bf4\u660e\uff0c\u5e76\u7ed9\u51fa\u7b97\u6cd5\u7684\u5b9e\u9645\u8fd0\u884c\u7ed3\u679c\uff0c\u540c\u65f6\u4e0e\u524d\u9762\u9057\u4f20\u7b97\u6cd5\u7684\u8fd0\u884c\u7ed3\u679c\u8fdb\u884c\u5bf9\u6bd4\u5206\u6790\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u6309\u7167\u4e0a\u4e00\u7ae0\u7684\u7b97\u6cd5\u6d41\u7a0b\uff0c\u672c\u7ae0\u7ed9\u51fa\u4e00\u4e2a\u81ea\u5df1\u7528Java\u4ee3\u7801\u53ca\u9762\u5411\u5bf9\u8c61\u601d\u8def\u5b9e\u73b0\u7684\u8681\u7fa4\u7b97\u6cd5\u3002\u5c3d\u91cf\u8ffd\u6c42\u4ee3\u7801\u7684\u8d28\u91cf\u3001\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56,46],"tags":[],"_links":{"self":[{"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/posts\/1024"}],"collection":[{"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/comments?post=1024"}],"version-history":[{"count":10,"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/posts\/1024\/revisions"}],"predecessor-version":[{"id":3172,"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/posts\/1024\/revisions\/3172"}],"wp:attachment":[{"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/media?parent=1024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/categories?post=1024"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yaochenkun.site\/index.php\/wp-json\/wp\/v2\/tags?post=1024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}