fork download
  1. from optapy import constraint_provider
  2. from optapy.score import HardSoftScore
  3. from optapy.constraint import Joiners, ConstraintFactory
  4.  
  5. from domain import Reservation, Employee, Seat
  6.  
  7.  
  8. @constraint_provider
  9. def define_constraints(constraint_factory: ConstraintFactory):
  10. return [
  11. #HARD CONSTRAINTS
  12. seat_conflict(constraint_factory),
  13. one_reservation_per_date(constraint_factory),
  14. # employee_conflict(constraint_factory),
  15. #SOFT CONSTRAINTS
  16. prefered_seat(constraint_factory),
  17. team_seat(constraint_factory)
  18. ]
  19.  
  20. def seat_conflict(constraint_factory: ConstraintFactory):
  21. return constraint_factory.for_each_unique_pair(Reservation,
  22. Joiners.equal(lambda res: res.date),
  23. Joiners.equal(lambda res: res.seat)
  24. ).penalize("Seat conflict", HardSoftScore.ONE_HARD)
  25.  
  26. def one_reservation_per_date(constraint_factory: ConstraintFactory):
  27. return constraint_factory.for_each_unique_pair(Reservation,
  28. Joiners.equal(lambda res: res.employee),
  29. Joiners.equal(lambda res: res.date)
  30. ).penalize("employee_conflict", HardSoftScore.ONE_HARD)
  31.  
  32. def employee_conflict(constraint_factory: ConstraintFactory):
  33. return constraint_factory.for_each(Reservation).join(Employee,
  34. Joiners.equal(lambda res: res.employee),
  35. Joiners.equal(lambda res: res.date),
  36. Joiners.equal(lambda res: res.seat)
  37. ).penalize("employee_conflict", HardSoftScore.ONE_HARD)
  38.  
  39.  
  40. def prefered_seat(constraint_factory: ConstraintFactory):
  41. return constraint_factory.for_each(Reservation).join(Employee,
  42. Joiners.equal(lambda res: res.employee.uuid,
  43. lambda emp: emp.uuid),
  44. Joiners.equal(lambda res: res.seat.seat_id,
  45. lambda emp: emp.id_emplacement_pref)
  46. ).penalize("Prefered Seat Constraint", HardSoftScore.ONE_SOFT)
  47.  
  48. def team_seat(constraint_factory: ConstraintFactory):
  49. return constraint_factory.for_each(Reservation).join(Employee,
  50. Joiners.equal(lambda res: res.employee.uuid,
  51. lambda emp: emp.uuid),
  52. Joiners.equal(lambda res: res.seat.team_id,
  53. lambda emp: emp.idequipe)
  54. ).penalize("Team Seat Constraint", HardSoftScore.ONE_SOFT)
  55.  
Runtime error #stdin #stdout #stderr 0.16s 23408KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
ModuleNotFoundError: No module named 'optapy'