Esempio n. 1
0
    def _set_up_variables(self):
        _mnm_temp_store = self.dump_structure.mnm_temp_store
        _move_trips_pairs = _mnm_temp_store.move_trips_pairs

        variables = []
        costs = []

        for _bus in self.dump_structure.all_buses():
            for _, trip in enumerate(self.dump_structure.filtered_trips):
                variables.append(combine_key([_bus, trip]))
                costs.append(cost(_bus, trip))

        for _bus in self.dump_structure.all_buses():
            for _, (_trip_1, _trip_2) in enumerate(_move_trips_pairs):
                if is_electric(_bus) or (
                        is_gas(_bus) and isinstance(_trip_1, OperatingTrip)
                        and isinstance(_trip_2, OperatingTrip)):
                    variables.append(combine_key([_bus, _trip_1, _trip_2]))
                    costs.append(get_mov_cost(_trip_1, _trip_2, _bus.bus_type))

        types = [self.variables.type.binary for i, _ in enumerate(variables)]
        ub = [1 for i, _ in enumerate(variables)]
        lb = [0 for i, _ in enumerate(variables)]

        return CustomCPLEXVariable(variables, types, lb, ub, costs)
 def _write_assign(self, prefix):
     variables = self.variables.get_names()
     x = self.solution.get_values()
     _filtered_trips = self.dump_structure.filtered_trips
     _buses = self.dump_structure.all_buses()
     assign_heading = trip_heading_prefix
     result_assign = open(output_directory + prefix + "results_assign.csv",
                          "w+")
     for _bus in _buses:
         assign_heading += _bus.__str__() + ", "
     assign_heading += "\n"
     result_assign.write(assign_heading)
     for trip in _filtered_trips:
         if isinstance(trip, OperatingTrip):
             assign_content = trip.__content__()
             for _bus in _buses:
                 key = combine_key([_bus, trip])
                 temp_value = 0
                 if key in variables:
                     temp_value = x[variables.index(key)]
                 assign_content += str(temp_value) + ","
             assign_content += "\n"
             result_assign.write(assign_content)
             result_assign.flush()
             os.fsync(result_assign.fileno())
     result_assign.close()
 def _write_charge(self, prefix):
     variables = self.variables.get_names()
     x = self.solution.get_values()
     _buses = self.dump_structure.ev_buses
     _charging = self.dump_structure.charging
     _charge_file_name = output_directory + prefix + "results_charge.csv"
     result_charge = open(_charge_file_name, "w+")
     charge_heading = charging_heading_prefix
     for _bus in _buses:
         if is_electric(_bus):
             charge_heading += _bus.__str__() + ", "
     charge_heading += "\n"
     result_charge.write(charge_heading)
     added_at_least_once = False
     for i, selected_charge in enumerate(_charging):
         add_this_content = False
         charge_content = selected_charge.__content__()
         for _bus in _buses:
             if is_electric(_bus):
                 key = combine_key([_bus, selected_charge])
                 temp_value = 0
                 if key in variables:
                     temp_value = x[variables.index(key)]
                     if temp_value > 0:
                         add_this_content = True
                         added_at_least_once = True
                 charge_content += str(temp_value) + ","
         charge_content += "\n"
         if add_this_content:
             result_charge.write(charge_content)
             result_charge.flush()
             os.fsync(result_charge.fileno())
     result_charge.close()
     if not added_at_least_once:
         os.remove(_charge_file_name)
    def _write_move(self, prefix):
        variables = self.variables.get_names()
        x = self.solution.get_values()
        _buses = self.dump_structure.all_buses()
        _all_trips = self.get_all_trips()
        result_move = open(output_directory + prefix + "results_move.csv",
                           "w+")
        move_heading = trip_heading_prefix
        for _bus in _buses:
            move_heading += _bus.__str__() + ", "
        move_heading += "\n"
        result_move.write(move_heading)

        for trip_1, trip_2 in self.dump_structure.mnm_temp_store.move_trips_pairs:
            _moving_trip = create_mov_trip(trip_1, trip_2)
            move_content = _moving_trip.__content__()
            for _bus in _buses:
                if movable(trip_1, trip_2) and _moving_trip.route.distance > 0:
                    key = combine_key([_bus, trip_1, trip_2])
                    temp_value = 0
                    if key in variables:
                        temp_value = x[variables.index(key)]
                    move_content += str(temp_value) + ","
            move_content += "\n"
            result_move.write(move_content)
            result_move.flush()
            os.fsync(result_move.fileno())
        result_move.close()
