From 0b89f2e80578121d8648a0135222816580c26be0 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 4 Aug 2010 14:12:37 -0400 Subject: [PATCH] Added the linear program solving the midatlantic region. --- bin/linear_programs/midatl | 118 +++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 bin/linear_programs/midatl diff --git a/bin/linear_programs/midatl b/bin/linear_programs/midatl new file mode 100644 index 0000000..246e34b --- /dev/null +++ b/bin/linear_programs/midatl @@ -0,0 +1,118 @@ +#!/usr/bin/env python + +""" +Solve an example problem with lp_solve. +""" + +from math import acos, sin, cos +import os +import site +import sys + +# Basically, add '../src' to our path. +# Needed for the imports that follow. +site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../../src') + +import LinearProgramming + + +class Facility(object): + + def __init__(self, fac_id, fac_capacity, fac_latitude, fac_longitude): + self.id = fac_id + self.capacity = fac_capacity + self.latitude = fac_latitude + self.longitude = fac_longitude + + def distance(self, other_facility): + return 3963.0 * acos(sin(self.latitude/57.2958) * sin(other_facility.latitude/57.2958) + cos(self.latitude/57.2958) * cos(other_facility.latitude/57.2958) * cos(other_facility.longitude/57.2958 - self.longitude/57.2958)) + + +class Consumer(Facility): + def __init__(self, fac_id, fac_capacity, fac_latitude, fac_longitude): + super(Consumer, self).__init__(fac_id, + fac_capacity, + fac_latitude, + fac_longitude) + +class Producer(Facility): + def __init__(self, fac_id, fac_capacity, fac_latitude, fac_longitude): + super(Producer, self).__init__(fac_id, + fac_capacity, + fac_latitude, + fac_longitude) + +f = open('midatl.csv', 'r') +f.readline() # Skip the header + +producers = [] +consumers = [] + +for line in f: + row = line.split(',') + fac_id = int(row[0]) + fac_type = row[1] + fac_capacity = float(row[2]) + fac_latitude = float(row[3]) + fac_longitude = float(row[4]) + + if fac_type == '"D"': + c = Consumer(fac_id, fac_capacity, fac_latitude, fac_longitude) + consumers.append(c) + else: + p = Producer(fac_id, fac_capacity, fac_latitude, fac_longitude) + producers.append(p) + + + +lp = LinearProgramming.LinearProgram() + +# Loop through the consumer/producer pairs, calculating costs +# (distances) and adding them to the array of objective function +# coefficients. +lp.objective_coefficients = [] + +for c_idx in range(0, len(consumers)): + for p_idx in range(0, len(producers)): + distance = consumers[c_idx].distance(producers[p_idx]) + lp.objective_coefficients.append(distance) + + +num_cols = len(lp.objective_coefficients) + +lp.constraint_matrix = [] +lp.inequalities = [] +lp.rhs = [] + +for c_idx in range(0, len(consumers)): + demand = consumers[c_idx].capacity + lp.rhs.append(demand) + + lp.inequalities.append(LinearProgramming.GE) + + demand_row = [0]*num_cols + offset = c_idx * len(producers) + for idx in range(0, len(producers)): + demand_row[offset+idx] = 1 + + lp.constraint_matrix.append(demand_row) + +for p_idx in range(0, len(producers)): + supply = producers[p_idx].capacity + lp.rhs.append(supply) + + lp.inequalities.append(LinearProgramming.LE) + + supply_row = [0]*num_cols + + for idx in range(0,len(consumers)): + offset = p_idx + len(producers)*idx + supply_row[offset] = 1 + lp.constraint_matrix.append(supply_row) + +lp.type = LinearProgramming.MINIMIZE + +[v,x,duals] = lp.solve() + +print 'Optimal objective function value: ', v +print 'Optimal solution vector: ', x -- 2.43.2