2018-02-19 15:21:05 +01:00
|
|
|
# Copyright 2011 Hakan Kjellerstrand hakank@gmail.com
|
2011-04-04 17:22:06 +00:00
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
Production planning problem in Google or-tools.
|
|
|
|
|
|
|
|
|
|
From the OPL model production.mod.
|
2013-12-24 11:35:01 +00:00
|
|
|
|
2018-02-19 15:21:05 +01:00
|
|
|
This model was created by Hakan Kjellerstrand (hakank@gmail.com)
|
2014-05-22 20:13:16 +00:00
|
|
|
Also see my other Google CP Solver models:
|
|
|
|
|
http://www.hakank.org/google_or_tools/
|
2011-04-04 17:22:06 +00:00
|
|
|
"""
|
2016-01-14 23:10:25 +01:00
|
|
|
from __future__ import print_function
|
2011-04-04 17:22:06 +00:00
|
|
|
import sys
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.linear_solver import pywraplp
|
2011-04-04 17:22:06 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
|
2016-03-19 18:54:18 +01:00
|
|
|
def main(sol='CBC'):
|
2013-12-24 11:35:01 +00:00
|
|
|
|
2011-04-04 17:22:06 +00:00
|
|
|
# Create the solver.
|
|
|
|
|
|
|
|
|
|
# using GLPK
|
|
|
|
|
if sol == 'GLPK':
|
|
|
|
|
solver = pywraplp.Solver('CoinsGridGLPK',
|
|
|
|
|
pywraplp.Solver.GLPK_LINEAR_PROGRAMMING)
|
|
|
|
|
else:
|
|
|
|
|
# Using CLP
|
|
|
|
|
solver = pywraplp.Solver('CoinsGridCLP',
|
|
|
|
|
pywraplp.Solver.CLP_LINEAR_PROGRAMMING)
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# data
|
|
|
|
|
#
|
|
|
|
|
kluski = 0
|
|
|
|
|
capellini = 1
|
|
|
|
|
fettucine = 2
|
|
|
|
|
products = ['kluski', 'capellini', 'fettucine']
|
|
|
|
|
num_products = len(products)
|
|
|
|
|
|
|
|
|
|
flour = 0
|
|
|
|
|
eggs = 1
|
|
|
|
|
resources = ['flour', 'eggs']
|
|
|
|
|
num_resources = len(resources)
|
|
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
consumption = [[0.5, 0.2], [0.4, 0.4], [0.3, 0.6]]
|
|
|
|
|
capacity = [20, 40]
|
|
|
|
|
demand = [100, 200, 300]
|
|
|
|
|
inside_cost = [0.6, 0.8, 0.3]
|
2011-04-04 17:22:06 +00:00
|
|
|
outside_cost = [0.8, 0.9, 0.4]
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# declare variables
|
|
|
|
|
#
|
2018-06-11 11:51:18 +02:00
|
|
|
inside = [
|
|
|
|
|
solver.NumVar(0, 10000, 'inside[%i]' % p) for p in range(num_products)
|
|
|
|
|
]
|
|
|
|
|
outside = [
|
|
|
|
|
solver.NumVar(0, 10000, 'outside[%i]' % p) for p in range(num_products)
|
|
|
|
|
]
|
2011-04-04 17:22:06 +00:00
|
|
|
|
|
|
|
|
# to minimize
|
2018-06-11 11:51:18 +02:00
|
|
|
z = solver.Sum([
|
|
|
|
|
inside_cost[p] * inside[p] + outside_cost[p] * outside[p]
|
|
|
|
|
for p in range(num_products)
|
|
|
|
|
])
|
2011-04-04 17:22:06 +00:00
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# constraints
|
|
|
|
|
#
|
|
|
|
|
for r in range(num_resources):
|
2018-06-11 11:51:18 +02:00
|
|
|
solver.Add(
|
|
|
|
|
solver.Sum([consumption[p][r] * inside[p]
|
|
|
|
|
for p in range(num_products)]) <= capacity[r])
|
2011-04-04 17:22:06 +00:00
|
|
|
|
|
|
|
|
for p in range(num_products):
|
2014-05-22 20:13:16 +00:00
|
|
|
solver.Add(inside[p] + outside[p] >= demand[p])
|
2011-04-04 17:22:06 +00:00
|
|
|
|
|
|
|
|
objective = solver.Minimize(z)
|
|
|
|
|
|
|
|
|
|
solver.Solve()
|
|
|
|
|
|
2016-01-14 23:10:25 +01:00
|
|
|
print()
|
|
|
|
|
print('z = ', solver.Objective().Value())
|
2011-04-04 17:22:06 +00:00
|
|
|
|
|
|
|
|
for p in range(num_products):
|
2018-06-11 11:51:18 +02:00
|
|
|
print(
|
|
|
|
|
products[p],
|
|
|
|
|
': inside:',
|
|
|
|
|
inside[p].SolutionValue(),
|
|
|
|
|
'(ReducedCost:',
|
|
|
|
|
inside[p].ReducedCost(),
|
|
|
|
|
')',
|
|
|
|
|
end=' ')
|
|
|
|
|
print('outside:', outside[p].SolutionValue(), ' (ReducedCost:',
|
|
|
|
|
outside[p].ReducedCost(), ')')
|
2016-01-14 23:10:25 +01:00
|
|
|
print()
|
2011-04-04 17:22:06 +00:00
|
|
|
|
2013-12-24 11:35:01 +00:00
|
|
|
|
2011-04-04 17:22:06 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
2014-07-23 19:09:05 +00:00
|
|
|
sol = 'CBC'
|
2011-04-04 17:22:06 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
sol = sys.argv[1]
|
|
|
|
|
if sol != 'GLPK' and sol != 'CBC':
|
2016-01-14 23:10:25 +01:00
|
|
|
print('Solver must be either GLPK or CBC')
|
2011-04-04 17:22:06 +00:00
|
|
|
sys.exit(1)
|
2013-12-24 11:35:01 +00:00
|
|
|
|
2011-04-04 17:22:06 +00:00
|
|
|
main(sol)
|