快轉到主要內容

利用pulp解決線性規劃問題

·702 字·2 分鐘
Y Cheung
作者
Y Cheung
Blogger, Programer & Traveler.

Stardew Valley(星露谷物語) 在 nintendo switch 上架的消息刺激 Y.Cheung 又回鍋啦~這次想做出完美開局,於是在研究過 wiki檔案 後決定計算出在一個季度月內使用初始資金種植什麼農作物種多少最轉錢。不考慮跨季度農作物與季度月內收成後售出後增加的資金部分,並且默認每天晚上都會澆水讓其生長。

因此可以簡化為一個線性規劃問題。

首先,基本數據的錄入並存儲在mysql數據庫中。基本資料包含:農作物名稱,成長天數,持續生長天數,每季度月最大收穫次數,種子價格,售賣單價,生長季節,每季度月最大生長周期。

每季度月最大生長周期由以下公式計算值,其他可在wiki中找到值。

每季度月最大生長周期 = 成長天數 + ((每季度月最大收穫次數 - 1) * 持續生長天數)

然後,安裝pulp,開始設計模型。

PULP使用起來也很簡單,具體可以參看文檔給出的CASE STUDIES

用戶輸入三個參數:所持金額,農作天數(此處可做更多限制),季度月。

在本問題中,先定義這是一個線性規劃求最大值的問題。

1model = pulp.LpProblem("Profit maximising problem", pulp.LpMaximize)

找出指定季度月中能種植的農作物作為元素集合(Ingredients)在pulp中處理為 pulp.LpVariable.dicts

1ingredient_vars = pulp.LpVariable.dicts("Ingr", Ingredients, lowBound=0, cat='Integer')

指定模型計算公式,pulp.lpSum 就是求和。profit是各農作物元素利潤的集合。單個農作物利潤的計算在模型建立之前已經計算出並暫存在變量profit中。利潤的計算方法區分一種多收的農作物與一種一收的農作物。

1model += pulp.lpSum(profit[i] * ingredient_vars[i] for i in Ingredients), "profit"

指定模型的限制條件,所持資金是固定的,即能夠購買的種子是有限制的。

1model += pulp.lpSum(cost[i] * ingredient_vars[i] for i in Ingredients) <= money

問題解決!

1model.solve()

最後,打印出最優解中各農作物元素名稱與數量值。

這個問題涉及到的因素很多,以後有時間可以再考慮更多的因素,讓結果更準確。

相關文章

解決本站故障小記

·505 字·2 分鐘
本站突然發生 HTTP502 錯誤,導致幾天不能訪問,又適逢勞動節休假,Y.CHEUNG 沒帶電腦,而且縂想著反正訪問量極低,也就不趕著修復站點,今日才來細細察看一番。

MYSQL外鍵導致無法清空表

·132 字·1 分鐘
最近設計的數據庫表含有外鍵,結果在開發時truncate table的時候報錯了, 1SQL错误(1701):Cannot truncate a table referenced in a foreign key constraint (`rollcall2`.`oncall`, CONSTRAINT `FK__calls` FOREIGN KEY (`calls_id`) REFERENCES `rollcall2`.`calls` (`id`)) 解決辦法如下: 1SET FOREIGN_KEY_CHECKS = 0; 2TRUNCATE table; 3SET FOREIGN_KEY_CHECKS = 1; 當然也可以通過移除外鍵之類的辦法,但是感覺還是用上面幾行命令來解決比較方便。

使用 extundelete 恢复rm误操作

·438 字·1 分鐘
系统:CentOS 7 文件系统:EXT4 今天 YCHEUNG 不小心用 rm 命令误删了一个名字奇奇怪怪的文件,因为名字太奇怪想不起来它是啥,怕是重要文件,所以急急忙忙去找办法恢复。

FEDORA安装electron-builder报错make:g++:命令未找到之解决

·354 字·1 分鐘
系统环境: Fedora 24 Node v4.5.0 NPM v2.15.9 NPM安装electron-builder报错 # 1$npm install electron-builder --save=dev 2npm WARN package.json [email protected] No repository field. 3npm WARN package.json [email protected] No README data 4 5> [email protected] install /var/www/html/test/node_modules/dev/node_modules/inotify 6> node-gyp rebuild 7 8make: Entering directory '/var/www/html/test/node_modules/dev/node_modules/inotify/build' 9 CXX(target) Release/obj.target/inotify/src/bindings.o 10make: g++:命令未找到 11inotify.target.mk:97: recipe for target 'Release/obj.target/inotify/src/bindings.o' failed 12make: *** [Release/obj.target/inotify/src/bindings.o] Error 127 13make: Leaving directory '/var/www/html/test/node_modules/dev/node_modules/inotify/build' 14gyp ERR! build error 15gyp ERR! stack Error: `make` failed with exit code: 2 16gyp ERR! stack at ChildProcess.onExit (/usr/lib/node_modules/npm/node_modules.bundled/node-gyp/lib/build.js:276:23) 17gyp ERR! stack at emitTwo (events.js:87:13) 18gyp ERR! stack at ChildProcess.emit (events.js:172:7) 19gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12) 20gyp ERR! System Linux 4.7.4-200.fc24.x86_64 21gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" 22gyp ERR! cwd /var/www/html/test/node_modules/dev/node_modules/inotify 23gyp ERR! node -v v4.5.0 24gyp ERR! node-gyp -v v3.4.0 25gyp ERR! not ok 26npm WARN optional dep failed, continuing [email protected] 27npm WARN deprecated [email protected]: This package is deprecated. Use Object.assign. 28npm WARN optional dep failed, continuing [email protected] 29npm WARN optional dep failed, continuing [email protected] 30npm ERR! Linux 4.7.4-200.fc24.x86_64 31npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "electron-builder" "--save=dev" 32npm ERR! node v4.5.0 33npm ERR! npm v2.15.9 34npm ERR! code ELIFECYCLE 35 36npm ERR! [email protected] install: `node-gyp rebuild` 37npm ERR! Exit status 1 38npm ERR! 39npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'. 40npm ERR! This is most likely a problem with the inotify package, 41npm ERR! not with npm itself. 42npm ERR! Tell the author that this fails on your system: 43npm ERR! node-gyp rebuild 44npm ERR! You can get information on how to open an issue for this project with: 45npm ERR! npm bugs inotify 46npm ERR! Or if that isn't available, you can get their info via: 47npm ERR! 48npm ERR! npm owner ls inotify 49npm ERR! There is likely additional logging output above. 50 51npm ERR! Please include the following file with any support request: 52npm ERR! /var/www/html/test/npm-debug.log 解决方法: # 安装G++ $sudo dnf install gcc-c++

Unity AvProVideo Plugin 中設置影片在安卓SD卡上的絕對地址

·359 字·1 分鐘
Media Play 設置 # Video Location 選擇 Absolute Path Or URL。 找到安卓設備SDCard的根目錄(ROOT PATH) # 如果電腦上安裝了adb工具,使用指令 adb shell echo $EXTERNAL_STORAGE 打印SD Card Root directory。 如果安卓設備上安裝了terminal工具,(比如Terminal Emulator for Android),使用指令 ls 根據經驗篩選可能的目錄。 一般而言,SD卡的根目錄可能但不限於以下幾種: /mnt/sdcard /storage/sdcard0 /storage/sdcard /sdcard 設置權限 # 在 AndroidManifest.xml 文件中的<uses-sdk前添加以下語句: 1<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 通常該文件位於Assets/Plugins/Android/文件夾內,該配置文件詳解請參閱官方文檔《App Manifest》(有中文版)。