-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopt.py
More file actions
53 lines (46 loc) · 2.16 KB
/
opt.py
File metadata and controls
53 lines (46 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import apache_beam as beam
def solve(element):
from ortools.linear_solver import pywraplp
A = element['A']
req = element['req']
store = element['store']
solver = pywraplp.Solver('prob', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
x = {i_pack: solver.IntVar(0, solver.infinity(), 'pack_{0}'.format(i_pack)) for i_pack, ai in enumerate(A)}
y = {j_sku: solver.Sum([A[i_pack][j_sku] * x[i_pack] for i_pack in range(len(A))]) for j_sku in range(len(A[0]))}
abs_diff = {}
for j_sku in y:
abs_diff[j_sku] = solver.NumVar(0, solver.infinity(), 'diff_{0}'.format(j_sku))
diff = req[j_sku] - y[j_sku]
solver.Add(-abs_diff[j_sku] <= diff)
solver.Add(diff <= abs_diff[j_sku])
solver.Minimize(solver.Sum(abs_diff.values()) + 0.1 * solver.Sum(x.values()))
status = solver.Solve()
if status == solver.OPTIMAL:
print('optimal_{0}'.format(store))
else:
print('error')
sol = {i: x[i].solution_value() for i in x}
sol.update({'store': store})
return sol
def main():
A = [[10, 10, 0], [0, 10, 10], [5, 10, 5], [2, 0, 0], [0, 2, 0], [0, 0, 2]]
np.random.seed(1)
R = np.random.randint(0, 50, (100, 3))
inputs = [{'store': store, 'req': req.tolist(), 'A': A} for store, req in enumerate(R)]
options = beam.options.pipeline_options.PipelineOptions()
setup_options = options.view_as(beam.options.pipeline_options.SetupOptions)
setup_options.setup_file = './setup.py'
gcloud_options = options.view_as(beam.options.pipeline_options.GoogleCloudOptions)
gcloud_options.job_name = "opt"
gcloud_options.project = "shibuya-dataflow"
gcloud_options.staging_location = "gs://shibuya-dataflow-dataflow/staging"
gcloud_options.temp_location = "gs://shibuya-dataflow-dataflow/temp"
options.view_as(beam.options.pipeline_options.StandardOptions).runner = "DataflowRunner"
p = beam.Pipeline(options=options)
(p | "Read" >> beam.Create(inputs)
| "Solve" >> beam.Map(solve)
| "Write" >> beam.io.WriteToText("results/simple/opt.txt"))#("gs://shibuya-dataflow-dataflow/results/simple.txt"))
p.run()
if __name__ == '__main__':
main()