Esempio n. 5
0
    def _set_up_variables(self):
        _mnm_temp_store = self.dump_structure.mnm_temp_store
        _move_trips_pairs = _mnm_temp_store.move_trips_pairs

        _custom_var = super(CustomCPLEXWTC, self)._set_up_variables()
        variables = _custom_var.variables
        types = _custom_var.types
        lb = _custom_var.lb
        ub = _custom_var.ub
        costs = _custom_var.costs

        _count = 0
        for _bus in self.dump_structure.ev_buses:
            for _charging in self.dump_structure.charging:
                variables.append(combine_key([_bus, _charging]))
                _count += 1
        types.extend([self.variables.type.binary for _ in range(_count)])
        costs.extend([0 for _ in range(_count)])
        lb.extend([0 for _ in range(_count)])
        ub.extend([1 for _ in range(_count)])

        _ac_count = 0
        slots = create_slots(self.dump_config.slot_duration)
        for _bus in self.dump_structure.ev_buses:
            for i, _slot in enumerate(slots):
                _bs_key = combine_key([_bus, _slot])
                variables.append("CA" + _bs_key)
                _ac_count += 1

        types.extend(
            [self.variables.type.continuous for _ in range(_ac_count)])
        costs.extend([0 for _ in range(_ac_count)])
        lb.extend([0 for _ in range(_ac_count)])
        ub.extend([electric_bus_type.capacity for _ in range(_ac_count)])

        return CustomCPLEXVariable(variables, types, lb, ub, costs)
 def write_summary(self):
     objective, status, gap = self.get_obj_status()
     variables = self.variables.get_names()
     x = self.solution.get_values()
     _buses = self.dump_structure.all_buses()
     _all_trips = self.get_all_trips()
     assign_pairs = {}
     for i, selected_trip in enumerate(_all_trips):
         for _bus in _buses:
             key = combine_key([_bus, selected_trip])
             if key in variables:
                 trip_assign_value = x[variables.index(key)]
                 if trip_assign_value == 1.0:
                     time_in_sec = selected_trip.start_s()
                     assigns = []
                     if time_in_sec in assign_pairs.keys():
                         assigns = assign_pairs[time_in_sec]
                     assigns.append((selected_trip, _bus))
                     assign_pairs[time_in_sec] = assigns
     fu_assign = ForcedUpdateBase(dump_structure=self.dump_structure)
     fu_assign.force_update(assign_pairs)
     self.assignment = fu_assign.assignment
     return objective, status, gap
