简体   繁体   English

使用 PULP 进行需求优化计划

[英]Demand optimization planning using PULP

Need help in solving a demand-optimiztion planning for factories.需要帮助解决工厂的需求优化计划。 Factories have Machines which can make one or more Products in it.工厂有可以生产一种或多种产品的机器。 Each Product takes time to make 1 unit which is known as 'Cycle-Time'.每个产品需要时间来制造 1 个单位,这被称为“周期时间”。 So, to make 10 units of product/component on a machine with cycle-time of 5, it will take 5*10=50 seconds in total.因此,在循环时间为 5 的机器上制造 10 个产品/组件,总共需要 5*10=50 秒。 Not all products can be made in all machines.并非所有产品都可以在所有机器上生产。 So, we need to make products on valid machines and in the most effective manner.因此,我们需要在有效的机器上以最有效的方式生产产品。 Also, each machine has an availability limit (in seconds) and we can't go over it.此外,每台机器都有一个可用性限制(以秒为单位),我们不能 go 超过它。

What we need to do is:我们需要做的是:

  • apply a "cost" of running a machine to make one or more products.应用运行机器来制造一种或多种产品的“成本”。
  • apply a "cost" in case the demand of a product is NOT met.如果产品的需求没有得到满足,则应用“成本”。
  • Objective is to Minimize this cost.目标是最小化这个成本。

I'm also happy if we are able to solve this using equations as constraints (like model += ( x 1 * 0.055555555555556 <= 10000, "material_300005551211-2" )).如果我们能够使用方程式作为约束来解决这个问题(例如 model += ( x 1 * 0.055555555555556 <= 10000, "material_300005551211-2" )),我也很高兴。 but unable to do so at the moment.但目前无法这样做。

Sample data:样本数据:

工厂机器设置

产品需求

I tried optimizing PULP, but this approach isn't working correctly - for example, in case of demand being too high, it doens't max upto the limit of machine's availability but unsure where i'm going wrong.我尝试优化 PULP,但这种方法无法正常工作 - 例如,如果需求太高,它不会最大程度地达到机器可用性的极限,但不确定我哪里出错了。

import pandas as pd
import pulp


factories = pd.read_csv('factory_machines_small.csv', index_col=['Component', 'Machine'])
print(factories)


demand = pd.read_csv('component_demand_small.csv', index_col=['Component'])
print(demand)


production = pulp.LpVariable.dicts("production",
                                     ((component, machine) for component, machine in factories.index),
                                     lowBound=0,
                                     #upBound=1,
                                     cat='Integer')

factory_status = pulp.LpVariable.dicts("factory_status",
                                     ((component, machine) for component, machine in factories.index),
                                     cat='Binary')

model = pulp.LpProblem("Cost minimising scheduling problem", pulp.LpMinimize)

model += pulp.lpSum(
    [production[component, machine] * factories.loc[(component, machine), 'Cycle_Time'] for component, machine in factories.index]
)

# Production in any month must be equal to demand
components = demand.index
for component in components :
    model += production[(component, 'IP01')] + production[(component, 'IP02')] + production[(component, 'IP03')] \
             + production[(component, 'IP04')] + production[(component, 'IP05')] == demand.loc[component, 'Demand']


# Production in any month must be between minimum and maximum capacity, or zero.
for component, machine in factories.index:
    min_production = factories.loc[(component, machine), 'Min_Capacity']
    max_production = factories.loc[(component, machine), 'Max_Capacity']
    model += production[(component, machine)] >= min_production * factory_status[component, machine]
    model += production[(component, machine)] <= max_production * factory_status[component, machine]


model.solve()
print(pulp.LpStatus[model.status])

output = []
for component, machine in production:
    var_output = {
        'Component': component,
        'Machine': machine,
        'Production': production[(component, machine)].varValue,
        'Machine Status': factory_status[(component, machine)].varValue
    }
    output.append(var_output)
    #print(output)
output_df = pd.DataFrame.from_records(output).sort_values(['Component', 'Machine'])
output_df.set_index(['Component', 'Machine'], inplace=True)
print(output_df)
output_df.to_csv('OUTPUT.csv')

# Print our objective function value (Total Costs)
print (pulp.value(model.objective))

The first thing to do is to get rid of production[(component, 'IP01')]+production[(component, 'IP02')]+production[(component, 'IP03')]+production[(component, 'IP04')]+production[(component, 'IP05')] .首先要做的就是去掉production[(component, 'IP01')]+production[(component, 'IP02')]+production[(component, 'IP03')]+production[(component, 'IP04')]+production[(component, 'IP05')] I hope you realize how bad this type of hardcoding is.我希望你意识到这种硬编码有多么糟糕。 This only works if you only have machines IP01..IP05.这仅适用于只有机器 IP01..IP05 的情况。 Indeed for the example data, this is not the case.事实上,对于示例数据,情况并非如此。 You need to make this data-driven.你需要使这个数据驱动。 We have a sum construct for that.我们有一个sum结构。

To model shortage, the rhs of the supply=demand constraint needs to become对于 model 短缺,供需约束的 rhs 需要变为

production == demand - shortage生产 == 需求 - 短缺

where shortage is an additional nonnegative variable.其中shortage是一个额外的非负变量。 You also need to add a cost term to the objective for this variable.您还需要为此变量的目标添加成本项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM