PuLP:目标函数:连接多个lpSum

时间:2019-01-16 12:56:06

标签: python pulp

我试图将多个lpSum表达式连接为一个长表达式,这将是我的目标函数。但是,我以优雅的方式合并这些表达式的尝试导致了不良结果。

我想要这样的东西:

a = pulp.lpSum(...)
b = pulp.lpSum(...)
c = pulp.lpSum(...)

prob += a + b - c

我的代码更具体:


    alloc_prob = pulp.LpProblem("Supplier Allocation Problem", pulp.LpMinimize)

    TPC_func = pulp.lpSum(X[s][p]*procCosts[s][p] for s in supplier for p in 
    project), "Total Procurement Costs"
    TTC_func = pulp.lpSum(X[s][p]*transCosts[s][p] for s in supplier for p in 
    project), "Total Transportation Costs (incl. taxes/duties)"
    TD_func = pulp.lpSum(X_SEP[c][1]*discountFactor['Bonus / ton [€/t]'][c] for 
    c in company), "Total Discounts"`

    # Objective function: TPC + TTC - TD -> min
    alloc_prob += TPC_func  + TTC_func - TD_func

我已经尝试了不同的嵌套方法,例如:

    prob += [pulp.lpSum(X[s][p]*procCosts[s][p] + X[s][p]*transCosts[s][p] for s 
    in supplier for p in project) - pulp.lpSum(X_SEP[c][1]*discountFactor['Bonus 
    / ton [€/t]'][c] for c in company)]

输出将执行应有的操作。但是,这既不是很好的代码,也不能分配给目标函数。有一种明智的实施方式吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

没有看到错误,我可以百分百确定,但是我认为您在lpsum中包含的名称引起了问题,请尝试以下操作

public static void LogMessage(string message)
        {
            var clusterName = "ABC";
            var db = "Kusto";
            var table = "Log";
            var mappingName = "KustoJsonMapping";

            // Set up table
            var kcsbEngine =
                new KustoConnectionStringBuilder($"https://KustoDB.kusto.windows.net");

            using (var kustoAdminClient = KustoClientFactory.CreateCslAdminProvider(kcsbEngine))
            {
                var columns = new List<Tuple<string, string>>()
        {
            new Tuple<string, string>("logId", "System.String"),
            new Tuple<string, string>("logDtm", "System.DateTime"),
            new Tuple<string, string>("userName", "System.String"),
        };

                var command = CslCommandGenerator.GenerateTableCreateCommand(table, columns);
                kustoAdminClient.ExecuteControlCommand(databaseName: db, command: command);

                // Set up mapping
                var columnMappings = new List<JsonColumnMapping>();
                columnMappings.Add(new JsonColumnMapping()
                { ColumnName = "logId", JsonPath = "$.Id" });
                columnMappings.Add(new JsonColumnMapping()
                { ColumnName = "logDtm", JsonPath = "$.Timestamp" });
                columnMappings.Add(new JsonColumnMapping()
                { ColumnName = "userName", JsonPath = "$.Message" });

                command = CslCommandGenerator.GenerateTableJsonMappingCreateCommand(
                                                    table, mappingName, columnMappings);
                kustoAdminClient.ExecuteControlCommand(databaseName: db, command: command);
            }

            }
        }