Esempio n. 7
0
    def _set_up_constraints(self):
        _buses = self.dump_structure.all_buses()
        _filtered_trips = self.dump_structure.filtered_trips
        _mnm_temp_store = self.dump_structure.mnm_temp_store
        row_names = []
        senses = []
        rhs = []
        row_constraints = []
        '''
        constraints : sum (a_{v,t}) >= 1
        conditions: t | T, v | V
        '''

        for trip in _filtered_trips:
            row_names.append(str("A" + str(self._no_of_const)))
            senses.append("G")
            rhs.append(1)
            pair = cplex.SparsePair(ind=[
                combine_key([_bus, trip]) for i, _bus in enumerate(_buses)
            ],
                                    val=[1 for i, _ in enumerate(_buses)])
            row_constraints.append(pair)
            self._no_of_const += 1
        '''
        constraints : a_{v,t1} + a_{v,t2} <= 1
        conditions: if ~Feasible, t1,t2 | T, v | V
        '''

        for _bus in self.dump_structure.all_buses():
            for _trip_1, _trip_2 in _mnm_temp_store.n_move_trips_pairs:
                gas_c = isinstance(_trip_1, OperatingTrip) and isinstance(
                    _trip_2, OperatingTrip)
                if is_electric(_bus) or (is_gas(_bus) and gas_c):
                    row_names.append(str("N" + str(self._no_of_const)))
                    senses.append("L")
                    rhs.append(1)
                    assign_1_key = combine_key([_bus, _trip_1])
                    assign_2_key = combine_key([_bus, _trip_2])
                    pair = cplex.SparsePair(ind=[assign_1_key, assign_2_key],
                                            val=[1, 1])
                    row_constraints.append(pair)
                    self._no_of_const += 1
        '''
        constraints : m_{v,t1,t2} - a_{v,t1} - a_{v,t2} + sum (a_{v,t}) >= -1
        conditions: if Feasible, t1,t2 | T; v | V; t | T[t1:t2];
        '''

        _mnm_temp_store = self.dump_structure.mnm_temp_store
        for _bus in self.dump_structure.all_buses():
            for _trip_1, _trip_2 in _mnm_temp_store.move_trips_pairs:
                if is_electric(_bus) or \
                        (is_gas(_bus) and isinstance(_trip_1, OperatingTrip)
                         and isinstance(_trip_2, OperatingTrip)):
                    all_trips = self.get_all_trips()
                    row_names.append(str("M" + str(self._no_of_const)))
                    senses.append("G")
                    rhs.append(-1)
                    assign_1_key = combine_key([_bus, _trip_1])
                    assign_2_key = combine_key([_bus, _trip_2])
                    move_trip_key = combine_key([_bus, _trip_1, _trip_2])

                    ind_temp = [assign_1_key, assign_2_key, move_trip_key]
                    val_temp = [-1, -1, 1]
                    all_trips = sorted(all_trips,
                                       key=lambda a_trip: a_trip.start_s())
                    i = all_trips.index(_trip_1)
                    j = all_trips.index(_trip_2)
                    if abs(j - i) > 1:
                        sub_trips = all_trips[i + 1:j]
                        sub_trips = sorted(sub_trips,
                                           key=lambda s_trip: s_trip.start_s())
                        for _trip in sub_trips:
                            if is_in_between(_trip_1, _trip, _trip_2):
                                if is_electric(_bus) or (
                                        is_gas(_bus)
                                        and isinstance(_trip, OperatingTrip)):
                                    trip_key = combine_key([_bus, _trip])
                                    ind_temp.append(trip_key)
                                    val_temp.append(1)
                    pair = cplex.SparsePair(ind=ind_temp.copy(),
                                            val=val_temp.copy())
                    row_constraints.append(pair)
                    self._no_of_const += 1
        return CustomCPLEXConstraint(row_names, senses, rhs, row_constraints)
