AMPL 代码。错误:问题出在哪里? mod 还是 dat 文件?

问题描述 投票:0回答:1
set D := {1, 2, 3, 4};

param d :=
1 80
2 70
3 120
4 150
5 100;

param a := 1;  # Cost per napkin for cleaning
param b := 2;  # Cost per napkin for buying new napkins
param c := 0.5;  # Cost per day for storing clean napkins

param stock_init := 100;  # Initial number of napkins in stock


 param  D := {1, 2, 3, 4};;  # Number of napkins needed each day
  param a;  # Cost per napkin for cleaning
  param b;  # Cost per napkin for buying new napkins
 param c;  # Cost per day for storing clean napkins
 param stock_init;  # Initial number of napkins in stock

var Buy{1..n} integer >= 0;  # Number of napkins bought each day
 var Clean{1..n} integer >= 0;  # Number of napkins cleaned each day
var Stock{0..n} integer >= 0;  # Number of napkins in stock at the end of each day

minimize sum(i in 1..n, a*Clean[i] + b*Buy[i] + c*Stock[i]);

 subject to NapkinBalance{i in 1..n}:
Stock[i-1] + Buy[i] + Clean[i] = d[i] + Stock[i];

subject to StockInit: Stock[0] = stock_init;

solve;

for i in 1..n do
printf "Day %d: Buy %d, Clean %d, Stock %d\n", i, Buy[i], Clean[i], Stock[i];

结束;

这个 .dat 和 .mod 文件没有给我所需的输出。我不知道是 mod 文件的问题还是 dat 文件的问题。我试图根据这些条件找到最佳解决方案。 为了 接下来 5 天的每一天,您都知道自己会需要多少张餐巾纸 需要:分别为 80、70、120、150、100。每天早上 送餐巾时,您可以捡起用过的餐巾 那些(您前一天交付的)。如果你发送那些 (或其中一些)清洁(一张餐巾纸)比他们 第二天早上交货时可以像新的一样使用。你 还可以在任何一天以 $b/餐巾纸购买新餐巾纸, 在您当天交货之前。你也可以 在您的库存中保留干净(新)的餐巾,价格为 $c/天/餐巾 接下来的几天。最初您有 100 张餐巾纸库存。

我期待基于这些约束的最佳解决方案

optimization ampl
1个回答
0
投票

模型存在多个错误,但距离解决问题已经不远了。主要问题如下:

  • 您从
    set D := {1, 2, 3, 4};
    开始,但它没有在任何地方使用。
  • 您没有定义参数
    n
    。你需要
    param n;
  • 您没有指定目标名称(例如,
    minimize Cost: ...;
    )。
  • 您在声明之前就有数据。在
    data;
    部分加载数据之前,您需要声明集合和参数。
  • 您没有限制
    Clean[i]
    ,因此它可以采用任何值,并且是最便宜的选择。

求解以下模型:

param n;  # number of days
param d{1..n};  # Number of napkins needed each day
param a;  # Cost per napkin for cleaning
param b;  # Cost per napkin for buying new napkins
param c;  # Cost per day for storing clean napkins
param stock_init;  # Initial number of napkins in stock

var Buy{1..n} integer >= 0;  # Number of napkins bought each day
var Clean{i in 1..n} integer >= 0;  # Number of napkins cleaned each day
var Stock{0..n} integer >= 0;  # Number of napkins in stock at the end of each day

minimize Cost:
    sum {i in 1..n} (a*Clean[i] + b*Buy[i] + c*Stock[i]);

subject to NapkinBalance{i in 1..n}:
    Stock[i-1] + Buy[i] + Clean[i] = d[i] + Stock[i];

subject to MaxClean{i in 1..n}:
    Clean[i] <= sum {j in 1..i-1} (d[j]-Clean[j]);

subject to StockInit:
    Stock[0] = stock_init;

data;
    param n := 5;
    param d :=
        1 80
        2 70
        3 120
        4 150
        5 100;
    param a := 1;  # Cost per napkin for cleaning
    param b := 2;  # Cost per napkin for buying new napkins
    param c := 0.5;  # Cost per day for storing clean napkins
    param stock_init := 100;  # Initial number of napkins in stock
model;

option solver gurobi;
solve;

for {i in 1..n} {
    printf "Day %d: Buy %d, Clean %d, Stock %d\n", i, Buy[i], Clean[i], Stock[i];
}

你应该得到以下最优解:

Gurobi 11.0.0: optimal solution; objective 480
6 simplex iterations
1 branching nodes

Day 1: Buy 0, Clean 0, Stock 20
Day 2: Buy 50, Clean 0, Stock 0
Day 3: Buy 0, Clean 120, Stock 0
Day 4: Buy 0, Clean 150, Stock 0
Day 5: Buy 0, Clean 100, Stock 0
© www.soinside.com 2019 - 2024. All rights reserved.