ファイル): set SET_NAME := element1 element2 ... ; (要素を列挙) 要素は文字列または数値。文字列に空白が含まれる場合は引用符 ( " " ) で囲む。 # --- In model file (model.mod) --- set PRODUCTS; # Set of products set RESOURCES; # Set of resources # --- In data file (data.dat) --- set PRODUCTS := "Steel Plate" Screws Bolts ; set RESOURCES := Labor MachineA MachineB ; 21
model file (model.mod) --- set PRODUCTS; set PARTS; set PARTS_NEEDED {PRODUCTS}; # For each product, a set of parts needed # --- In data file (data.dat) --- set PRODUCTS := Car Bike ; set PARTS := Wheel Frame Seat Chain ; set PARTS_NEEDED[Car] := Wheel Frame Seat ; set PARTS_NEEDED[Bike] := Wheel Frame Seat Chain ; 22
, last() , next() , prev() などの関数が利用可能。 主に期間やステップを表すのに使う。 # --- In model file (model.mod) --- set PERIODS ordered; # Time periods # --- In data file (data.dat) --- set PERIODS := Jan Feb Mar Apr ; # --- In model file (model.mod) --- param initial_inventory {PRODUCTS}; var Inventory {PRODUCTS, PERIODS}; subject to InventoryBalance {p in PRODUCTS, t in PERIODS: t != first(PERIODS)}: Inventory[p,t] = Inventory[p, prev(t)] + ... ; # Using prev() 23
ORIGINS within LOCATIONS; # Origins must be a subset of Locations set DESTINATIONS within LOCATIONS; # Destinations must be a subset of Locations # --- In data file (data.dat) --- set LOCATIONS := London Paris Berlin Tokyo ; set ORIGINS := London Paris ; set DESTINATIONS := Berlin Tokyo ; # set ORIGINS := London Chicago ; # This would cause an error 25
--- In model file (model.mod) --- set PRODUCTS; param tax_rate {PRODUCTS} default 0.05; # Default tax rate is 5% # --- In data file (data.dat) --- param tax_rate := WidgetA 0.08 ; # WidgetA has 8% tax # WidgetB will use the default 5% tax rate 28
0) の連続変数とみなされる。 # --- In model file (model.mod) --- set PRODUCTS; set LOCATIONS; set CUSTOMERS; var Produce {PRODUCTS}; # Amount of each product to produce var Transport {PRODUCTS, LOCATIONS, CUSTOMERS}; # Amount transported 31
(製品ごと、期間ごとの生産量) # --- In model file (model.mod) --- set ORIG; # Origins set DEST; # Destinations set PRODUCTS; var Transport {PRODUCTS, ORIG, DEST} >= 0; # Amount of product p shipped from i to j 33
が使用される。 # --- In model file (model.mod) --- var x >= 0, := 1.0; # Variable x with initial value 1.0 var y {i in 1..5} := i; # Initial value of y[i] is i 34
minimize OBJECTIVE_NAME: expression; OBJECTIVE_NAME : 目的関数に付ける名前。 expression : 変数、パラメータ、定数を用いた数式。 sum などの演算子も使用可能。 # --- In model file (model.mod) --- set PRODUCTS; param unit_profit {PRODUCTS}; param unit_cost {PRODUCTS}; var Produce {PRODUCTS} >= 0; # Example 1: Maximize total profit maximize TotalProfit: sum {p in PRODUCTS} unit_profit[p] * Produce[p]; # Example 2: Minimize total cost minimize TotalCost: sum {p in PRODUCTS} unit_cost[p] * Produce[p]; 36
index_set 内の各要素に対して制約が生成される。 条件式 ( : condition ) を追加して、特定のインデックスに対してのみ制約を生成することも可能。 # --- In model file (model.mod) --- set PLANTS; set PRODUCTS; set PERIODS ordered; param capacity {PLANTS, PRODUCTS}; param demand {PRODUCTS, PERIODS}; var Produce {PLANTS, PRODUCTS, PERIODS} >= 0; var Inventory {PRODUCTS, PERIODS} >= 0; # Production capacity constraint for each plant, product, period subject to CapacityLimit {pl in PLANTS, pr in PRODUCTS, t in PERIODS}: Produce[pl, pr, t] <= capacity[pl, pr]; # Inventory balance constraint for each product, period (except the first) subject to InvBalance {pr in PRODUCTS, t in PERIODS: t > first(PERIODS)}: Inventory[pr, t] = Inventory[pr, prev(t)] + sum {pl in PLANTS} Produce[pl, pr, t] - demand[pr, t]; 40
reset data の有無による)。 include <filename>; 指定されたファイルの内容をその場で実行する。 .mod , .dat , .run ファイルのいずれでも可。 ampl: model transportation.mod; ampl: data transportation_set1.dat; ampl: # Model is ready with data set 1 ampl: reset data; # Clear current data values ampl: data transportation_set2.dat; ampl: # Model is ready with data set 2 52
例: show Produce; は var Produce {PRODUCTS} >= 0; のような宣言を表示。 expand <item_name>; 指定されたインデックス付きの制約や目的関数を、現在のデータに基づいて 展開し、具体的なインスタン スを表示する。デバッグに非常に便利。 ampl: show ResourceLimit; subject to ResourceLimit{r in RESOURCES}: sum{p in PRODUCTS} usage[r,p]*Produce[p] <= availability[r]; ampl: expand ResourceLimit['Labor']; # Show the constraint for Labor resource subject to ResourceLimit['Labor']: usage['Labor','Steel']*Produce['Steel'] + usage['Labor','Bolts']*Produce['Bolts'] + usage['Labor','Nuts']*Produce['Nuts'] + usage['Labor','Screws']*Produce['Screws'] <= availability['Labor']; 56
reset data; 現在の データ値のみをクリアする。モデルの構造(宣言)は保持される。 異なるデータセットで同じモデルを解く際に使う。 reset data <item1>, <item2>, ... ; 指定されたパラメータや集合のデータのみをリセットする。 ampl: model m.mod; data d1.dat; solve; ampl: display x; ampl: reset data; # Clear data from d1.dat ampl: data d2.dat; # Load new data ampl: solve; # Solve with new data ampl: display x; ampl: reset; # Clear everything (model, data, options) 57
solve_my_model.run --- # Reset AMPL state (optional but often good practice) reset; # Load model and data model my_model.mod; data my_data.dat; # Set options option solver cplex; option cplex_options 'timelimit=600'; # Solve the problem solve; # Display results display TotalCost; display Produce > produce_results.txt; display Transport; # Optionally display solver status display solve_result, solve_message; 63
} # --- sensitivity_analysis.run --- model base.mod; set SCENARIOS := low_demand normal_demand high_demand; param demand_factor {SCENARIOS}; for {s in SCENARIOS} { reset data; # Reset previous data data base_data.dat; # Load base data let demand_multiplier := demand_factor[s]; # Modify a parameter (using let) # OR modify data file parameters directly if needed before loading option solver cplex; solve; print "--- Scenario:", s, "---" > scenario_results.txt; display _obj >> scenario_results.txt; display Produce >> scenario_results.txt; } Note: let コマンドは、モデル内のパラメータや変数の値を一時的に変更するために使用します。 65
B(AMPL Environment); B -- model & data --> C{Problem Instance Generation}; C -- problem instance --> D[Solver]; D -- solution --> C; C -- results (values, duals, status) --> B; B -- display --> A; 69
制約の下限値、上限値 ( = , >= , <= に応じて定義される)。 constraint.slack : 制約の余裕。 <= 制約: ub - body (非負) >= 制約: body - lb (非負) = 制約: 0 (常に) display constraint.slack; などで表示。 ampl: display ResourceLimit.body; # Show how much of each resource is used ampl: display ResourceLimit.ub; # Show the availability of each resource ampl: display ResourceLimit.slack; # Show the unused amount of each resource ResourceLimit.slack [*] := Labor 0.0 # Binding constraint MachineA 15.0 # 15 units of slack MachineB 0.0 # Binding constraint ; 78
set NODES; node Balance {n in NODES}; # Define balance constraint at each node set ARCS within NODES cross NODES; param cost {ARCS}; param capacity {ARCS}; var Flow {ARCS} >= 0, <= capacity; minimize TotalCost: sum {(u,v) in ARCS} cost[u,v] * Flow[u,v]; subject to BalanceConstraint {n in NODES}: sum {(u,n) in ARCS} Flow[u,n] + supply[n] # In-flow + supply = sum {(n,v) in ARCS} Flow[n,v] + demand[n]; # Out-flow + demand 82
yA + yB <= 1; AMPLは ==> (implication), if-then-else などの構文もサポートしており、これらがMIP制約に変換され る。 var BuildX binary; var BuildY binary; var UseProcessA binary; var UseProcessB binary; # If we build X, we must build Y subject to Implication1: BuildX ==> BuildY; # Equivalent to BuildX <= BuildY # Use either Process A or Process B, but not both subject to Choice: UseProcessA + UseProcessB = 1; 83
Language for Mathematical Programming" by Robert Fourer, David M. Gay, and Brian W. Kernighan. AMPLのバイブル。詳細な解説と多数の例題。オンラインで利用可能な章も多い。 日本語サイト https://scmopt.github.io/ampl ソルバーのマニュアル: CPLEX, Gurobiなどの各ソルバーのドキュメントも、オプション設定などで参照が必 要。 オンラインコースやチュートリアル: 様々な大学やプラットフォームでAMPLを用いた最適化コースが提供さ れている。 89