Esempio n. 8
0
    def _set_up_constraints(self):
        _mnm_temp_store = self.dump_structure.mnm_temp_store
        _bus_slot_val_store = self.dump_structure.bus_slot_val_store
        slots = create_slots(self.dump_config.slot_duration)

        constraint = super(CustomCPLEXWTC, self)._set_up_constraints()
        row_names = constraint.row_names
        senses = constraint.senses
        rhs = constraint.rhs
        row_constraints = constraint.row_constraints
        '''
        constraints : sum (a_{v,c}) <= 1
        conditions : c | C, v | V, is_electric(v)
        '''

        for _charging in self.dump_structure.charging:
            ind_temp = []
            val_temp = []
            for _bus in self.dump_structure.ev_buses:
                key = combine_key([_bus, _charging])
                ind_temp.append(key)
                val_temp.append(1)

            if len(ind_temp) > 0:
                row_names.append(str("CS" + str(self._no_of_const)))
                senses.append("L")
                rhs.append(1)
                pair = cplex.SparsePair(ind=ind_temp.copy(),
                                        val=val_temp.copy())
                row_constraints.append(pair)
                self._no_of_const += 1
        '''
        constraints : sum (a_{v,c} · P) - c_{v,s} => 0
        conditions : c | C, s | S is_in_slot(c, s); v | V, is_electric(v);
        '''

        for i, slot in enumerate(slots):
            _s_key = combine_key([slot])
            bs_charge_temp_store = _bus_slot_val_store.charge_temp_store
            bs_charge_key_list = bs_charge_temp_store.charge_key_list[_s_key]
            bs_charge_energies = bs_charge_temp_store.charge_energies[_s_key]

            for _bus in self.dump_structure.ev_buses:
                _bs_key = combine_key([_bus, slot])

                if is_electric(_bus):

                    charge_ind_temp = []
                    charge_val_temp = []

                    for charge_key in bs_charge_key_list:
                        charge_key_custom = combine_key([_bus, charge_key])
                        charge_ind_temp.append(charge_key_custom)
                        charge_val_temp.append(bs_charge_energies[charge_key])

                    charge_ind_temp.append("CA" + combine_key([_bus, slot]))
                    charge_val_temp.append(-1)

                    row_names.append(str("CC" + str(self._no_of_const)))
                    senses.append("G")
                    rhs.append(0)
                    pair = cplex.SparsePair(ind=charge_ind_temp.copy(),
                                            val=charge_val_temp.copy())
                    row_constraints.append(pair)
                    self._no_of_const += 1
        '''
        constraints :  0 <=  sum (a_{v,t} * E) + sum (m_{v, t1, t2} * E) - c_{v,s} <= capacity
        conditions : c | C, s | S, is_in_slot(c, s); t, t1, t2 | T; v | V, is_electric(v);
        '''
        for _bus in self.dump_structure.ev_buses:
            ind_temp = []
            val_temp = []

            bs_glb_temp_store = _bus_slot_val_store
            bs_assign_temp_store = bs_glb_temp_store.assign_temp_store
            bs_move_temp_store = bs_glb_temp_store.move_temp_store

            for i, slot in enumerate(slots):

                _s_key = combine_key([slot])
                bs_assign_key_list = bs_assign_temp_store.assign_key_list[
                    _s_key]
                bs_assign_energies = bs_assign_temp_store.assign_energies[
                    _s_key]
                bs_move_key_list = bs_move_temp_store.move_key_list[_s_key]
                bs_move_energies = bs_move_temp_store.move_energies[_s_key]

                _ca_bs_key = "CA" + combine_key([_bus, slot])

                if is_electric(_bus):
                    for assign_key in bs_assign_key_list:
                        assign_key_custom = combine_key([_bus, assign_key])
                        ind_temp.append(assign_key_custom)
                        val_temp.append(bs_assign_energies[assign_key])

                    for move_key in bs_move_key_list:
                        move_key_custom = combine_key([_bus, move_key])
                        ind_temp.append(move_key_custom)
                        val_temp.append(bs_move_energies[move_key])

                    ind_temp.append(_ca_bs_key)
                    val_temp.append(-1)

                    row_names.append(str("CL" + str(self._no_of_const)))
                    senses.append("L")
                    rhs.append(electric_bus_type.capacity)
                    pair = cplex.SparsePair(ind=ind_temp.copy(),
                                            val=val_temp.copy())
                    row_constraints.append(pair)
                    self._no_of_const += 1

                    row_names.append(str("CG" + str(self._no_of_const)))
                    senses.append("G")
                    rhs.append(0)
                    pair = cplex.SparsePair(ind=ind_temp.copy(),
                                            val=val_temp.copy())
                    row_constraints.append(pair)
                    self._no_of_const += 1

        return CustomCPLEXConstraint(row_names, senses, rhs, row_constraints)