Exemple #1
0
	def generate_constraints(self, **kwargs):
		r_max = kwargs['r_max']
		max_cap = kwargs['max_cap']
		candidate_stops = kwargs['candidate_stops']
		demand_centroids = kwargs['demand_centroids']
		demands = kwargs['demands']
		n_buses = kwargs['n_buses']

		n_candidate_stops = len(candidate_stops)
		n_demands = len(demands)

		costs = []

		for i in range(n_demands + n_candidate_stops):
			costs.append([])

			for j in range(n_demands + n_candidate_stops):
				org = demand_centroids[i] if i < n_demands else candidate_stops[i-n_demands]
				des = demand_centroids[j] if j < n_demands else candidate_stops[j-n_demands]

				if i < n_demands or j < n_demands:
					costs[i].append(walktime(org, des))
				else:
					costs[i].append(ridetime(i - n_demands, j - n_demands))

		def it(spec):
			lengths = {'d': n_demands, 's': n_candidate_stops, 'n': n_demands+n_candidate_stops, 'r': r_max}

			if len(spec) == 1:
				return enumerate(range(lengths[spec]))

			return enumerate(product(*[range(lengths[x]) for x in spec]))

		vars = self.vars

		cs = []
		cns = []

		#///////////////////////////////////////////////////////////////////////
		#          Origin destination shortest path problem (ODSSP)             
		#///////////////////////////////////////////////////////////////////////

		for idx, (o, d) in it('dd'):
			if o != d:
				cns.append('A1(%d)' % idx)
				cs.append((
					linex([(vars.x[o][d][o][j], 1)
						   for j in range(n_demands + n_candidate_stops)
						   if j != o]),
					'E', 1, 0))
			else:
				cns.append('A1(%d)' % idx)
				cs.append((
					linex([(vars.x[o][d][o][j], 1)
						   for j in range(n_demands + n_candidate_stops)]),
					'E', 0, 0))

		for idx, (o, d) in it('dd'):
			if o != d:
				cns.append('A2(%d)' % idx)
				cs.append((
					linex([(vars.x[o][d][i][d], 1)
						   for i in range(n_demands + n_candidate_stops)
						   if i != d]),
					'E', 1, 0))
			else:
				cns.append('A2(%d)' % idx)
				cs.append((
					linex([(vars.x[o][d][i][d], 1)
						   for i in range(n_demands + n_candidate_stops)]),
					'E', 0, 0))


		for idx, (o, d, i) in it('ddn'):
			if o != d:
				rhs = 0

				if i == o:
					rhs = 1
				elif i == d:
					rhs = -1

				cns.append('A3(%d)' % idx)
				cs.append((
					linex([(vars.x[o][d][i][j], 1)
						   for j in range(n_demands + n_candidate_stops)
						   if j != i] + 
						  [(vars.x[o][d][j][i], -1)
						   for j in range(n_demands + n_candidate_stops)
						   if j != i]),
					'E', rhs, 0))

		for idx, (o,d,j) in it('dds'):
			cns.append('A4(%d)' % idx)
			cs.append((
				linex([(vars.x[o][d][i][j+n_demands], 1)
					   for i in range(n_demands)] +
					  [(vars.x[o][d][j+n_demands][i], 1)
					   for i in range(n_demands)]),
				'L', 1, 0))

		for idx, (o,d) in it('dd'):
			if o != d:
				cns.append('A5(%d)' % idx)
				cs.append((
					linex([(vars.w[o][d], 1)] + 
						  [(vars.x[o][d][i][j], -costs[i][j])
						   for i in range(n_demands + n_candidate_stops)
						   for j in range(n_demands + n_candidate_stops)
						   if i == o or j == d]),
					'E', 0, 0))

		for idx, (o,d) in it('dd'):
			if o != d:
				cns.append('A6(%d)' % idx)
				cs.append((
					linex([(vars.r[o][d], 1)] + 
						  [(vars.x[o][d][i+n_demands][j+n_demands], -costs[i+n_demands][j+n_demands])
						   for i in range(n_candidate_stops)
						   for j in range(n_candidate_stops)]),
					'E', 0, 0))

		for idx, (o,d,j) in it('dds'):
			if costs[o][j+n_demands] > 5:
				cns.append('A7(%d)' % idx)
				cs.append((linex([(vars.x[o][d][o][j+n_demands], 1)]), 'E', 0, 0))

		for idx, (o,d,i) in it('dds'):
			if costs[i+n_demands][d] > 5:
				cns.append('A8(%d)' % idx)
				cs.append((linex([(vars.x[o][d][i+n_demands][d], 1)]), 'E', 0, 0))

		for idx, (o,d,i,j) in it('ddnd'):
			if j != d:
				cns.append('A9(%d)' % idx)
				cs.append((linex([(vars.x[o][d][i][j], 1)]), 'E', 0, 0))

		for idx, (o,d,i,j) in it('dddn'):
			if i != o:
				cns.append('A10(%d)' % idx)
				cs.append((linex([(vars.x[o][d][i][j], 1)]), 'E', 0, 0))

		for idx, (o,d) in it('dd'):
			if o != d:
				if costs[o][d] == 0:
					f = 1
				else:
					f = 1.0/costs[o][d]

				cns.append(('A11-1(%d)' % idx))
				cs.append((
					linex([(vars.s[o][d], 1),
						   (vars.r[o][d], 1-f),
						   (vars.w[o][d], 1-f)]),
					'E', costs[o][d]*(1-f), 0))

				cns.append(('A12(%d)' % idx))
				cs.append((
					linex([(vars.r[o][d], 1),
						   (vars.w[o][d], 1)]),
					'L', costs[o][d], 0))
			else:
				cns.append(('A11-2(%d)' % idx))
				cs.append((linex([(vars.s[o][d], 1)]),'E', 0, 0))



		#///////////////////////////////////////////////////////////////////////
		#            Transit route network design problem (TRNDP)
		#///////////////////////////////////////////////////////////////////////

		for idx, (r,i,j) in it('rss'):
			cns.append('B1(%d)' % idx)
			cs.append((
				linex([(vars.rx0[r][i][j], 1),
					   (vars.c[r], 1)]),
				'L', 1, 0))

		for idx, (i,j) in it('ss'):
			cns.append('B2(%d)' % idx)
			cs.append((
				linex([(vars.rx[r][i][j], 1)
					   for r in range(r_max)] + 
					  [(vars.rx0[r][i][j], 1)
					   for r in range(r_max)] +
					  [(vars.x[o][d][i+n_demands][j+n_demands], -1.0/n_demands/n_demands)
					   for o in range(n_demands)
					   for d in range(n_demands)]),
				'G', 0, 0))

		for idx, (r) in it('r'):
			cns.append('B3(%d)' % idx)
			cs.append((
				linex([(vars.rx[r][n_candidate_stops][j], 1)
					   for j in range(n_candidate_stops+1)]),
				'E', 1, 0))

		for idx, (r) in it('r'):
			cns.append('B4(%d)' % idx)
			cs.append((
				linex([(vars.rx[r][i][n_candidate_stops], 1)
					   for i in range(n_candidate_stops+1)] +
					  [(vars.rx[r][i][n_candidate_stops+1], 1)
					   for i in range(n_candidate_stops)]),
				'E', 1, 0))

		for idx, (r,i) in it('rs'):
			cns.append('B5(%d)' % idx)
			cs.append((
				linex([(vars.rx[r][i][j],  1)
					   for j in range(n_candidate_stops+2)
					   if i != j] +
					  [(vars.rx[r][j][i], -1)
					   for j in range(n_candidate_stops+2)
					   if i != j]),
				'E', 0, 0))

		for idx, (r,i) in it('rs'):
			cns.append('B6(%d)' % idx)
			cs.append((
				linex([(vars.rx[r][j][i], 1)
					   for j in range(n_candidate_stops+2)]),
				'L', 1, 0))

		for idx, (r,i) in it('rs'):
			cns.append('B7(%d)' % idx)
			cs.append((
				linex([(vars.rx[r][i][j], 1)
					   for j in range(n_candidate_stops+2)]),
				'L', 1, 0))

		for idx, (r,i) in it('rs'):
			cns.append('B8(%d)' % idx)
			cs.append((linex([(vars.rx[r][i][i], 1)]), 'E', 0, 0))

		for idx, (r) in it('r'):
			cns.append('B9(%d)' % r)
			cs.append((linex([(vars.rx[r][n_candidate_stops][n_candidate_stops+1], 1)]), 'E', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('B10(%d)' % idx)
				cs.append((
					linex([(vars.rq[r][i],       1),
						   (vars.rq[r][j],      -1),
						   (vars.rx[r][i][j], 1000)]),
					'L', 999, 0))


		for idx, (r,i) in it('rs'):
			if i != 0:
				cns.append('B11-1(%d)' % idx)
				cs.append((
					linex([(vars.rx[r][i][n_candidate_stops], 1)] +
						  [(vars.rx[r][k][n_candidate_stops], 1)
						    for k in range(n_candidate_stops+1)
						    if k < i]),
					'L', 1, 0))

		for idx, (r,i) in it('rs'):
			if i != 0:
				cns.append('B11-2(%d)' % idx)
				cs.append((
					linex([(vars.rx[r][i][n_candidate_stops],  1)] +
						  [(vars.rx[r][j][i],                 -1)
						   for j in range(n_candidate_stops+1)]),
					'L', 0, 0))

		for idx, (r,i) in it('rs'):
			if i != 0:
				cns.append('B11-3(%d)' % idx)
				cs.append((
					linex([(vars.rx[r][i][n_candidate_stops], 1),
						   (vars.c[r],                        1)]),
					'L', 1, 0))

		for idx, (r,i) in it('rs'):
			if i != 0:
				cns.append('B11-4(%d)' % idx)
				cs.append((
					linex([(vars.rx[r][i][n_candidate_stops],  1),
						   (vars.c[r],                         1)] +
						  [(vars.rx[r][k][n_candidate_stops],  1)
						    for k in range(n_candidate_stops+1)
						    if k < i] +
						  [(vars.rx[r][j][i],                 -1)
						   for j in range(n_candidate_stops+1)]),
					'G', 0, 0))

		for idx, (r,i,j) in it('rss'):
			cns.append('B12-1(%d)' % idx)
			cs.append((
				linex([(vars.rx0[r][i][j],                 1),
					   (vars.rx[r][i][n_candidate_stops], -1),
					   (vars.rx[r][n_candidate_stops][j], -1)]),
				'G', -1, 0))

		for idx, (r,i,j) in it('rss'):
			cns.append('B12-2(%d)' % idx)
			cs.append((
				linex([(vars.rx0[r][i][j],                 1),
					   (vars.rx[r][i][n_candidate_stops], -1)]),
				'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			cns.append('B12-3(%d)' % idx)
			cs.append((
				linex([(vars.rx0[r][i][j],                 1),
					   (vars.rx[r][n_candidate_stops][j], -1)]),
				'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			cns.append('B13(%d)' % idx)
			cs.append((
				linex([(vars.rx[r][i][j],  1),
					   (vars.rx0[r][i][j], 1)]),
				'L', 1, 0))

		#///////////////////////////////////////////////////////////////////////
		#                Frequency determination problem (FDP)
		#///////////////////////////////////////////////////////////////////////

		for idx, (r) in it('r'):
			cns.append('C1(%d)' % r)
			cs.append((
				linex([(vars.c[r], 1)] +
					  [(vars.rx[r][i][n_candidate_stops+1], -1)
					   for i in range(n_candidate_stops)]),
				'E', 0, 0))

		for idx, (i,j) in it('ss'):
			if i != j:
				cns.append('C3-1(%d)' % idx)
				cs.append((
					linex([(vars.x[o][d][i+n_demands][j+n_demands], demands[o][d])
						   for o in range(n_demands)
						   for d in range(n_demands)] + 
						  [(vars.z1[r][i][j], -max_cap)
						   for r in range(r_max)] + 
						  [(vars.z3[r][i][j], -max_cap)
						   for r in range(r_max)]),
					'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C3-2(%d)' % idx)
				cs.append((
					linex([(vars.z1[r][i][j],   1),
						   (vars.rx[r][i][j], -60)]),
					'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C3-3(%d)' % idx)
				cs.append((
					linex([(vars.z1[r][i][j],  1),
						   (vars.f[r],        -1)]),
					'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C3-4(%d)' % idx)
				cs.append((
					linex([(vars.z1[r][i][j],   1),
						   (vars.f[r],         -1),
						   (vars.rx[r][i][j], -60)]),
					'G', -60, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C3-5(%d)' % idx)
				cs.append((
					linex([(vars.z3[r][i][j],   1),
						   (vars.rx0[r][i][j], -60)]),
					'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C3-6(%d)' % idx)
				cs.append((
					linex([(vars.z3[r][i][j],  1),
						   (vars.f[r],        -1)]),
					'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C3-7(%d)' % idx)
				cs.append((
					linex([(vars.z3[r][i][j],   1),
						   (vars.f[r],         -1),
						   (vars.rx0[r][i][j], -60)]),
					'G', -60, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C3-8(%d)' % idx)
				cs.append((
					linex([(vars.z3[r][i][j], 1),
						   (vars.c[r],        1)]),
					'L', 1, 0))

		cns.append('C4-1')
		cs.append((
			linex([(vars.z1[r][i][j], 1)
				   for r in range(r_max)
				   for i in range(n_candidate_stops)
				   for j in range(n_candidate_stops)
				   if i != j] + 
				  [(vars.z2[r][i][j], 1)
				   for r in range(r_max)
				   for i in range(n_candidate_stops)
				   for j in range(n_candidate_stops)
				   if i != j]),
			'L', n_buses, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C4-2(%d)' % idx)
				cs.append((
					linex([(vars.z2[r][i][j],   1),
						   (vars.rx[r][i][j], -60)]),
					'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C4-3(%d)' % idx)
				cs.append((
					linex([(vars.z2[r][i][j],   1),
						   (vars.c[r],        -60)]),
					'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C4-4(%d)' % idx)
				cs.append((
					linex([(vars.z2[r][i][j],  1),
						   (vars.f[r],        -1)]),
					'L', 0, 0))

		for idx, (r,i,j) in it('rss'):
			if i != j:
				cns.append('C4-5(%d)' % idx)
				cs.append((
					linex([(vars.z2[r][i][j],   1),
						   (vars.f[r],         -1),
						   (vars.rx[r][i][j], -60),
						   (vars.c[r],        -60)]),
					'G', -120, 0))

		for idx, (r) in it('r'):
			cns.append('C5(%d)'  % idx)
			cs.append((linex([(vars.f[r], 1)]), 'G', 0.2, 0))

		cs = zip(*cs)

		self._cp.linear_constraints.add(
            lin_expr=cs[0],
            senses=cs[1],
            rhs=cs[2],
            range_values=cs[3])

		self._cp.linear_constraints.set_names(enumerate(cns))
Exemple #2
0
    def generate_constraints(self):
        r_max = 1

        candidate_stops = [  #[55.785470, 12.523041],
            [55.786239, 12.522234], [55.786541, 12.526961],
            [55.789277, 12.523946], [55.786431, 12.521428],
            [55.785898, 12.520618], [55.785138, 12.520728],
            [55.784637, 12.520459], [55.782099, 12.514132],
            [55.782460, 12.519244], [55.782226, 12.520035]
        ]
        demand_centroids = [[55.786344, 12.524316], [55.786344, 12.524316],
                            [55.786872, 12.5259], [55.78944, 12.525146],
                            [55.786745, 12.520469], [55.787486, 12.518472],
                            [55.787911, 12.518621], [55.785438, 12.519377],
                            [55.785154, 12.520434], [55.785241, 12.518699],
                            [55.783, 12.512931], [55.781929, 12.521461]]
        demands = [[0, 100, 3, 36, 0, 14, 13, 19, 42, 55, 5, 20],
                   [190, 0, 2, 25, 0, 18, 6, 8, 40, 37, 5, 4],
                   [13, 10, 0, 3, 0, 3, 0, 1, 5, 3, 0, 0],
                   [137, 1, 0, 0, 0, 10, 4, 1, 17, 34, 1, 1],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [47, 9, 2, 4, 0, 0, 63, 0, 0, 6, 2, 0],
                   [46, 4, 0, 0, 0, 173, 0, 0, 0, 2, 2, 1],
                   [61, 2, 0, 1, 0, 1, 0, 0, 78, 116, 0, 0],
                   [116, 13, 1, 5, 0, 8, 0, 52, 0, 84, 16, 8],
                   [167, 6, 0, 6, 0, 33, 13, 52, 230, 0, 23, 6],
                   [1, 1, 0, 0, 0, 1, 0, 0, 7, 1, 0, 1],
                   [97, 55, 0, 6, 0, 3, 2, 2, 47, 39, 9, 0]]

        n_candidate_stops = len(candidate_stops)
        n_buses = 20

        costs = []

        for i1, (stop_x1, stop_y1) in enumerate([[0, 0]] + candidate_stops):
            costs.append([])

            for i2, (stop_x2,
                     stop_y2) in enumerate([[0, 0]] + candidate_stops):
                costs[i1].append(
                    275 * (abs(stop_x1 - stop_x2) + abs(stop_y1 - stop_y2)))

        vars = self.vars

        cs = []
        cns = []

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops + 1):
                for j in range(n_candidate_stops + 1):
                    cns.append('XF1(%d)' % idx)
                    cs.append((linex([(vars.xf[r][i][j], 1),
                                      (vars.f[r], -1)]), 'L', 0, 0))

                    cns.append('XF2(%d)' % idx)
                    cs.append((linex([(vars.xf[r][i][j], 1),
                                      (vars.x[r][i][j], -60)]), 'L', 0, 0))

                    cns.append('XF3(%d)' % idx)
                    cs.append((linex([(vars.xf[r][i][j], 1), (vars.f[r], -1),
                                      (vars.x[r][i][j], -60)]), 'G', -60, 0))

                    idx += 1

        for r in range(r_max):
            cns.append('C1(%d)' % r)
            cs.append(
                (linex([(vars.x[r][0][j], 1)
                        for j in range(n_candidate_stops + 1)]), 'E', 1, 0))

        for r in range(r_max):
            cns.append('C2(%d)' % r)
            cs.append(
                (linex([(vars.x[r][i][0], 1)
                        for i in range(n_candidate_stops + 1)]), 'L', 1, 0))

        idx = 0
        for r in range(r_max):
            for j in range(n_candidate_stops):
                cns.append('C3(%d)' % idx)
                cs.append((linex(
                    [(vars.x[r][i][j + 1], 1)
                     for i in range(n_candidate_stops + 1) if i != (j + 1)] +
                    [(vars.x[r][j + 1][i], -1)
                     for i in range(n_candidate_stops + 2) if i != (j + 1)]),
                           'E', 0, 0))

                idx += 1

        idx = 0
        for r in range(r_max):
            for j in range(n_candidate_stops + 1):
                cns.append('C4(%d)' % idx)
                cs.append((linex([(vars.x[r][i][j + 1], 1)
                                  for i in range(n_candidate_stops + 1)
                                  if i != (j + 1)]), 'L', 1, 0))

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                cns.append('C5(%d)' % idx)
                cs.append((linex([(vars.x[r][i + 1][j], 1)
                                  for j in range(n_candidate_stops + 1)
                                  if (i + 1) != j]), 'L', 1, 0))

        idx = 0
        for r in range(r_max):
            cns.append('C7a(%d)' % idx)
            cs.append((linex([(vars.x[r][i + 1][n_candidate_stops + 1], 1)
                              for i in range(n_candidate_stops)]), 'L', 1, 0))

            cns.append('C7b(%d)' % idx)
            cs.append(
                (linex([(vars.x[r][i + 1][n_candidate_stops + 1], 1)
                        for i in range(n_candidate_stops)] +
                       [(vars.x[r][i][0], 1)
                        for i in range(n_candidate_stops + 1)]), 'E', 1, 0))

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                cns.append('C6(%d)' % idx)
                cs.append((linex([(vars.x[r][i + 1][i + 1], 1)]), 'E', 0, 0))

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops + 1):
                for j in range(n_candidate_stops + 1):
                    cns.append('C7-1(%d)' % idx)
                    cs.append((linex([(vars.zxf[r][i][j], 1),
                                      (vars.xf[r][i][j], -1)]), 'L', 0, 0))

                    cns.append('C7-2(%d)' % idx)
                    cs.append((linex([(vars.zxf[r][i][j], 1),
                                      (vars.x[r][0][0], 60)]), 'L', 60, 0))

                    cns.append('C7-3(%d)' % idx)
                    cs.append((linex([(vars.zxf[r][i][j], 1),
                                      (vars.xf[r][i][j], -1),
                                      (vars.x[r][0][0], 60)]), 'G', 0, 0))

                    idx += 1

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops + 1):
                for j in range(n_candidate_stops + 1):
                    cns.append('zzxf1(%d)' % idx)
                    cs.append((linex([(vars.zzxf[r][i][j], 1),
                                      (vars.zxf[r][i][j], -1)]), 'L', 0, 0))

                    cns.append('zzxf2(%d)' % idx)
                    cs.append(
                        (linex([(vars.zzxf[r][i][j], 1)] +
                               [(vars.x[r][i + 1][n_candidate_stops + 1], -60)
                                for i in range(n_candidate_stops)]), 'L', 0,
                         0))

                    cns.append('zzxf3(%d)' % idx)
                    cs.append(
                        (linex([(vars.zzxf[r][i][j], 1), (vars.zxf[r][i][j],
                                                          -1)] +
                               [(vars.x[r][i + 1][n_candidate_stops + 1], -60)
                                for i in range(n_candidate_stops)]), 'G', -60,
                         0))

                    idx += 1

        cns.append('C8')
        cs.append(
            (linex([(vars.zxf[r][i + 1][j + 1], costs[i + 1][j + 1])
                    for r in range(r_max) for i in range(n_candidate_stops)
                    for j in range(n_candidate_stops)] +
                   [(vars.zzxf[r][i + 1][j + 1], costs[i + 1][j + 1])
                    for r in range(r_max) for i in range(n_candidate_stops)
                    for j in range(n_candidate_stops)]), 'L', n_buses, 0, 0))

        for r in range(r_max):
            cns.append('C9(%d)' % r)
            cs.append((
                linex(
                    [(vars.x[r][i + 1][j + 1], costs[i + 1][j + 1])
                     for i in range(n_candidate_stops)
                     for j in range(n_candidate_stops) if i != j] +
                    # [(vars.cxx[r][i][j], costs[i+1][j+1])
                    #  for i in range(n_candidate_stops)
                    #  for j in range(n_candidate_stops)
                    #  if i != j] +
                    [(vars.T[r], -1)]),
                'E',
                0,
                0))

        idx = 0
        for p in range(len(demands)):
            for o in range(len(candidate_stops)):
                if walktime(candidate_stops[o], demand_centroids[p]) >= 5:
                    for q in range(len(demands)):
                        for d in range(len(candidate_stops)):
                            cns.append('FILTER(%d)' % idx)
                            cs.append(
                                (linex([(vars.s[p][q][o][d], 1)]), 'E', 0, 0))

                            idx += 1

        for q in range(len(demands)):
            for d in range(len(candidate_stops)):
                if walktime(candidate_stops[d], demand_centroids[q]) >= 5:
                    for p in range(len(demands)):
                        for o in range(len(candidate_stops)):
                            cns.append('FILTER(%d)' % idx)
                            cs.append(
                                (linex([(vars.s[p][q][o][d], 1)]), 'E', 0, 0))

                            idx += 1

        idx = 0
        for p in range(len(demands)):
            for q in range(len(demands)):
                if demands[p][q] != 0:
                    cns.append('S2(%d)' % idx)
                    cs.append((linex([(vars.s[p][q][c1][c2], 1)
                                      for c1 in range(n_candidate_stops)
                                      for c2 in range(n_candidate_stops)]),
                               'E', 1, 0))

                    idx += 1
                else:
                    for c1 in range(n_candidate_stops):
                        for c2 in range(n_candidate_stops):
                            cns.append('S2N(%d)' % idx)
                            cs.append((linex([(vars.s[p][q][c1][c2], 1)]), 'E',
                                       0, 0))
                            idx += 1

                for c in range(n_candidate_stops):
                    cns.append('S2N2(%d)' % idx)
                    cs.append((linex([(vars.s[p][q][c][c], 1)]), 'E', 0, 0))
                    idx += 1

        # Sub tour elimination
        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                for j in range(n_candidate_stops):
                    if i != j:
                        cns.append('SUB(%d)' % idx)
                        cs.append((linex([
                            (vars.q[r][i], 1), (vars.q[r][j], -1),
                            (vars.x[r][i + 1][j + 1], 1000)
                        ]), 'L', 999, 0))

                        idx += 1

        idx = 0
        for r in range(r_max):
            for c2 in range(n_candidate_stops):
                cns.append("DEL(%d)" % idx)
                cs.append(
                    (linex([(vars.delta[r][c2], 1)] +
                           [(vars.x[r][i + 1][c2 + 1], -1)
                            for i in range(n_candidate_stops)]), 'E', 0, 0))

                idx += 1

        idx = 0
        for r in range(r_max):
            for c2 in range(n_candidate_stops):
                cns.append("DELF1(%d)" % idx)
                cs.append((linex([(vars.deltaf[r][c2], 1),
                                  (vars.f[r], -1)]), 'L', 0, 0))

                cns.append("DELF2(%d)" % idx)
                cs.append((linex([(vars.deltaf[r][c2], 1),
                                  (vars.delta[r][c2], -60)]), 'L', 0, 0))

                cns.append("DELF3(%d)" % idx)
                cs.append((linex([(vars.deltaf[r][c2], 1), (vars.f[r], -1),
                                  (vars.delta[r][c2], -60)]), 'G', -60, 0))

                idx += 1

        idx = 0
        for c in range(n_candidate_stops):
            cns.append("CAP(%d)" % idx)
            cs.append((linex([(vars.deltaf[r][c], 40) for r in range(r_max)] +
                             [(vars.s[p][q][c1][c], -demands[p][q] / 15.0)
                              for c1 in range(n_candidate_stops)
                              for p in range(len(demands))
                              for q in range(len(demands[0]))]), 'G', 0, 0))

            idx += 1

        ############################################################
        # Shortest path constraints
        ############################################################
        idx = 0
        for o in range(n_candidate_stops):
            for d in range(n_candidate_stops):
                if o != d:
                    cns.append('SP1(%d)' % idx)
                    cs.append((linex([(vars.t[o][d], 1)] +
                                     [(vars.xs[o][d][r][i][j], -ridecost(i, j))
                                      for r in range(r_max)
                                      for i in range(n_candidate_stops + 1)
                                      for j in range(n_candidate_stops + 2)]),
                               'E', 0, 0))
                else:
                    cns.append('SP1(%d)' % idx)
                    cs.append((linex([(vars.t[o][d], 1)]), 'E', 0, 0))

                idx += 1

        idx = 0
        for o in range(n_candidate_stops):
            for d in range(n_candidate_stops):
                for r in range(r_max):
                    for i in range(n_candidate_stops + 1):
                        for j in range(n_candidate_stops + 2):
                            if o != d:
                                cns.append('SP2(%d)' % idx)
                                cs.append((linex([(vars.xs[o][d][r][i][j], 1),
                                                  (vars.x[r][i][j], -1)]), 'L',
                                           0, 0))
                            else:
                                cns.append('SP2(%d)' % idx)
                                cs.append((linex([(vars.xs[o][d][r][i][j], 1)
                                                  ]), 'E', 0, 0))

                            idx += 1

        idx = 0
        for o in range(n_candidate_stops):
            for d in range(n_candidate_stops):
                if o != d:
                    for i in range(n_candidate_stops):
                        rhs = 0

                        if i == o:
                            rhs = 1
                        elif i == d:
                            rhs = -1

                        cns.append('SP3(%d)' % idx)
                        cs.append((linex([(vars.xs[o][d][r][i + 1][j], 1)
                                          for r in range(r_max)
                                          for j in range(n_candidate_stops + 2)
                                          if j != (i + 1)] +
                                         [(vars.xs[o][d][r][j][i + 1], -1)
                                          for r in range(r_max)
                                          for j in range(n_candidate_stops + 1)
                                          if j != (i + 1)]), 'E', rhs, 0))

                        idx += 1

        idx = 0
        for p in range(len(demands)):
            for q in range(len(demands)):
                if demands[p][q] != 0:
                    for o in range(n_candidate_stops):
                        for d in range(n_candidate_stops):
                            cns.append('ST1(%d)' % idx)
                            cs.append((linex([(vars.st[p][q][o][d], 1),
                                              (vars.s[p][q][o][d], -3 * 60)]),
                                       'L', 0, 0))

                            cns.append('ST2(%d)' % idx)
                            cs.append((linex([(vars.st[p][q][o][d], 1),
                                              (vars.t[o][d], -1)]), 'L', 0, 0))

                            cns.append('ST3(%d)' % idx)
                            cs.append((linex([(vars.st[p][q][o][d], 1),
                                              (vars.t[o][d], -1),
                                              (vars.s[p][q][o][d], -180)]),
                                       'G', -180, 0))

                            idx += 1

                else:
                    for o in range(n_candidate_stops):
                        for d in range(n_candidate_stops):
                            cns.append('STN(%d)' % idx)
                            cs.append(
                                (linex([(vars.st[p][q][o][d], 1)]), 'E', 0, 0))

                            idx += 1

        idx = 0
        for p in range(len(demands)):
            for q in range(len(demands)):
                for o in range(len(candidate_stops)):
                    for d in range(len(candidate_stops)):
                        cns.append('FILT2-1(%d)' % idx)
                        cs.append((linex([(vars.s[p][q][o][d], 1)] +
                                         [(vars.delta[r][o], -1)
                                          for r in range(r_max)]), 'L', 0, 0))
                        idx += 1

                        cns.append('FILT2-2(%d)' % idx)
                        cs.append((linex([(vars.s[p][q][o][d], 1)] +
                                         [(vars.delta[r][d], -1)
                                          for r in range(r_max)]), 'L', 0, 0))

                        idx += 1

        cs = zip(*cs)

        self._cp.linear_constraints.add(lin_expr=cs[0],
                                        senses=cs[1],
                                        rhs=cs[2],
                                        range_values=cs[3])

        self._cp.linear_constraints.set_names(enumerate(cns))
Exemple #3
0
    def varyMaxTT(self, maxtt=False, step=5):
        self.vars.solutionFound = False

        vars = self.vars

        cs = []
        cns = []

        directory_maxtt = os.path.join(self.base_directory, "maxtt")
        if not os.path.exists(directory_maxtt):
            os.makedirs(directory_maxtt)

        nm = self._cp.variables.get_names()

        solution_exists = True

        if not maxtt:
            maxtt = self.sc.plan_horizon / 2

        results = []
        cons_idx_first = self._cp.linear_constraints.get_num()

        # C17
        vars.solutionFound = False
        for x, g in enumerate(self.sc.groups):
            cns.append('C17(%d)' % x)
            cs.append((linex([(vars.tg[g.id], 1)] + (
                [(vars.to[g.id][k], 1)
                 for k in range(g.routes[0].n_buses)] if self.
                options['max_transfer_waiting_time_constraint'] else [])), 'L',
                       maxtt))

        cs = zip(*cs)
        self._cp.linear_constraints.add(lin_expr=cs[0],
                                        senses=cs[1],
                                        rhs=cs[2])

        self._cp.linear_constraints.set_names([(i + cons_idx_first, cn)
                                               for i, cn in enumerate(cns)])

        while solution_exists and maxtt > 0:
            try:
                directory = os.path.join(self.base_directory, "maxtt",
                                         "%d" % (maxtt, ))
                if not os.path.exists(directory):
                    os.makedirs(directory)

                self.solve(name=os.path.join(
                    self.base_directory, "maxtt", "%d" %
                    (maxtt, ), ('%s_%s_%s_maxtt%d' %
                                (self.timestamp, self.__class__.__name__,
                                 self.sc.name, maxtt))))
                results += [
                    (maxtt, g, self.sc.groups[g].size, tg, sum(to))
                    for g, (to,
                            tg) in enumerate(zip(self.vars.to, self.vars.tg))
                ]

                fig_schedule = viz.schedule(self.vars, self.sc)
                plot(fig_schedule,
                     image_filename="schedule",
                     image="png",
                     output_type="file",
                     image_width=888,
                     image_height=fig_schedule.layout.height)
                time.sleep(2)
                os.rename(
                    "/Users/kelvinlee/Downloads/schedule.png",
                    os.path.join(self.base_directory, "maxtt", str(maxtt),
                                 "schedule.png"))

                fig_trip = viz.trip(self.vars, self.sc)
                plot(fig_trip,
                     image_filename="trip",
                     image="png",
                     output_type="file",
                     image_width=888,
                     image_height=fig_trip.layout.height)
                time.sleep(2)
                os.rename(
                    "/Users/kelvinlee/Downloads/trip.png",
                    os.path.join(self.base_directory, "maxtt", str(maxtt),
                                 "trip.png"))

                # fig = viz.comprehensive(self.vars, self.sc)
                # plot(fig,
                #      filename=os.path.join(
                #          self.base_directory,
                #          "maxtt",
                #          ('%s_%s_%s_maxtt%d_viz.html' % (
                #               self.timestamp, self.__class__.__name__, self.sc.name, maxtt))),
                #      auto_open=False)

                maxtt -= step
                self._cp.linear_constraints.set_rhs(
                    zip(cns, [maxtt] * len(cns)))
            except Exception as e:
                print e
                solution_exists = False

        f = open(os.path.join(self.base_directory, "maxtt", "results.csv"),
                 'w+')
        f.write(('Max transfer time, Group, Demand, '
                 'Transfer time, Waiting time\n'))
        for row in results:
            f.write(','.join(map(str, list(row))) + '\n')
        f.close()
Exemple #4
0
    def generate_constraints(self):
        routes, groups = (self.sc.routes, self.sc.groups)
        omega, theta = (self.sc.omega, self.sc.theta)
        plan_horizon, M = (self.sc.plan_horizon, self.sc.M)

        vars = self.vars

        r = routes[0]

        cs = []
        cns = []

        # C1: First bus
        if r.first_bus is not None:
            cns.append('C1')
            cs.append((linex([(vars.d[0][0], 1)]), 'E', r.first_bus, 0))

        # C2: Last bus
        if r.last_bus is not None:
            cns.append('C2')
            cs.append(
                (linex([(vars.d[0][r.n_buses - 1], 1)]), 'E', r.last_bus, 0))

        # C3: Headway range
        for x, (r, s, k) in enumerate(routes.rsk(1)):
            if r.hw_max != r.hw_min or (r.hw_max == r.hw_min and s == 0):
                cns.append('C3(%d)' % x)
                cs.append((linex([(vars.d[s][k + 1], 1), (vars.d[s][k], -1)]),
                           'R', r.hw_max, r.hw_min - r.hw_max))

        # C?: Bus order
        for x, (r, s, k) in enumerate(routes.rsk(1)):
            cns.append('C?(%d)' % x)
            cs.append((linex([(vars.a[s][k], 1),
                              (vars.a[s][k + 1], -1)]), 'L', 0, 0))

        for x, (r, s, k) in enumerate(routes.rsk(1)):
            cns.append('C??(%d)' % x)
            cs.append((linex([(vars.d[s][k], 1),
                              (vars.d[s][k + 1], -1)]), 'L', 0, 0))

        # C4: Departure-arrival relationship
        for x, (r, s, k) in enumerate(routes.rsk()):
            cns.append('C4(%d)' % x)
            cs.append((linex([(vars.d[s][k], 1), (vars.a[s][k], -1),
                              (vars.h[s][k], -1)]), 'E', 0, 0))

        for x, (r, s, k) in enumerate(routes.rsk()):
            cns.append('Dwell(%d)' % x)
            cs.append((
                # linex([(vars.h[s][k], 1)]), 'R', r.dw_max, r.dw_min-r.dw_max))
                linex([(vars.h[s][k], 1)]),
                'E',
                1,
                0))

        # C5:
        for x, (r, s, k) in enumerate(routes.rsk(0, 1)):
            cns.append('C5(%d)' % x)
            cs.append((linex([(vars.a[s + 1][k], 1),
                              (vars.d[s][k], -1)]), 'E', theta[s], 0))

        # C6:
        for x, (g, r, _, o, k) in enumerate(groups.gp(1, True, False)):
            cns.append('C6(%d)' % x)
            cs.append((linex([(vars.d[r.stops.index(o)][k], 1),
                              (vars.p[g.id][k], -M)]), 'R', g.alpha, -M))

        # C72:
        for x, (g, r, k) in enumerate(groups.gp(0, True, True)):
            cns.append('C72(%d' % x)
            cs.append((linex([(vars.pt[g.id][k], 1),
                              (vars.p[g.id][k], -1)]), 'L', 0, 0))

        # At least one pt = 1
        for x, (g, r) in enumerate(groups.gp(0, False)):
            cns.append('CTest(%d' % x)
            cs.append((linex([(pt, 1) for pt in vars.pt[g.id]]), 'E', 1, 0))

        # # C7:
        # for x, (g, r, k) in enumerate(groups.gp(0, True)):
        # 	cns.append('C7(%d)' % x)
        # 	cs.append((
        # 		linex([(vars.p[g.id][k],     1),
        # 			   (vars.pt[g.id][k],   -M)] +
        # 			  ([(vars.p[g.id][k-1], -1)]
        # 			   if k != 0 else [])),
        # 		'R', 0, M))

        # C8:
        for x, (r, s, k) in enumerate(routes.rsk()):
            cns.append('C8(%d)' % x)
            cs.append(
                (linex([(vars.c[s][k], 1)] +
                       ([(vars.c[s - 1][k], -1)] if s != 0 else []) +
                       ([(vars.pt[g.id][k], -g.size)
                         for g in groups if g.origin == r.stops[s]]) +
                       ([(vars.pt[g.id][k], g.size)
                         for g in groups if g.destination == r.stops[s]])),
                 'E', 0, 0))

        # C9:
        for x, (r, s, k) in enumerate(routes.rsk(0)):
            cns.append('C9(%d)' % x)
            cs.append((linex([(vars.c[s][k], 1)]), 'L', 50, 0))

        for x, (g, r, k) in enumerate(groups.gp(0, True, True)):
            cns.append('C10_2(%d' % x)
            cs.append((linex([(vars.pta[g.id][k], 1),
                              (vars.pt[g.id][k], -M)]), 'L', 0, 0))

        for x, (g, r, _, d, k) in enumerate(groups.gp(0, True, False)):
            cns.append('C10_3(%d' % x)
            cs.append((linex([(vars.pta[g.id][k], 1),
                              (vars.a[r.stops.index(d)][k], -1)]), 'L', 0, 0))

        for x, (g, r, _, d, k) in enumerate(groups.gp(0, True, False)):
            cns.append('C10_4(%d' % x)
            cs.append((linex([(vars.pta[g.id][k], 1),
                              (vars.a[r.stops.index(d)][k], -1),
                              (vars.pt[g.id][k], -M)]), 'G', -M, 0))

        # for x, (g, r, o, _, k) in enumerate(groups.gp(0, True, False)):
        # 	cns.append('C12_1(%d)' % x)
        # 	cs.append((
        # 		linex([(vars.ptd[g.id][k],  		      1),
        # 			   (vars.pt[g.id][k],            -M)]),
        # 		'L', 0, 0))

        # for x, (g, r, o, _, k) in enumerate(groups.gp(0, True, False)):
        # 	cns.append('C12_2(%d)' % x)
        # 	cs.append((
        # 		linex([(vars.ptd[g.id][k],  		      1),
        # 			   (vars.d[r.stops.index(o)][k], -1)]),
        # 		'L', 0, 0))

        # for x, (g, r, o, _, k) in enumerate(groups.gp(0, True, False)):
        # 	cns.append('C12_3(%d)' % x)
        # 	cs.append((
        # 		linex([(vars.ptd[g.id][k],  		      1),
        # 			   (vars.d[r.stops.index(o)][k], -1),
        # 			   (vars.pt[g.id][k],            -M)]),
        # 		'G', -M, 0))

        # for x, (g, r) in enumerate(groups.gp(0, False)):
        # 	cns.append('C12_4(%d' % x)
        # 	cs.append((
        # 		linex([(ptd, 1) for ptd in vars.ptd[g.id]] +
        # 			  [(vars.w[g.id], -1)]),
        # 		'E', g.alpha, 0))

        for x, (g, r) in enumerate(groups.gp(0, False)):
            cns.append('CLast(%d' % x)
            cs.append(
                (linex([(pta, 1)
                        for pta in vars.pta[g.id]] + [(vars.t[g.id], -1)]),
                 'E', g.alpha, 0))

        for x, (g1, g2) in enumerate(combinations(groups, 2)):
            cns.append('CComb(%d)' % x)
            cs.append((linex([(vars.pt[g1.id][k], k)
                              for k in range(g1.routes[0].n_buses)] +
                             [(vars.pt[g2.id][k], -k)
                              for k in range(g2.routes[0].n_buses)]),
                       ('L' if g1.alpha < g2.alpha else 'G'), 0, 0))

        cs = zip(*cs)

        self._cp.linear_constraints.add(lin_expr=cs[0],
                                        senses=cs[1],
                                        rhs=cs[2],
                                        range_values=cs[3])

        self._cp.linear_constraints.set_names(enumerate(cns))
Exemple #5
0
    def generate_constraints(self):
        routes, groups = (self.sc.routes, self.sc.groups)
        omega, theta = (self.sc.omega, self.sc.theta)
        plan_horizon, M = (self.sc.plan_horizon, self.sc.M)

        vars = self.vars

        cs = []
        cns = []

        # C2: First bus
        for r in routes:
            if r.first_bus is not None:
                cns.append('C2(%d)' % r.id)
                cs.append(
                    (linex([(vars.d[r.id][0][0], 1)]), 'E', r.first_bus, 0))

        # C3: Last bus
        for r in routes:
            if r.last_bus is not None:
                cns.append('C3(%d)' % r.id)
                cs.append((linex([(vars.d[r.id][0][r.n_buses - 1], 1)]), 'E',
                           r.last_bus, 0))

        # C4: Headway range
        for x, (r, s, k) in enumerate(routes.rsk(1)):
            if r.hw_max != r.hw_min or (r.hw_max == r.hw_min and s == 0):
                cns.append('C4(%d)' % x)
                cs.append((linex([(vars.d[r.id][s][k + 1], 1),
                                  (vars.d[r.id][s][k], -1)]), 'R', r.hw_max,
                           r.hw_min - r.hw_max))

        # C5: Bus order
        for x, (r, s, k) in enumerate(routes.rsk(1)):
            cns.append('C5(%d)' % x)
            cs.append((linex([(vars.d[r.id][s][k], 1),
                              (vars.d[r.id][s][k + 1], -1)]), 'L', 0, 0))

        # C6: Departure-arrival relationship
        for x, (r, s, k) in enumerate(routes.rsk()):
            cns.append('C6(%d)' % x)
            cs.append(
                (linex([(vars.d[r.id][s][k], 1), (vars.a[r.id][s][k], -1),
                        (vars.h[r.id][s][k], -1)]), 'E', 0, 0))

        # C7: Dwell time range
        for x, (r, s, k) in enumerate(routes.rsk()):
            cns.append('C7(%d)' % x)
            cs.append((linex([(vars.h[r.id][s][k], 1)]), 'R', r.dw_max,
                       r.dw_min - r.dw_max))

        # C8/11
        for x, (r, s, k) in enumerate(routes.rsk(0, 1)):
            cns.append('C8(%d)' % x)
            cs.append((
                linex([(vars.a[r.id][s + 1][k], 1), (vars.d[r.id][s][k], -1)] +
                      [(vars.db[r.id][s][k][n], -theta[r.id][s][n])
                       for n in range(plan_horizon)]), 'E', 0, 0))

        # C9
        for x, (r, s, k) in enumerate(routes.rsk()):
            cns.append('C9(%d)' % x)
            cs.append((linex([(vars.d[r.id][s][k], 1)] +
                             [(vars.db[r.id][s][k][n], -n)
                              for n in range(plan_horizon)]), 'E', 0, 0))

        # C10
        for x, (r, s, k) in enumerate(routes.rsk()):
            cns.append('C10(%d)' % x)
            cs.append((linex([(vars.db[r.id][s][k][n], 1)
                              for n in range(plan_horizon)]), 'E', 1, 0))

        # C12
        for x, (r, rp, s, k, kp) in enumerate(routes.tf()):
            cns.append('C12(%d)' % x)
            cs.append((linex([(vars.d[rp.id][rp.stops.index(s)][kp], 1),
                              (vars.a[r.id][r.stops.index(s)][k], -1),
                              (vars.c[r.id][rp.id][s][k][kp], -M)]), 'R',
                       omega[r.id][s][rp.id], -M))

        # C13
        for x, (r, rp, s, k, kp) in enumerate(routes.tf()):
            cns.append('C13(%d)' % x)
            cs.append((linex([(vars.d[rp.id][rp.stops.index(s)][kp], 1),
                              (vars.a[r.id][r.stops.index(s)][k], -1),
                              (vars.t[r.id][rp.id][s][k][kp], -1)]), 'L',
                       omega[r.id][s][rp.id], 0))

        # C14
        for x, (g, r, _, o, k) in enumerate(groups.gp(1, True, False)):
            cns.append('C14(%d)' % x)
            cs.append(
                (linex([(vars.d[r.id][r.stops.index(o)][k], 1),
                        (vars.co[g.id][k], -M)]), 'R', g.alpha, -plan_horizon))

        # C14b
        for x, (g, r, _, o, k) in enumerate(groups.gp(1, True, False)):
            cns.append('C14b(%d)' % x)
            cs.append((linex([(vars.d[r.id][r.stops.index(o)][k],
                               1), (vars.to[g.id][k], -1)] +
                             ([(vars.co[g.id][k - 1], -M)] if k != 0 else [])),
                       'L', g.alpha, 0))

        # C15a
        for x, (g, r, rp, s, k, kp) in enumerate(groups.gp(1, True, True)):
            cns.append('C15(%d)' % x)
            cs.append((linex(
                [(vars.z[g.id][k][kp], 1), (vars.c[r.id][rp.id][s][k][kp],
                                            -1), (vars.co[g.id][k], -1)] +
                ([(vars.c[r.id][rp.id][s][k][kp - 1], 1)] if kp != 0 else []) +
                ([(vars.co[g.id][k - 1], 1)] if k != 0 else [])), 'G', -1, 0))

        # C15b
        for x, (g, r, rp, s, k, kp) in enumerate(groups.gp(1, True, True)):
            cns.append('C15b(%d)' % x)
            cs.append((linex([(vars.z[g.id][k][kp],
                               1), (vars.c[r.id][rp.id][s][k][kp], -1)] +
                             ([(vars.c[r.id][rp.id][s][k][kp - 1],
                                1)] if kp != 0 else [])), 'L', 0, 0))

        # C15c
        for x, (g, _, _, _, k, kp) in enumerate(groups.gp(1, True, True)):
            cns.append('C15c(%d)' % x)
            cs.append(
                (linex([(vars.z[g.id][k][kp], 1), (vars.co[g.id][k], -1)] +
                       ([(vars.co[g.id][k - 1], 1)] if k != 0 else [])), 'L',
                 0, 0))

        # C15h
        for x, (g, r, rp, s, k, kp) in enumerate(groups.gp(1, True, True)):
            cns.append('C15h(%d)' % x)
            cs.append((linex([(vars.p[r.id][rp.id][s][k][kp][g.id], 1),
                              (vars.z[g.id][k][kp], -M)]), 'L', 0, 0))

        # C15i
        for x, (g, r, rp, s, k, kp) in enumerate(groups.gp(1, True, True)):
            cns.append('C15i(%d)' % x)
            cs.append((linex([(vars.p[r.id][rp.id][s][k][kp][g.id], 1),
                              (vars.z[g.id][k][kp], -1)]), 'G', 0, 0))

        # C3.5
        print(groups.gp())
        for x, (g, r, rp, s) in enumerate(groups.gp()):
            cns.append('C3.5(%d)' % x)
            cs.append((linex([(vars.p[r.id][rp.id][s][k][kp][g.id], 1)
                              for k in range(r.n_buses)
                              for kp in range(rp.n_buses)]), 'E', 1, 0))

        # C3.6
        for x, (g, r1, rp1, s1, r2, rp2, s2) in enumerate(groups.gp(2)):
            cns.append('C3.6(%d)' % x)
            cs.append(
                (linex([(vars.p[r1.id][rp1.id][s1][k1][kp1][g.id], kp1 + 1)
                        for k1 in range(r1.n_buses)
                        for kp1 in range(rp1.n_buses)] +
                       [(vars.p[r2.id][rp2.id][s2][k2][kp2][g.id], -k2 - 1)
                        for k2 in range(r2.n_buses)
                        for kp2 in range(rp2.n_buses)]), 'E', 0, 0))

        # C3.8
        for x, (g, r, rp, s, k, kp) in enumerate(groups.gp(False, True, True)):
            cns.append('C3.8(%d)' % x)
            cs.append(
                (linex([(vars.p[r.id][rp.id][s][k][kp][g.id], 1),
                        (vars.c[r.id][rp.id][s][k][kp], -1)]), 'L', 0, 0))

        # C3.7a
        for x, (g, r, rp, s, k, kp) in enumerate(groups.gp(False, True, True)):
            cns.append('C3.7a(%d)' % x)
            cs.append((linex([(vars.zt[r.id][rp.id][s][k][kp][g.id], 1),
                              (vars.p[r.id][rp.id][s][k][kp][g.id],
                               -plan_horizon)]), 'L', 0, 0))

        # C3.7c
        for x, (g, r, rp, s, k, kp) in enumerate(groups.gp(False, True, True)):
            cns.append('C3.7c(%d)' % x)
            cs.append((linex([(vars.zt[r.id][rp.id][s][k][kp][g.id], 1),
                              (vars.t[r.id][rp.id][s][k][kp], -1),
                              (vars.p[r.id][rp.id][s][k][kp][g.id],
                               -plan_horizon)]), 'G', -plan_horizon, 0))

        # C3.7d
        for x, (g, r, rp, s, k, kp) in enumerate(groups.gp(False, True, True)):
            cns.append('C3.7d(%d)' % x)
            cs.append(
                (linex([(vars.zt[r.id][rp.id][s][k][kp][g.id], 1),
                        (vars.t[r.id][rp.id][s][k][kp], -1)]), 'L', 0, 0))

        # C3.7e
        for x, (g, trs) in enumerate(groups.gp(-1)):
            cns.append('C3.7e(%d)' % x)
            cs.append((linex([(vars.tg[g.id], 1)] +
                             [(vars.zt[r.id][rp.id][s][k][kp][g.id], -1)
                              for r, rp, s in trs for k in range(r.n_buses)
                              for kp in range(rp.n_buses)]), 'G', 0, 0))

        cs = zip(*cs)

        self._cp.linear_constraints.add(lin_expr=cs[0],
                                        senses=cs[1],
                                        rhs=cs[2],
                                        range_values=cs[3])

        self._cp.linear_constraints.set_names(enumerate(cns))
Exemple #6
0
	def generate_constraints(self, c=30, n=20):
		routes = [1,2,3]
		M = 60
		route_length = [2,3,4]
		max_capacity = c
		n_buses = n
		# demand = [[[9,9]], [[10,10]], [[24,25]]]
		# demand = [[[138,141,155,157,146,162,180,174,165,182,194,167,100,116,144,182,202,207,205,209,221,218,211,221,224,208,203,201,181,174,170,156]], [[152,158,164,192,185,179,156,167,166,177,160,179,90,99,91,96,59,51,45,47,46,48,35,43,39,45,42,35,42,28,30,31]], [[366,386,395,436,443,432,436,430,426,442,480,516,366,323,392,496,516,485,491,491,499,464,479,424,418,417,388,386,360,357,350,355], [2,8,10,2,2,5,6,3,3,2,6,2,2,5,7,4,7,2,4,6,8,7,8,11,15,15,16,13,12,11,14,13]]]
		demand = [[[591,662,708,542,823,871,836,681]], [[666,687,682,376,202,172,161,131]], [[1583,1741,1864,1577,1983,1866,1609,1422], [22,16,13,18,19,34,59,50]]]
		vars = self.vars

		cs = []
		cns = []

		cns.append('last')
		cs.append((
			linex([(vars.sw[0], 								   -1)] + 
			      [(vars.w[r][i], sum(map(lambda x: x[i], demand[r])))
			       for r in range(len(routes))
			       for i in range(len(demand[0][0]))]),
			'E', 0, 0))

		for r in range(len(routes)):
			for i in range(len(demand[0][0])):
				cns.append('C0(%d)' % (r*len(demand[0][0]) + i,))
				cs.append((
					linex([(vars.w[r][i],        1),
						   (vars.delta[r][i], -0.5)]),
					'E', 0, 0))

		for r in range(len(routes)):
			for i in range(len(demand[0][0])):
				cns.append('C1(%d)' % (r*len(demand[0][0]) + i,))
				cs.append((linex([(vars.delta[r][i], 1)]), 'L', max_capacity / sum(map(lambda x: x[i]/60.0, demand[r])), 0))

		for r in range(len(routes)):
			for i in range(len(demand[0][0])):
				cns.append('C22(%d)' % (r*len(demand[0][0]) + i,))
				cs.append((
					linex([(vars.ndelta[i][r][k], k+1)
						   for k in range(n_buses)]), 
					'G', route_length[r]*2, 0))

		for r in range(len(routes)):
			for i in range(len(demand[0][0])):
				cns.append('C2(%d)' % (r*len(demand[0][0]) + i,))
				cs.append((
					linex([(vars.n[i][r][k], 1)
						   for k in range(n_buses)]), 
					'E', 1, 0))

		if n_buses < len(vars.n[i][r]):
			for r in range(len(routes)):
				for i in range(len(demand[0][0])):
					for k in range(len(vars.n[i][r]) - n_buses):
						cns.append('C?(%d)' % k)
						cs.append((linex([(vars.n[i][r][k+n_buses], 1)]), 'E', 0, 0))

		for r in range(len(routes)):
			for i in range(len(demand[0][0])):
				for k in range(len(vars.n[i][r])):
				# for k in range(n_buses):
					cns.append('C3(%d)' % (r*len(demand[0][0]) + i,))
					cs.append((
						linex([(vars.ndelta[i][r][k],  1),
							   (vars.n[i][r][k],      -M)]), 
						'L', 0, 0))

					cns.append('C4(%d)' % (r*len(demand[0][0]) + i,))
					cs.append((
						linex([(vars.ndelta[i][r][k],  1),
							   (vars.delta[r][i],     -1)]),
						'L', 0, 0)) 

					cns.append('C5(%d)' % (r*len(demand[0][0]) + i,))
					cs.append((
						linex([(vars.ndelta[i][r][k],  1),
							   (vars.n[i][r][k],      -M),
							   (vars.delta[r][i],     -1)]), 
						'G', -M, 0))

		for i in range(len(demand[0][0])):
			cns.append('C6(%d)' % i)
			cs.append((
				linex([(vars.n[i][r][k], k+1)
					   for k in range(n_buses)
					   for r in range(len(routes))]), 
				'L', n_buses, 0))

		cs = zip(*cs)

		self._cp.linear_constraints.add(
            lin_expr=cs[0],
            senses=cs[1],
            rhs=cs[2],
            range_values=cs[3])

		self._cp.linear_constraints.set_names(enumerate(cns))
Exemple #7
0
    def generate_constraints(self):
        r_max = 1
        max_cap = 30
        n_buses = 5

        candidate_stops = [[55.785470, 12.523041], [55.786239, 12.522234],
                           [55.786541, 12.526961], [55.789277, 12.523946],
                           [55.786431, 12.521428], [55.785898, 12.520618],
                           [55.785138, 12.520728], [55.784637, 12.520459],
                           [55.782099, 12.514132], [55.782460, 12.519244],
                           [55.782226, 12.520035]]
        demand_centroids = [[55.786344, 12.524316], [55.786344, 12.524316],
                            [55.786872, 12.5259], [55.78944, 12.525146],
                            [55.786745, 12.520469], [55.787486, 12.518472],
                            [55.787911, 12.518621], [55.785438, 12.519377],
                            [55.785154, 12.520434], [55.785241, 12.518699],
                            [55.782330, 12.513640], [55.781929, 12.521461]]
        demands = [[
            0.0, 59.04, 20.34, 17.0, 0.0, 15.87, 8.27, 2.94, 23.92, 34.73,
            1.91, 15.26
        ],
                   [
                       47.32, 0.0, 7.31, 8.7, 0.0, 8.66, 5.07, 1.23, 11.37,
                       14.67, 0.97, 6.27
                   ],
                   [
                       14.09, 7.53, 0.0, 4.51, 0.0, 2.02, 1.23, 0.39, 3.78,
                       3.75, 0.24, 1.67
                   ],
                   [
                       14.64, 8.1, 4.75, 0.0, 0.0, 1.13, 0.81, 0.4, 2.07, 2.76,
                       0.27, 1.53
                   ],
                   [
                       0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                       0.0
                   ],
                   [
                       14.1, 8.61, 2.06, 1.38, 0.0, 0.0, 20.35, 1.28, 6.48,
                       16.1, 0.25, 1.72
                   ],
                   [
                       7.06, 4.96, 1.13, 1.09, 0.0, 20.74, 0.0, 0.39, 2.31,
                       6.38, 0.15, 0.86
                   ],
                   [
                       2.3, 1.03, 0.34, 0.23, 0.0, 0.99, 0.22, 0.0, 5.55, 5.08,
                       0.07, 0.37
                   ],
                   [
                       19.67, 11.18, 3.53, 1.91, 0.0, 7.05, 2.24, 6.45, 0.0,
                       28.64, 0.82, 4.56
                   ],
                   [
                       31.91, 14.58, 3.91, 2.84, 0.0, 17.88, 6.71, 7.28,
                       28.09, 0.0, 1.15, 7.68
                   ],
                   [
                       1.72, 0.94, 0.23, 0.3, 0.0, 0.22, 0.12, 0.1, 0.83, 1.13,
                       0.0, 1.2
                   ],
                   [
                       13.15, 5.68, 1.69, 1.78, 0.0, 1.63, 0.82, 0.4, 4.24,
                       7.15, 1.11, 0.0
                   ]]

        # demands = [[0, 100, 3, 36, 0, 14, 13, 19, 42, 55, 5, 20], [190, 0, 2, 25, 0, 18, 6, 8, 40, 37, 5, 4], [13, 10, 0, 3, 0, 3, 0, 1, 5, 3, 0, 0], [137, 1, 0, 0, 0, 10, 4, 1, 17, 34, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [47, 9, 2, 4, 0, 0, 63, 0, 0, 6, 2, 0], [46, 4, 0, 0, 0, 173, 0, 0, 0, 2, 2, 1], [61, 2, 0, 1, 0, 1, 0, 0, 78, 116, 0, 0], [116, 13, 1, 5, 0, 8, 0, 52, 0, 84, 16, 8], [167, 6, 0, 6, 0, 33, 13, 52, 230, 0, 23, 6], [1, 1, 0, 0, 0, 1, 0, 0, 7, 1, 0, 1], [97, 55, 0, 6, 0, 3, 2, 2, 47, 39, 9, 0]]

        n_candidate_stops = len(candidate_stops)
        n_demands = len(demands)

        costs = []

        for i in range(len(demands) + n_candidate_stops):
            costs.append([])

            for j in range(len(demands) + n_candidate_stops):
                org = demand_centroids[i] if i < len(
                    demands) else candidate_stops[i - len(demands)]
                des = demand_centroids[j] if j < len(
                    demands) else candidate_stops[j - len(demands)]

                if i < len(demands) or j < len(demands):
                    costs[i].append(walktime(org, des))
                else:
                    costs[i].append(
                        ridetime(i - len(demands), j - len(demands)))

        dd = enumerate([(x, y) for x in range(n_demands)
                        for y in range(n_demands)])
        dd_neq = enumerate([(x, y) for x in range(n_demands)
                            for y in range(n_demands) if x != y])
        dd_eq = enumerate([(x, x) for x in range(n_demands)])

        ddn = enumerate([(x, y, p) for x in range(n_demands)
                         for y in range(n_demands)
                         for p in range(n_demands + n_candidate_stops)])

        vars = self.vars

        cs = []
        cns = []

        for idx, (o, d) in dd:
            if o != d:
                cns.append('A1(%d)' % idx)
                cs.append((linex([(vars.x[o][d][o][j], 1)
                                  for j in range(n_demands + n_candidate_stops)
                                  if j != o]), 'E', 1, 0))
            else:
                cns.append('A1(%d)' % idx)
                cs.append((linex([(vars.x[o][d][o][j], 1)
                                  for j in range(n_demands + n_candidate_stops)
                                  ]), 'E', 0, 0))

        for idx, (o, d) in dd:
            if o != d:
                cns.append('A2(%d)' % idx)
                cs.append((linex([(vars.x[o][d][i][d], 1)
                                  for i in range(n_demands + n_candidate_stops)
                                  if i != d]), 'E', 1, 0))
            else:
                cns.append('A2(%d)' % idx)
                cs.append((linex([(vars.x[o][d][i][d], 1)
                                  for i in range(n_demands + n_candidate_stops)
                                  ]), 'E', 0, 0))

        # idx = 0
        # for o in range(n_demands):
        # 	for d in range(n_demands):
        # 		if o != d:
        # 			for i in range(n_demands + n_candidate_stops):
        for idx, (o, d, i) in ddn:
            if o != d:
                rhs = 0

                if i == o:
                    rhs = 1
                elif i == d:
                    rhs = -1

                cns.append('A3(%d)' % idx)
                cs.append((linex(
                    [(vars.x[o][d][i][j], 1)
                     for j in range(n_demands + n_candidate_stops) if i != j] +
                    [(vars.x[o][d][j][i], -1)
                     for j in range(n_demands + n_candidate_stops) if i != j]),
                           'E', rhs, 0))

                # idx += 1

        idx = 0
        for o in range(n_demands):
            for d in range(n_demands):
                for j in range(n_candidate_stops):
                    cns.append('A4(%d)' % idx)
                    cs.append((linex([(vars.x[o][d][i][j + n_demands], 1)
                                      for i in range(n_demands)] +
                                     [(vars.x[o][d][j + n_demands][i], 1)
                                      for i in range(n_demands)]), 'L', 1, 0))

                    idx += 1

        idx = 0
        for o in range(n_demands):
            for d in range(n_demands):
                if o != d:
                    cns.append('A5(%d)' % idx)
                    cs.append(
                        (linex([(vars.w[o][d], 1)] +
                               [(vars.x[o][d][i][j], -costs[i][j])
                                for i in range(n_demands + n_candidate_stops)
                                for j in range(n_demands + n_candidate_stops)
                                if i == o or j == d]), 'E', 0, 0))

                    idx += 1

        idx = 0
        for o in range(n_demands):
            for d in range(n_demands):
                if o != d:
                    cns.append('A6(%d)' % idx)
                    cs.append(
                        (linex([(vars.r[o][d], 1)] +
                               [(vars.x[o][d][i][j], -costs[i][j])
                                for i in range(n_demands + n_candidate_stops)
                                for j in range(n_demands + n_candidate_stops)
                                if i != o and j != d]), 'E', 0, 0))

                    idx += 1

        idx = 0
        for o in range(n_demands):
            for d in range(n_demands):
                # cns.append('TIMEGUARANTEE(%d)' % idx)
                # cs.append((
                # 	linex([(vars.r[o][d], 1),
                # 		   (vars.w[o][d], 1)]),
                # 	'L', 20, 0))

                # for j in range(n_demands):
                # 	cns.append('MAXWALK(%d)' % idx)
                # 	cs.append((linex([(vars.x[o][d][o][j], 1)]), 'E', 0, 0))

                # 	idx += 1

                # for i in range(n_demands):
                # 	cns.append('MAXWALK(%d)' % idx)
                # 	cs.append((linex([(vars.x[o][d][i][d], 1)]), 'E', 0, 0))
                # 	idx += 1

                for j in range(n_candidate_stops):
                    if costs[o][j + n_demands] > 5:
                        cns.append('A7(%d)' % idx)
                        cs.append((linex([(vars.x[o][d][o][j + n_demands], 1)
                                          ]), 'E', 0, 0))

                        idx += 1

                for i in range(n_candidate_stops):
                    if costs[d][i + n_demands] > 5:
                        cns.append('A8(%d)' % idx)
                        cs.append((linex([(vars.x[o][d][i + n_demands][d], 1)
                                          ]), 'E', 0, 0))

                        idx += 1

                for i in range(n_demands + n_candidate_stops):
                    for j in range(n_demands):
                        if j != d:
                            cns.append('A9(%d)' % idx)
                            cs.append(
                                (linex([(vars.x[o][d][i][j], 1)]), 'E', 0, 0))

                            idx += 1

                for i in range(n_demands):
                    for j in range(n_demands + n_candidate_stops):
                        if i != o:
                            cns.append('A10(%d)' % idx)
                            cs.append(
                                (linex([(vars.x[o][d][i][j], 1)]), 'E', 0, 0))

                        idx += 1

        idx = 0
        for r in range(r_max):
            # for i in range(n_candidate_stops):
            # 	for j in range(n_candidate_stops):
            # 		cns.append('B0(%d)' % idx)
            # 		cs.append((
            # 			linex([(vars.rx[r][i][j], 1)] +
            # 				  [(vars.x[o][d][i+n_demands][j+n_demands], -1)
            # 				   for o in range(len(demands))
            # 				   for d in range(len(demands))]),
            # 			'L', 0, 0))

            # 		idx += 1

            # idx = 0
            # for i in range(n_candidate_stops):
            # 	for j in range(n_candidate_stops):
            # 		cns.append('B0-1(%d)' % idx)
            # 		cs.append((
            # 			linex([(vars.rx0[r][i][j], 1)] +
            # 				  [(vars.x[o][d][i+n_demands][j+n_demands], -1)
            # 				   for o in range(len(demands))
            # 				   for d in range(len(demands))]),
            # 			'L', 0, 0))

            # 		idx += 1

            idx = 0
            for r in range(r_max):
                for i in range(n_candidate_stops):
                    for j in range(n_candidate_stops):
                        cns.append('B0-2(%d)' % idx)
                        cs.append((linex([(vars.rx0[r][i][j], 1),
                                          (vars.c[r], 1)]), 'L', 1, 0))

                        idx += 1

        idx = 0
        for i in range(n_candidate_stops):
            for j in range(n_candidate_stops):
                cns.append('B1(%d)' % idx)
                cs.append(
                    (linex([(vars.rx[r][i][j], 1)
                            for r in range(r_max)] + [(vars.rx0[r][i][j], 1)
                                                      for r in range(r_max)] +
                           [(vars.x[o][d][i + n_demands][j + n_demands],
                             -1.0 / n_demands / n_demands)
                            for o in range(n_demands)
                            for d in range(n_demands)]), 'G', 0, 0))
                idx += 1

        idx = 0
        for r in range(r_max):
            cns.append('B2(%d)' % idx)
            cs.append(
                (linex([(vars.rx[r][n_candidate_stops][j], 1)
                        for j in range(n_candidate_stops + 1)]), 'E', 1, 0))

            cns.append('B3(%d)' % idx)
            cs.append((linex([(vars.rx[r][i][n_candidate_stops], 1)
                              for i in range(n_candidate_stops + 1)] +
                             [(vars.rx[r][i][n_candidate_stops + 1], 1)
                              for i in range(n_candidate_stops)]), 'E', 1, 0))

            idx += 1

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                cns.append('B4(%d)' % idx)
                cs.append(
                    (linex([(vars.rx[r][i][j], 1)
                            for j in range(n_candidate_stops + 2) if i != j] +
                           [(vars.rx[r][j][i], -1)
                            for j in range(n_candidate_stops + 2) if i != j]),
                     'E', 0, 0))

                idx += 1

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                cns.append('B5(%d)' % idx)
                cs.append((linex([
                    (vars.rx[r][j][i], 1) for j in range(n_candidate_stops + 2)
                ]), 'L', 1, 0))

                cns.append('B6(%d)' % idx)
                cs.append((linex([
                    (vars.rx[r][i][j], 1) for j in range(n_candidate_stops + 2)
                ]), 'L', 1, 0))

                idx += 1

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                cns.append('B7(%d)' % idx)
                cs.append((linex([(vars.rx[r][i][i], 1)]), 'E', 0, 0))

                idx += 1

        for r in range(r_max):
            cns.append('R9(%d)' % r)
            cs.append((linex([
                (vars.rx[r][n_candidate_stops][n_candidate_stops + 1], 1)
            ]), 'E', 0, 0))

        idx = 0
        for r in range(r_max):
            for i in range(1, n_candidate_stops):
                cns.append('B8-1(%d)' % idx)
                cs.append(
                    (linex([(vars.rx[r][i][n_candidate_stops], 1)] +
                           [(vars.rx[r][k][n_candidate_stops], 1)
                            for k in range(n_candidate_stops + 1) if k < i]),
                     'L', 1, 0))

                cns.append('B8-2(%d)' % idx)
                cs.append((linex([(vars.rx[r][i][n_candidate_stops], 1)] +
                                 [(vars.rx[r][j][i], -1)
                                  for j in range(n_candidate_stops + 1)]), 'L',
                           0, 0))

                cns.append('B8-3(%d)' % idx)
                cs.append((linex([(vars.rx[r][i][n_candidate_stops], 1),
                                  (vars.c[r], 1)]), 'L', 1, 0))

                cns.append('B8-4(%d)' % idx)
                cs.append(
                    (linex([(vars.rx[r][i][n_candidate_stops],
                             1), (vars.c[r], 1)] +
                           [(vars.rx[r][k][n_candidate_stops], 1)
                            for k in range(n_candidate_stops + 1) if k < i] +
                           [(vars.rx[r][j][i], -1)
                            for j in range(n_candidate_stops + 1)]), 'G', 0,
                     0))

                idx += 1

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                for j in range(n_candidate_stops):
                    if i != j:
                        cns.append('B9(%d)' % idx)
                        cs.append(
                            (linex([(vars.rq[r][i], 1), (vars.rq[r][j], -1),
                                    (vars.rx[r][i][j], 1000)]), 'L', 999, 0))

                        idx += 1

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                for j in range(n_candidate_stops):
                    cns.append('B10-1(%d)' % idx)
                    cs.append((linex([(vars.rx0[r][i][j], 1),
                                      (vars.rx[r][i][n_candidate_stops], -1),
                                      (vars.rx[r][n_candidate_stops][j], -1)]),
                               'G', -1, 0))

                    cns.append('B10-2(%d)' % idx)
                    cs.append((linex([(vars.rx0[r][i][j], 1),
                                      (vars.rx[r][i][n_candidate_stops], -1)]),
                               'L', 0, 0))

                    cns.append('B10-3(%d)' % idx)
                    cs.append((linex([(vars.rx0[r][i][j], 1),
                                      (vars.rx[r][n_candidate_stops][j], -1)]),
                               'L', 0, 0))

                    idx += 1

        for r in range(r_max):
            cns.append('C1(%d)' % r)
            cs.append((linex([(vars.c[r], 1)] +
                             [(vars.rx[r][i][n_candidate_stops + 1], -1)
                              for i in range(n_candidate_stops)]), 'E', 0, 0))

        idx = 0
        for i in range(n_candidate_stops):
            for j in range(n_candidate_stops):
                if i != j:
                    cns.append('C3(%d)' % idx)
                    cs.append(
                        (linex([(vars.x[o][d][i + n_demands][j + n_demands],
                                 demands[o][d]) for o in range(n_demands)
                                for d in range(n_demands)] +
                               [(vars.z1[r][i][j], -max_cap)
                                for r in range(r_max)]), 'L', 0, 0))

                    idx += 1

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                for j in range(n_candidate_stops):
                    if i != j:
                        cns.append('C3-2(%d)' % idx)
                        cs.append(
                            (linex([(vars.z1[r][i][j], 1),
                                    (vars.rx[r][i][j], -60)]), 'L', 0, 0))

                        cns.append('C3-3(%d)' % idx)
                        cs.append((linex([(vars.z1[r][i][j], 1),
                                          (vars.f[r], -1)]), 'L', 0, 0))

                        cns.append('C3-3(%d)' % idx)
                        cs.append(
                            (linex([(vars.z1[r][i][j], 1), (vars.f[r], -1),
                                    (vars.rx[r][i][j], -60)]), 'G', -60, 0))

                        idx += 1

        idx = 0
        for r in range(r_max):
            for i in range(n_candidate_stops):
                for j in range(n_candidate_stops):
                    if i != j:
                        cns.append('C5-2(%d)' % idx)
                        cs.append(
                            (linex([(vars.z2[r][i][j], 1),
                                    (vars.rx[r][i][j], -60)]), 'L', 0, 0))

                        cns.append('C5-3(%d)' % idx)
                        cs.append((linex([(vars.z2[r][i][j], 1),
                                          (vars.c[r], -60)]), 'L', 0, 0))

                        cns.append('C5-4(%d)' % idx)
                        cs.append((linex([(vars.z2[r][i][j], 1),
                                          (vars.f[r], -1)]), 'L', 0, 0))

                        cns.append('C5-5(%d)' % idx)
                        cs.append((linex([(vars.z2[r][i][j], 1),
                                          (vars.f[r], -1),
                                          (vars.rx[r][i][j], -60),
                                          (vars.c[r], -60)]), 'G', -120, 0))

                        idx += 1

        cns.append('C5')
        cs.append((linex([(vars.z1[r][i][j], 1) for r in range(r_max)
                          for i in range(n_candidate_stops)
                          for j in range(n_candidate_stops) if i != j] +
                         [(vars.z2[r][i][j], 1) for r in range(r_max)
                          for i in range(n_candidate_stops)
                          for j in range(n_candidate_stops) if i != j]), 'L',
                   n_buses, 0))

        # idx = 0
        # for o in range(n_demands):
        # 	for d in range(n_demands):
        # 		if o != d:
        # 			for i in range(n_demands + n_candidate_stops):
        # 				for j in range(n_demands + n_candidate_stops):
        # 					if i != j:
        # 						cns.append('SUB(%d)' % idx)
        # 						cs.append((
        # 							linex([(vars.q[o][d][i],       1),
        # 								   (vars.q[o][d][j],      -1),
        # 								   (vars.x[o][d][i][j], 1000)]),
        # 							'L', 999, 0))

        # 						idx += 1

        # idx = 0
        # for o in range(n_demands):
        # 	for d in range(n_demands):
        # 		if o != d:
        # 			cns.append('C0(%d)' % idx)
        # 			cs.append((
        # 				linex([(vars.x[o][d][i][j], 1)
        # 					   for j in range(n_demands)
        # 					   for i in range(n_candidate_stops+n_demands)]),
        # 				'E', 1, 0))

        # 			idx += 1

        # idx = 0
        # for o in range(n_demands):
        # 	for d in range(n_demands):
        # 		if o != d:
        # 			cns.append('C4(%d)' % idx)
        # 			cs.append((
        # 				linex([(vars.x[o][d][i][j], 1)
        # 					   for i in range(n_demands)
        # 					   for j in range(n_demands)]),
        # 				'E', 0, 0))

        # idx += 1

        # idx = 0
        # for o in range(n_demands):
        # 	for d in range(n_demands):
        # 		if o != d:
        # 			cns.append('C5(%d)' % idx)
        # 			cs.append((
        # 				linex([(vars.t[o][d], 1)] +
        # 					  [(vars.x[o][d][i][j], -costs[i][j])
        # 					   for i in range(n_demands + n_candidate_stops)
        # 					   for j in range(n_demands + n_candidate_stops)]),
        # 				'E', 0, 0))

        # 			idx += 1

        # idx = 0
        # for i in range(n_candidate_stops):
        # 	for j in range(n_candidate_stops):
        # 		if i != j:
        # 			cns.append('C3(%d)' % idx)
        # 			cs.append((
        # 				linex([(vars.x[o][d][n_demands + i][n_demands + j], -demands[o][d])
        # 					   for o in range(n_demands)
        # 					   for d in range(n_demands)] +
        # 					  [(vars.D[i][j], 1)]),
        # 				'E', 0, 0))

        # idx = 0
        # for i in range(n_candidate_stops):
        # 	for j in range(n_candidate_stops):
        # 		if i != j:
        # 			cns.append('C2(%d)' % idx)
        # 			cs.append((
        # 				linex([(vars.D[i][j], 1)] +
        # 					  [(vars.f[r], -max_cap)]),
        # 				'L', 0, 0))
        # 			idx += 1

        # idx = 0
        # for r in range(r_max):
        # 	cns.append('C4(%d)' % idx)
        # 	cs.append((
        # 		linex([(vars.T[r], 1)] +
        # 			  [(vars.rx[r][i][j], -ridetime(i, j))
        # 			   for i in range(n_candidate_stops)
        # 			   for j in range(n_candidate_stops)
        # 			   if i != j]),
        # 		'E', 0, 0))

        # 	idx += 1

        # idx = 0
        # for r in range(r_max):
        # 	for i in range(n_candidate_stops-1):
        # 		cns.append('B8(%d)' % i)
        # 		cs.append((
        # 			linex([(vars.rx[r][i][n_candidate_stops],    1),
        # 				   (vars.rx[r][i+1][n_candidate_stops], -1)]),
        # 			'L', 0, 0))

        # 		idx += 1

        cs = zip(*cs)

        self._cp.linear_constraints.add(lin_expr=cs[0],
                                        senses=cs[1],
                                        rhs=cs[2],
                                        range_values=cs[3])

        self._cp.linear_constraints.set_names(enumerate(cns))
Exemple #8
0
    def generate_constraints(self, **kwargs):
        r_max = kwargs['r_max']
        max_cap = kwargs['max_cap']
        candidate_stops = kwargs['candidate_stops']
        demand_centroids = kwargs['demand_centroids']
        demands = kwargs['demands']
        n_buses = kwargs['n_buses']
        candidate_routes = kwargs['candidate_routes']
        is_circular = kwargs['is_circular']
        t = kwargs['route_time']
        costs = kwargs['costs']

        n_candidate_stops = len(candidate_stops)
        n_demands = len(demands)
        n_candidate_routes = len(candidate_routes)

        def it(spec):
            lengths = {
                'd': n_demands,
                's': n_candidate_stops,
                'n': n_demands + n_candidate_stops,
                'r': r_max,
                'c': n_candidate_routes,
                'b': n_buses + 1
            }

            if len(spec) == 1:
                return enumerate(range(lengths[spec]))

            return enumerate(product(*[range(lengths[x]) for x in spec]))

        vars = self.vars

        cs = []
        cns = []

        #///////////////////////////////////////////////////////////////////////
        #          Origin destination shortest path problem (ODSSP)
        #///////////////////////////////////////////////////////////////////////

        for idx, (o, d) in it('dd'):
            if o != d:
                cns.append('A1(%d)' % idx)
                cs.append((linex([(vars.x[o][d][o][j], 1)
                                  for j in range(n_demands + n_candidate_stops)
                                  if j != o]), 'E', 1, 0))
            else:
                cns.append('A1(%d)' % idx)
                cs.append((linex([(vars.x[o][d][o][j], 1)
                                  for j in range(n_demands + n_candidate_stops)
                                  ]), 'E', 0, 0))

        for idx, (o, d) in it('dd'):
            if o != d:
                cns.append('A2(%d)' % idx)
                cs.append((linex([(vars.x[o][d][i][d], 1)
                                  for i in range(n_demands + n_candidate_stops)
                                  if i != d]), 'E', 1, 0))
            else:
                cns.append('A2(%d)' % idx)
                cs.append((linex([(vars.x[o][d][i][d], 1)
                                  for i in range(n_demands + n_candidate_stops)
                                  ]), 'E', 0, 0))

        for idx, (o, d, i) in it('ddn'):
            if o != d:
                rhs = 0

                if i == o:
                    rhs = 1
                elif i == d:
                    rhs = -1

                cns.append('A3(%d)' % idx)
                cs.append((linex(
                    [(vars.x[o][d][i][j], 1)
                     for j in range(n_demands + n_candidate_stops) if j != i] +
                    [(vars.x[o][d][j][i], -1)
                     for j in range(n_demands + n_candidate_stops) if j != i]),
                           'E', rhs, 0))

        for idx, (o, d, j) in it('dds'):
            cns.append('A4(%d)' % idx)
            cs.append((linex([(vars.x[o][d][i][j + n_demands], 1)
                              for i in range(n_demands)] +
                             [(vars.x[o][d][j + n_demands][i], 1)
                              for i in range(n_demands)]), 'L', 1, 0))

        for idx, (o, d) in it('dd'):
            if o != d:
                cns.append('A5(%d)' % idx)
                cs.append((linex([(vars.w[o][d], 1)] +
                                 [(vars.x[o][d][i][j], -costs[i][j])
                                  for i in range(n_demands + n_candidate_stops)
                                  for j in range(n_demands + n_candidate_stops)
                                  if i == o or j == d]), 'E', 0, 0))

        for idx, (o, d) in it('dd'):
            if o != d:
                cns.append('A6(%d)' % idx)
                cs.append(
                    (linex([(vars.r[o][d], 1)] +
                           [(vars.x[o][d][i + n_demands][j + n_demands],
                             -costs[i + n_demands][j + n_demands])
                            for i in range(n_candidate_stops)
                            for j in range(n_candidate_stops)]), 'E', 0, 0))

        for idx, (o, d, j) in it('dds'):
            if costs[o][j + n_demands] > 5:
                cns.append('A7(%d)' % idx)
                cs.append(
                    (linex([(vars.x[o][d][o][j + n_demands], 1)]), 'E', 0, 0))

        for idx, (o, d, i) in it('dds'):
            if costs[i + n_demands][d] > 5:
                cns.append('A8(%d)' % idx)
                cs.append(
                    (linex([(vars.x[o][d][i + n_demands][d], 1)]), 'E', 0, 0))

        for idx, (o, d, i, j) in it('ddnd'):
            if j != d:
                cns.append('A9(%d)' % idx)
                cs.append((linex([(vars.x[o][d][i][j], 1)]), 'E', 0, 0))

        for idx, (o, d, i, j) in it('dddn'):
            if i != o:
                cns.append('A10(%d)' % idx)
                cs.append((linex([(vars.x[o][d][i][j], 1)]), 'E', 0, 0))

        for idx, (o, d) in it('dd'):
            if o != d:
                # if costs[o][d] == 0:
                # 	f = 1
                # else:
                # 	f = 1.0/float(costs[o][d])/float(costs[o][d])
                f = 0

                cns.append(('A11-1(%d)' % idx))
                cs.append((linex([(vars.s[o][d], 1), (vars.r[o][d], 1),
                                  (vars.w[o][d], 1 - f), (vars.hod[o][d], 0.5)
                                  ]), 'E', costs[o][d] * (1 - f), 0))

                cns.append(('A12(%d)' % idx))
                cs.append(
                    (linex([(vars.r[o][d], 1), (vars.w[o][d], 1),
                            (vars.hod[o][d], 0.5)]), 'L', costs[o][d], 0))
            else:
                cns.append(('A11-2(%d)' % idx))
                cs.append((linex([(vars.s[o][d], 1)]), 'E', 0, 0))

        #///////////////////////////////////////////////////////////////////////
        #            Transit route network design problem (TRNDP)
        #///////////////////////////////////////////////////////////////////////

        # for idx, (r,i,j) in it('rss'):
        # 	cns.append('B1(%d)' % idx)
        # 	cs.append((
        # 		linex([(vars.rx0[r][i][j], 1),
        # 			   (vars.c[r], 1)]),
        # 		'L', 1, 0))

        # for idx, (r,i,j) in it('rss'):
        # 	cns.append('B1(%d)' % idx)
        # 	cs.append((
        # 		linex([(vars.rx0[r][i][j], 1)] +
        # 		      [(vars.xc[r][c],    -(candidate_routes[c][i][n_candidate_stops] + candidate_routes[c][n_candidate_stops][j] - 1))
        # 		       for c in range(n_candidate_routes)]),
        # 		'L', 0, 0))

        for idx, (i, j) in it('ss'):
            cns.append('B2(%d)' % idx)
            cs.append(
                (linex([(vars.rx[r][i][j], 1)
                        for r in range(r_max)] + [(vars.rx0[r][i][j], 1)
                                                  for r in range(r_max)] +
                       [(vars.x[o][d][i + n_demands][j + n_demands],
                         -1.0 / n_demands / n_demands)
                        for o in range(n_demands)
                        for d in range(n_demands)]), 'G', 0, 0))

        for idx, (r, i, j) in it('rss'):
            cns.append('B3(%d)' % idx)
            cs.append((linex([(vars.rx[r][i][j], 1)] +
                             [(vars.xc[r][c], -candidate_routes[c][i][j])
                              for c in range(n_candidate_routes)]), 'E', 0, 0))

        for idx, (r, i) in it('rs'):
            cns.append('B4(%d)' % idx)
            cs.append((linex([(vars.rx[r][i][n_candidate_stops], 1)] +
                             [(vars.xc[r][c],
                               -candidate_routes[c][i][n_candidate_stops])
                              for c in range(n_candidate_routes)]), 'E', 0, 0))

        for idx, (r, i) in it('rs'):
            cns.append('B5(%d)' % idx)
            cs.append((linex([(vars.rx[r][i][n_candidate_stops + 1], 1)] +
                             [(vars.xc[r][c],
                               -candidate_routes[c][i][n_candidate_stops + 1])
                              for c in range(n_candidate_routes)]), 'E', 0, 0))

        for idx, (r, j) in it('rs'):
            cns.append('B8(%d)' % idx)
            cs.append((linex([(vars.rx[r][n_candidate_stops][j], 1)] +
                             [(vars.xc[r][c],
                               -candidate_routes[c][n_candidate_stops][j])
                              for c in range(n_candidate_routes)]), 'E', 0, 0))

        for idx, (r) in it('r'):
            cns.append('B6(%d)' % idx)
            cs.append((linex([(vars.xc[r][c], 1)
                              for c in range(n_candidate_routes)]), 'E', 1, 0))

        for idx, (r) in it('r'):
            cns.append('B7(%d)' % idx)
            cs.append((linex([(vars.xn[r][n], 1)
                              for n in range(n_buses + 1)]), 'E', 1, 0))

        # for idx, (r) in it('r'):
        # 	cns.append('B3(%d)' % idx)
        # 	cs.append((
        # 		linex([(vars.rx[r][n_candidate_stops][j], 1)
        # 			   for j in range(n_candidate_stops+1)]),
        # 		'E', 1, 0))

        # for idx, (r) in it('r'):
        # 	cns.append('B4(%d)' % idx)
        # 	cs.append((
        # 		linex([(vars.rx[r][i][n_candidate_stops], 1)
        # 			   for i in range(n_candidate_stops+1)] +
        # 			  [(vars.rx[r][i][n_candidate_stops+1], 1)
        # 			   for i in range(n_candidate_stops)]),
        # 		'E', 1, 0))

        # for idx, (r,i) in it('rs'):
        # 	cns.append('B5(%d)' % idx)
        # 	cs.append((
        # 		linex([(vars.rx[r][i][j],  1)
        # 			   for j in range(n_candidate_stops+2)
        # 			   if i != j] +
        # 			  [(vars.rx[r][j][i], -1)
        # 			   for j in range(n_candidate_stops+2)
        # 			   if i != j]),
        # 		'E', 0, 0))

        # for idx, (r,i) in it('rs'):
        # 	cns.append('B6(%d)' % idx)
        # 	cs.append((
        # 		linex([(vars.rx[r][j][i], 1)
        # 			   for j in range(n_candidate_stops+2)]),
        # 		'L', 1, 0))

        # for idx, (r,i) in it('rs'):
        # 	cns.append('B7(%d)' % idx)
        # 	cs.append((
        # 		linex([(vars.rx[r][i][j], 1)
        # 			   for j in range(n_candidate_stops+2)]),
        # 		'L', 1, 0))

        # for idx, (r,i) in it('rs'):
        # 	cns.append('B8(%d)' % idx)
        # 	cs.append((linex([(vars.rx[r][i][i], 1)]), 'E', 0, 0))

        # for idx, (r) in it('r'):
        # 	cns.append('B9(%d)' % r)
        # 	cs.append((linex([(vars.rx[r][n_candidate_stops][n_candidate_stops+1], 1)]), 'E', 0, 0))

        # for idx, (r,i,j) in it('rss'):
        # 	if i != j:
        # 		cns.append('B10(%d)' % idx)
        # 		cs.append((
        # 			linex([(vars.rq[r][i],       1),
        # 				   (vars.rq[r][j],      -1),
        # 				   (vars.rx[r][i][j], 1000)]),
        # 			'L', 999, 0))

        # for idx, (r,i) in it('rs'):
        # 	if i != 0:
        # 		cns.append('B11-1(%d)' % idx)
        # 		cs.append((
        # 			linex([(vars.rx[r][i][n_candidate_stops], 1)] +
        # 				  [(vars.rx[r][k][n_candidate_stops], 1)
        # 				    for k in range(n_candidate_stops+1)
        # 				    if k < i]),
        # 			'L', 1, 0))

        # for idx, (r,i) in it('rs'):
        # 	if i != 0:
        # 		cns.append('B11-2(%d)' % idx)
        # 		cs.append((
        # 			linex([(vars.rx[r][i][n_candidate_stops],  1)] +
        # 				  [(vars.rx[r][j][i],                 -1)
        # 				   for j in range(n_candidate_stops+1)]),
        # 			'L', 0, 0))

        # for idx, (r,i) in it('rs'):
        # 	if i != 0:
        # 		cns.append('B11-3(%d)' % idx)
        # 		cs.append((
        # 			linex([(vars.rx[r][i][n_candidate_stops], 1),
        # 				   (vars.c[r],                        1)]),
        # 			'L', 1, 0))

        # for idx, (r,i) in it('rs'):
        # 	if i != 0:
        # 		cns.append('B11-4(%d)' % idx)
        # 		cs.append((
        # 			linex([(vars.rx[r][i][n_candidate_stops],  1),
        # 				   (vars.c[r],                         1)] +
        # 				  [(vars.rx[r][k][n_candidate_stops],  1)
        # 				    for k in range(n_candidate_stops+1)
        # 				    if k < i] +
        # 				  [(vars.rx[r][j][i],                 -1)
        # 				   for j in range(n_candidate_stops+1)]),
        # 			'G', 0, 0))

        for idx, (r, i, j) in it('rss'):
            cns.append('B12-1(%d)' % idx)
            cs.append(
                (linex([(vars.rx0[r][i][j], 1),
                        (vars.rx[r][i][n_candidate_stops], -1),
                        (vars.rx[r][n_candidate_stops][j], -1)]), 'G', -1, 0))

        for idx, (r, i, j) in it('rss'):
            cns.append('B12-2(%d)' % idx)
            cs.append(
                (linex([(vars.rx0[r][i][j], 1),
                        (vars.rx[r][i][n_candidate_stops], -1)]), 'L', 0, 0))

        for idx, (r, i, j) in it('rss'):
            cns.append('B12-3(%d)' % idx)
            cs.append(
                (linex([(vars.rx0[r][i][j], 1),
                        (vars.rx[r][n_candidate_stops][j], -1)]), 'L', 0, 0))

        for idx, (r, i, j) in it('rss'):
            cns.append('B13(%d)' % idx)
            cs.append((linex([(vars.rx[r][i][j], 1),
                              (vars.rx0[r][i][j], 1)]), 'L', 1, 0))

        #///////////////////////////////////////////////////////////////////////
        #                Frequency determination problem (FDP)
        #///////////////////////////////////////////////////////////////////////


# 		for idx, (r) in it('r'):
# 			cns.append('C1(%d)' % r)
# 			cs.append((
# 				linex([(vars.c[r], 1)] +
# 					  [(vars.rx[r][i][n_candidate_stops+1], -1)
# 					   for i in range(n_candidate_stops)]),
# 				'E', 0, 0))

# for idx, (i,j) in it('ss'):
# 	if i != j:
# 		cns.append('C3-1(%d)' % idx)
# 		cs.append((
# 			linex([(vars.x[o][d][i+n_demands][j+n_demands], demands[o][d])
# 				for o in range(n_demands)
# 				for d in range(n_demands)] +
# 				[(vars.zc[r][c], -max_cap*candidate_routes[c][i][j])
# 				for c in range(n_candidate_routes)
# 				for r in range(r_max)] +
# 				[(vars.z3[r][i][j], -max_cap*candidate_routes[c][i][j])
# 				for r in range(r_max)]),
# 			'L', 0, 0))
        for idx, (i, j) in it('ss'):
            if i != j:
                cns.append('C3-1(%d)' % idx)
                cs.append((linex([(vars.x[o][d][i + n_demands][j + n_demands],
                                   demands[o][d]) for o in range(n_demands)
                                  for d in range(n_demands)] +
                                 [(vars.z4[r][i][j], -max_cap)
                                  for r in range(r_max)] +
                                 [(vars.z5[r][i][j], -max_cap)
                                  for r in range(r_max)]), 'L', 0, 0))

        for idx, (r, c) in it('rc'):
            cns.append('C3-2(%d)' % idx)
            cs.append((linex([(vars.zc[r][c], 1),
                              (vars.f[r], -1)]), 'L', 0, 0))

            cns.append('C3-3(%d)' % idx)
            cs.append((linex([(vars.zc[r][c], 1),
                              (vars.xc[r][c], -60)]), 'L', 0, 0))

            cns.append('C3-4(%d)' % idx)
            cs.append((linex([(vars.zc[r][c], 1), (vars.f[r], -1),
                              (vars.xc[r][c], -60)]), 'G', -60, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-2(%d)' % idx)
                cs.append((linex([(vars.z4[r][i][j], 1),
                                  (vars.rx[r][i][j], -60)]), 'L', 0, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-3(%d)' % idx)
                cs.append((linex([(vars.z4[r][i][j], 1),
                                  (vars.f[r], -1)]), 'L', 0, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-4(%d)' % idx)
                cs.append((linex([(vars.z4[r][i][j], 1), (vars.f[r], -1),
                                  (vars.rx[r][i][j], -60)]), 'G', -60, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-5(%d)' % idx)
                cs.append((linex([(vars.z5[r][i][j], 1),
                                  (vars.rx0[r][i][j], -60)]), 'L', 0, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-6(%d)' % idx)
                cs.append((linex([(vars.z5[r][i][j], 1),
                                  (vars.f[r], -1)]), 'L', 0, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-7(%d)' % idx)
                cs.append((linex([(vars.z5[r][i][j], 1), (vars.f[r], -1),
                                  (vars.rx0[r][i][j], -60)]), 'G', -60, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-5(%d)' % idx)
                cs.append((linex([(vars.z3[r][i][j], 1),
                                  (vars.rx0[r][i][j], -60)]), 'L', 0, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-6(%d)' % idx)
                cs.append((linex([(vars.z3[r][i][j], 1),
                                  (vars.f[r], -1)]), 'L', 0, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-7(%d)' % idx)
                cs.append((linex([(vars.z3[r][i][j], 1), (vars.f[r], -1),
                                  (vars.rx0[r][i][j], -60)]), 'G', -60, 0))

        for idx, (r, i, j) in it('rss'):
            if i != j:
                cns.append('C3-8(%d)' % idx)
                cs.append(
                    (linex([(vars.z3[r][i][j], 1)] +
                           [(vars.xc[r][c], -60)
                            for c in range(n_candidate_routes)]), 'L', 0, 0))

        for idx, (r) in it('r'):
            cns.append('C4(%d)' % idx)
            cs.append((linex([(vars.xn[r][n], n) for n in range(n_buses + 1)] +
                             [(vars.zc[r][c], -t[c])
                              for c in range(n_candidate_routes)]), 'E', 0, 0))

        cns.append('C5')
        cs.append((linex([(vars.xn[r][n], n)
                          for n in range(n_buses + 1)]), 'L', n_buses, 0))

        for idx, (r) in it('r'):
            cns.append('C5(%d)' % idx)
            cs.append((linex([(vars.f[r], 1)]), 'G', 0.2, 0))

        for idx, (r) in it('r'):
            cns.append('C6(%d)' % idx)
            cs.append(
                (linex([(vars.h[r], 1)] +
                       [(vars.z1[r][n][c], -t[c] / float(n) if n != 0 else 0)
                        for n in range(n_buses + 1)
                        for c in range(n_candidate_routes)]), 'E', 0, 0))

        for idx, (r, n, c) in it('rbc'):
            cns.append('C7-1(%d)' % idx)
            cs.append((linex([(vars.z1[r][n][c], 1),
                              (vars.xn[r][n], -1)]), 'L', 0, 0))

            cns.append('C7-2(%d)' % idx)
            cs.append((linex([(vars.z1[r][n][c], 1),
                              (vars.xc[r][c], -1)]), 'L', 0, 0))

            cns.append('C7-3(%d)' % idx)
            cs.append((linex([(vars.z1[r][n][c], 1), (vars.xc[r][c], -1),
                              (vars.xn[r][n], -1)]), 'G', -1, 0))

        for idx, (o, d, r, j, k) in it('ddrss'):
            cns.append('C8-1(%d)' % idx)
            cs.append(
                (linex([(vars.z2[o][d][r][j][k], 1),
                        (vars.x[o][d][o][j + n_demands], -60)]), 'L', 0, 0))

            cns.append('C8-2(%d)' % idx)
            cs.append((linex([(vars.z2[o][d][r][j][k], 1),
                              (vars.x[o][d][j + n_demands][k + n_demands], -60)
                              ]), 'L', 0, 0))

            cns.append('C8-3(%d)' % idx)
            cs.append((linex([(vars.z2[o][d][r][j][k], 1),
                              (vars.rx[r][j][k], -60),
                              (vars.rx0[r][j][k], -60)]), 'L', 0, 0))

            # cns.append('C8-4(%d)' % idx)
            # cs.append((
            # 	linex([(vars.z2[o][d][r][j][k],  1),
            # 		   (vars.h[r],              -1)]),
            # 	'L', 0, 0))

            cns.append('C8-6(%d)' % idx)
            cs.append((linex([
                (vars.z2[o][d][r][j][k], 1), (vars.h[r], -1),
                (vars.rx[r][j][k], -60), (vars.rx0[r][j][k], -60),
                (vars.x[o][d][o][j + n_demands], -60),
                (vars.x[o][d][j + n_demands][k + n_demands], -60)
            ]), 'G', -180, 0))

        for idx, (o, d) in it('dd'):
            cns.append('C9(%d)' % idx)
            cs.append((linex([(vars.hod[o][d], 1)] +
                             [(vars.z2[o][d][r][j][k], -1)
                              for r in range(r_max)
                              for j in range(n_candidate_stops)
                              for k in range(n_candidate_stops) if j != k]),
                       'E', 0, 0))

        cs = zip(*cs)

        self._cp.linear_constraints.add(lin_expr=cs[0],
                                        senses=cs[1],
                                        rhs=cs[2],
                                        range_values=cs[3])

        self._cp.linear_constraints.set_names(enumerate(cns))
Exemple #9
0
	def generate_constraints(self):
		r_max = 1

		candidate_stops = [[55.785470, 12.523041],[55.786239, 12.522234],[55.786541, 12.526961],[55.789277, 12.523946],[55.786431, 12.521428],[55.785898, 12.520618],[55.785138, 12.520728],[55.784637, 12.520459],[55.782099, 12.514132],[55.782460, 12.519244],[55.782226, 12.520035]]
		demand_centroids = [[55.786344,12.524316],[55.786344,12.524316],[55.786872,12.5259],[55.78944,12.525146],[55.786745,12.520469],[55.787486,12.518472],[55.787911,12.518621],[55.785438,12.519377],[55.785154,12.520434],[55.785241,12.518699],[55.783,12.512931],[55.781929,12.521461]]
		demands = [[0, 100, 3, 36, 0, 14, 13, 19, 42, 55, 5, 20],
					 [190, 0, 2, 25, 0, 18, 6, 8, 40, 37, 5, 4],
					 [13, 10, 0, 3, 0, 3, 0, 1, 5, 3, 0, 0],
					 [137, 1, 0, 0, 0, 10, 4, 1, 17, 34, 1, 1],
					 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
					 [47, 9, 2, 4, 0, 0, 63, 0, 0, 6, 2, 0],
					 [46, 4, 0, 0, 0, 173, 0, 0, 0, 2, 2, 1],
					 [61, 2, 0, 1, 0, 1, 0, 0, 78, 116, 0, 0],
					 [116, 13, 1, 5, 0, 8, 0, 52, 0, 84, 16, 8],
					 [167, 6, 0, 6, 0, 33, 13, 52, 230, 0, 23, 6],
					 [1, 1, 0, 0, 0, 1, 0, 0, 7, 1, 0, 1],
					 [97, 55, 0, 6, 0, 3, 2, 2, 47, 39, 9, 0]]

		n_candidate_stops = len(candidate_stops)
		n_buses = 11

		costs = []

		for i1, (stop_x1, stop_y1) in enumerate([[0,0]] + candidate_stops):
			costs.append([])

			for i2, (stop_x2, stop_y2) in enumerate([[0,0]] + candidate_stops):
				costs[i1].append(275 * (abs(stop_x1 - stop_x2) + abs(stop_y1 - stop_y2)))

		vars = self.vars

		cs = []
		cns = []

		# idx = 0
		# for r in range(r_max):
		# 	for i in range(n_candidate_stops):
		# 		for j in range(n_candidate_stops):
		# 			cns.append('RTS(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.D[r][i][j], 1),
		# 					   (vars.x[r][i+1][j+1], -ridetime(candidate_stops[i], candidate_stops[j]))] + 
		# 					  [(vars.x[r][i+1][x+1], -ridetime(candidate_stops[i], candidate_stops[x]))
		# 					   for x in range(n_candidate_stops)
		# 					   if x != i and x != j] +
		# 					  [(vars.xD[r][i][x][j], -1)
		# 					   for x in range(n_candidate_stops)
		# 					   if x != i and x != j]),
		# 				'E', 0, 0))

		# 			idx += 1

		# idx = 0
		# for r in range(r_max):
		# 	for i in range(n_candidate_stops):
		# 		for x in range(n_candidate_stops):
		# 			for j in range(n_candidate_stops):
		# 				cns.append('XD1(%d)' % idx)
		# 				cs.append((
		# 					linex([(vars.xD[r][i][x][j],  1),
		# 						   (vars.D[r][x][j],     -1)]),
		# 					'L', 0, 0))

		# 				cns.append('XD2(%d)' % idx)
		# 				cs.append((
		# 					linex([(vars.xD[r][i][x][j],    1),
		# 						   (vars.x[r][i+1][x+1], -100)]),
		# 					'L', 0, 0))

		# 				cns.append('XD3(%d)' % idx)
		# 				cs.append((
		# 					linex([(vars.xD[r][i][x][j],    1),
		# 						   (vars.D[r][x][j],       -1),
		# 						   (vars.x[r][i+1][x+1], -100)]),
		# 					'G', -100, 0))

		# 				idx += 1

		# max_T = sum(list(map(lambda x: ridetime(x[0], x[1]), combinations(candidate_stops, 2))))

		# idx = 0
		# for r in range(r_max):
		# 	for i in range(n_candidate_stops):
		# 		for j in range(n_candidate_stops):
		# 			cns.append('SD1(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.sD[r][i][j],      1),
		# 					   (vars.D[r][i][j],  -max_T)]),
		# 				'L', 0, 0))

		# 			cns.append('SXD2(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.sD[r][i][j],        1),
		# 					   (vars.D[r][i][j],  -1/max_T)]),
		# 				'G', 0, 0))

		# 			idx += 1

		# cns.append('TS')
		# cs.append((
		# 	linex([(vars.D[r][i][j], -1)
		# 		   for r in range(r_max)
		# 		   for i in range(n_candidate_stops)
		# 		   for j in range(n_candidate_stops)] + 
		# 		  [(vars.sD[r][i][j], walktime(candidate_stops[i], candidate_stops[j]))
		# 		   for r in range(r_max)
		# 		   for i in range(n_candidate_stops)
		# 		   for j in range(n_candidate_stops)] + 
		# 		  [(vars.ts[0], -1)]),
		# 	'E', 0, 0))

		# for i in range(n_candidate_stops):
		# 	cns.append('STP(%d)' % i)
		# 	cs.append((
		# 		linex([(vars.st[i], 1)] + 
		# 			  [(vars.x[r][i+1][j], -1)
		# 			   for r in range(r_max)
		# 			   for j in range(n_candidate_stops+1)]),
		# 		'L', 0, 0))

		# idx = 0
		# for r in range(r_max):
		# 	cns.append('ITS(%d)' % idx)
		# 	cs.append((
		# 		linex([(vars.x[r][i+1][j+1], 1)
		# 			   for i in range(n_candidate_stops)
		# 			   for j in range(n_candidate_stops)
		# 			   if i != j]),
		# 		'L', 1, 0))
		# 	idx += 1

		# idx = 0
		# for r in range(r_max):
		# 	for i in range(n_candidate_stops + 1):
		# 		for j in range(n_candidate_stops + 1):
		# 			cns.append('XF1(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.xf[r][i][j],  1),
		# 					   (vars.f[r],        -1)]),
		# 				'L', 0, 0))

		# 			cns.append('XF2(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.xf[r][i][j],   1),
		# 					   (vars.x[r][i][j],  -60)]),
		# 				'L', 0, 0))

		# 			cns.append('XF3(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.xf[r][i][j],   1),
		# 					   (vars.f[r],         -1),
		# 					   (vars.x[r][i][j],  -60)]),
		# 				'G', -60, 0))

		# 			idx += 1



		for r in range(r_max):
			cns.append('C1(%d)' % r)
			cs.append((
				linex([(vars.x[r][0][j], 1)
					   for j in range(n_candidate_stops+1)]),
				'E', 1, 0))

		# for r in range(r_max):
		# 	cns.append('C2(%d)' % r)
		# 	cs.append((
		# 		linex([(vars.x[r][i][0], 1)
		# 			   for i in range(n_candidate_stops+1)]),
		# 		'E', 1, 0))

		idx = 0
		for r in range(r_max):
			for j in range(n_candidate_stops):
				cns.append('C3(%d)' % idx)
				cs.append((
					linex([(vars.x[r][i][j],  1)
						   for i in range(n_candidate_stops + 1)
						   if i != j] + 
						  [(vars.x[r][j][i], -1)
						   for i in range(n_candidate_stops + 1)
						   if i != j]),
					'E', 0, 0))

				idx += 1

		idx = 0
		for r in range(r_max):
			for j in range(n_candidate_stops):
				cns.append('C4(%d)' % idx)
				cs.append((
					linex([(vars.x[r][i][j+1], 1)
						   for i in range(n_candidate_stops + 1)
						   if i != (j+1)]),
					'L', 1, 0))

		idx = 0
		for r in range(r_max):
			for i in range(n_candidate_stops):
				cns.append('C5(%d)' % idx)
				cs.append((
					linex([(vars.x[r][i+1][j], 1)
						   for j in range(n_candidate_stops + 1)
						   if (i+1) != j]),
					'L', 1, 0))

		idx = 0
		for r in range(r_max):
			for i in range(n_candidate_stops):
				cns.append('C6(%d)' % idx)
				cs.append((linex([(vars.x[r][i+1][i+1], 1)]), 'E', 0, 0))

		# idx = 0
		# for r in range(r_max):
		# 	for i in range(n_candidate_stops + 1):
		# 		for j in range(n_candidate_stops + 1):
		# 			cns.append('C7-1(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.zxf[r][i][j],  1),
		# 					   (vars.xf[r][i][j],  -1)]),
		# 				'L', 0, 0))

		# 			cns.append('C7-2(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.zxf[r][i][j],  1),
		# 					   (vars.x[r][0][0],   60)]),
		# 				'L', 60, 0))

		# 			cns.append('C7-3(%d)' % idx)
		# 			cs.append((
		# 				linex([(vars.zxf[r][i][j],  1),
		# 					   (vars.xf[r][i][j],  -1),
		# 					   (vars.x[r][0][0],   60)]),
		# 				'G', 0, 0))

		# 			idx += 1

		# cns.append('C8')
		# cs.append((
		# 	linex([(vars.zxf[r][i+1][j+1], 2*costs[i+1][j+1])
		# 		   for r in range(r_max)
		# 		   for i in range(n_candidate_stops)
		# 		   for j in range(n_candidate_stops)]),
		# 	'L', n_buses, 0, 0))

		# for r in range(r_max):
		# 	cns.append('C9(%d)' % r)
		# 	cs.append((
		# 		linex([(vars.x[r][i+1][j+1], costs[i+1][j+1])
		# 			   for i in range(n_candidate_stops)
		# 			   for j in range(n_candidate_stops)
		# 			   if i != j] + 
		# 			  [(vars.T[r], -1)]),
		# 		'E', 0, 0))

		# Stops for demand origin/destination
		idx = 0
		for p in range(len(demands)):
			for q in range(len(demands)):
				for c1 in range(n_candidate_stops):
					for c2 in range(n_candidate_stops):
						cns.append('S1(%d)' % idx)		
						cs.append((
							linex([(vars.s[p][q][c1][c2],  1),
								   (vars.adm[c1+1][c2+1], -1)]),
							'L', 0, 0))

						idx += 1

		# Stop for demand origin
		idx = 0
		for p in range(len(demands)):
			for q in range(len(demands)):
				cns.append('S2(%d)' % idx)		
				cs.append((
					linex([(vars.s[p][q][c1][c2], 1)
						   for c1 in range(n_candidate_stops)
						   for c2 in range(n_candidate_stops)]),
					'E', 1, 0))

				idx += 1

		# # Sub tour elimination
		# idx = 0
		# for r in range(r_max):
		# 	for i in range(n_candidate_stops):
		# 		for j in range(n_candidate_stops):
		# 			if i != j:
		# 				cns.append('SUB(%d)' % idx)
		# 				cs.append((
		# 					linex([(vars.q[r][i],            1),
		# 						   (vars.q[r][j],           -1),
		# 						   (vars.x[r][i+1][j+1], 1000)]),
		# 					'L', 999, 0))

		# 				idx += 1

		# Admissibility
		idx = 0
		for i in range(n_candidate_stops+1):
			for j in range(n_candidate_stops+1):
				cns.append('ADM(%d)' % idx)
				cs.append((
					linex([(vars.adm[i][j], 1)] + 
						  [(vars.x[r][i][j], -1)
						   for r in range(r_max)] + 
						  [(vars.xadm[r][i][x][j], -1)
						   for r in range(r_max)
						   for x in range(n_candidate_stops + 1)]),
					'L', 0, 0))


		# XADM
		idx = 0
		for r in range(r_max):
			for i in range(n_candidate_stops+1):
				for j in range(n_candidate_stops+1):
					for x in range(n_candidate_stops+1):
						cns.append('XADM1(%d)' % idx)
						cs.append((
							linex([(vars.xadm[r][i][x][j],  1),
								   (vars.x[r][i][x],       -1)]),
							'L', 0, 0))

						cns.append('XADM2(%d)' % idx)
						cs.append((
							linex([(vars.xadm[r][i][x][j],  1),
								   (vars.adm[x][j],        -1)]),
							'L', 0, 0))

						cns.append('XADM3(%d)' % idx)
						cs.append((
							linex([(vars.xadm[r][i][x][j],  1),
								   (vars.adm[x][j],        -1),
								   (vars.x[r][i][x],       -1)]),
							'G', -1, 0))

						idx += 1

		idx = 0
		for p in range(len(demands)):
			for q in range(len(demands)):
				cns.append("WAL(%d)" % idx)
				cs.append((
					linex([(vars.w[p][q], 1)] +
  						  [(vars.s[p][q][c1][c2], -walktime(demand_centroids[p], candidate_stops[c1])-walktime(demand_centroids[p], candidate_stops[c2]))
						   for c1 in range(n_candidate_stops)
						   for c2 in range(n_candidate_stops)]),
					'E', 0, 0))

		idx = 0
		for r in range(r_max):
			for c2 in range(n_candidate_stops):
				cns.append("DEL(%d)" % idx)
				cs.append((
					linex([(vars.delta[r][c2],     1)] + 
						  [(vars.x[r][i+1][c2+1], -1)
						   for i in range(n_candidate_stops)]),
					'E', 0, 0))

				idx += 1

		idx = 0
		for r in range(r_max):
			for c2 in range(n_candidate_stops):
				cns.append("DELF1(%d)" % idx)
				cs.append((
					linex([(vars.deltaf[r][c2],  1),
						   (vars.f[r],          -1)]),
					'L', 0, 0))

				cns.append("DELF2(%d)" % idx)
				cs.append((
					linex([(vars.deltaf[r][c2],   1), 
						   (vars.delta[r][c2],  -60)]),
					'L', 0, 0))

				cns.append("DELF3(%d)" % idx)
				cs.append((
					linex([(vars.deltaf[r][c2],   1),
						   (vars.f[r],           -1),
						   (vars.delta[r][c2],  -60)]),
					'G', -60, 0))

				idx += 1

		idx = 0
		for c in range(n_candidate_stops):
			cns.append("CAP(%d)" % idx)
			cs.append((
				linex([(vars.deltaf[r][c],            40)
					   for r in range(r_max)] + 
					  [(vars.s[p][q][c1][c], -demands[p][q]/15.0)
					   for c1 in range(n_candidate_stops)
					   for p in range(len(demands))
					   for q in range(len(demands[0]))]),
				'G', 0, 0))

			idx += 1

		# idx = 0
		# for r in range(r_max):
		# 	cns.append("FRQ(%d)" % idx)
		# 	cs.append((
		# 		linex([(vars.f[r],           1),
		# 			   (vars.x[r][0][0], 0.033)]),
		# 		'G', 0.033, 0))

		# 	idx += 1

		cs = zip(*cs)

		self._cp.linear_constraints.add(
            lin_expr=cs[0],
            senses=cs[1],
            rhs=cs[2],
            range_values=cs[3])

		self._cp.linear_constraints.set_names(enumerate(cns))