def load():
    with open('smaller_schedule.json') as data:
        data = json.load(data)
        shifts = [
            Shift(day=s['day'],
                  start=datetime.strptime(s['startTime'], "%H:%M:%S"),
                  end=datetime.strptime(s['endTime'], "%H:%M:%S"),
                  location=s['location']) for s in data['shifts']
        ]
        employees = [
            Employee(cpr=e['cpr'],
                     name=e['name'],
                     min_hours=timedelta(hours=int(e['min_hours'])),
                     max_hours=timedelta(hours=int(e['max_hours'])),
                     locations=e['locations'],
                     time_constraint=e['time_constraint'])
            for e in data['employees']
        ]

        return shifts, employees
def automatic_shift_creation(latitude, longitude):
    # create the new shift
    new_shift = Shift(location_lat=latitude, location_long=longitude)
    db.session.add(new_shift)
    # instantiate the new shift. We need to get the ID to stamp our vehicles
    db.session.commit()
    # query for 20 nearest not in use/not fully charged vehicles
    target_data = db.session.execute(
        """SELECT *,
      SQRT(
        POW(69.1 * (location_lat - :lat), 2) +
        POW(69.1 * (location_long - :long) * COS(location_lat / 57.3), 2)
      ) AS distance
      FROM vehicle
      WHERE battery_level != 100.0
      AND shift_id IS NULL
      AND in_use = 'False'
      ORDER BY distance LIMIT 20""", {
            'lat': latitude,
            'long': longitude
        })

    vehicles = []
    for v in target_data:
        vehicles.append(
            Vehicle(
                id=v.id,
                license_plate=v.license_plate,
                battery_level=v.battery_level,
                in_use=v.in_use,
                model=v.model,
                location_lat=v.location_lat,
                location_long=v.location_long,
                shift_id=new_shift.id,
                created_at=v.created_at,
            ))

    path = PathFinder(vehicles, new_shift).initial_route[1:len(vehicles) + 1]
    # pathing logic for vehicles goes HERE
    # currently employing nearest neighbor heuristic, need to hook up the Two_opt solution for further accuracy
    # then iterate through the newly sorted/pathed vehicles

    # not enough time to implement two_opt confidently. Will do further research and go over during onsite, currently just using Nearest Neighbor
    if len(path) > 0:
        for i in range(0, len(path)):
            # set the Vehicle.next_id to be the next vehicle
            current_vehicle_index = path[i] - 1
            if i < len(path) - 1:
                db.session.query(Vehicle).filter(
                    Vehicle.id == vehicles[current_vehicle_index].id).update({
                        'next_id':
                        vehicles[path[i + 1] - 1].id,
                        'shift_id':
                        new_shift.id
                    })
            else:
                db.session.query(Vehicle).filter(
                    Vehicle.id == vehicles[current_vehicle_index].id).update(
                        {'shift_id': new_shift.id})
        # create the shift_index row
        new_link = ShiftIndex(shift_id=new_shift.id,
                              next_vehicle_id=vehicles[path[0]].id)
        db.session.add(new_link)

    # # commit all changes
    db.session.commit()
    return ShiftSchema().dump(new_shift)
def create_shift():
    new_shift = Shift()
    db.session.add(new_shift)
    db.session.commit()
    return ShiftSchema().dump(new_shift)
Example #4
0
File: Admin.py Project: ran632/SSM
from google.appengine.ext.webapp import template