Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 7.90 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math/rand"
  6.     "sync"
  7.     "time"
  8. )
  9.  
  10. type ControlLogic struct {
  11.     capacityPeople   int
  12.     capacityElevator int
  13.     levels           int
  14.     elevators        int
  15.     runtime          float64
  16.     simulations      int
  17.     function         int
  18.     functions        []interface{}
  19.     building         Building
  20. }
  21.  
  22. type Building struct {
  23.     name      string
  24.     people    []Person
  25.     levels    []Level
  26.     elevators []Elevator
  27.     muTex     sync.Mutex
  28. }
  29.  
  30. type Level struct {
  31.     identifier    int
  32.     waitingPeople []Person
  33. }
  34.  
  35. type Elevator struct {
  36.     identifier   int
  37.     capacity     int
  38.     distance     int
  39.     currentLevel int
  40.     goingToLevel int
  41.     people       []Person
  42. }
  43.  
  44. type Person struct {
  45.     waitingOnLevel int
  46.     wantingToLevel int
  47.     timeWaited     int
  48.     timeSpend      int
  49. }
  50.  
  51. var counter int
  52.  
  53. func main() {
  54.     central := ControlLogic{}
  55.     central.centralControlLogic()
  56.     central.controlLogic()
  57. }
  58.  
  59. func (central *ControlLogic) centralControlLogic() {
  60.     central.capacityPeople = 5
  61.     central.capacityElevator = 10
  62.     central.levels = 4
  63.     central.elevators = 1
  64.     central.runtime = 0.2
  65.     central.simulations = 1
  66.     central.function = 0
  67.  
  68.     counter = central.capacityPeople
  69. }
  70.  
  71. func (central *ControlLogic) controlLogic() {
  72.     personChannel := make(chan Person)
  73.     elevatorChannels := make([]chan Elevator, central.elevators)
  74.  
  75.     central.functions = append(central.functions, LongestWaitingorWanting)
  76.     central.functions = append(central.functions, LongestWanting)
  77.     central.building = Building{
  78.         name:      "FH-AACHEN",
  79.         people:    []Person{},
  80.         levels:    []Level{},
  81.         elevators: []Elevator{},
  82.     }
  83.     for i := 0; i < central.elevators; i++ {
  84.         tmpElevator := Elevator{
  85.             identifier: i,
  86.             capacity:   central.capacityElevator,
  87.             people:     []Person{},
  88.         }
  89.         central.building.elevators = append(central.building.elevators, tmpElevator)
  90.     }
  91.     for i := 0; i < central.levels; i++ {
  92.         tmpLevel := Level{
  93.             identifier:    i,
  94.             waitingPeople: []Person{},
  95.         }
  96.         central.building.levels = append(central.building.levels, tmpLevel)
  97.     }
  98.  
  99.     central.elevator(elevatorChannels)
  100.     go central.person(personChannel)
  101.  
  102.     for counter != 0 {
  103.         select {
  104.         case person := <-personChannel:
  105.             central.building.people = append(central.building.people, person)
  106.             central.building.levels[person.waitingOnLevel].waitingPeople = append(central.building.levels[person.waitingOnLevel].waitingPeople, person)
  107.             fmt.Println("new person waiting on Floor", person.waitingOnLevel, "who wants to", person.wantingToLevel)
  108.         default:
  109.             for _, elevatorChannel := range elevatorChannels {
  110.                 select {
  111.                 case elevator := <-elevatorChannel:
  112.                     central.functions[central.function].(func(*Elevator, *ControlLogic))(&elevator, central)
  113.                 default:
  114.                     continue
  115.                 }
  116.             }
  117.  
  118.         }
  119.     }
  120.  
  121. }
  122.  
  123. func (central *ControlLogic) person(personChannel chan<- Person) {
  124.     for i := 0; i < central.capacityPeople; i++ {
  125.         go func() {
  126.             rand.Seed(time.Now().UTC().UnixNano())
  127.             person := Person{
  128.                 timeWaited:     1,
  129.                 waitingOnLevel: rand.Intn(4),
  130.             }
  131.             for {
  132.                 rand.Seed(time.Now().UTC().UnixNano())
  133.                 person.wantingToLevel = rand.Intn(4)
  134.                 if person.waitingOnLevel == person.wantingToLevel {
  135.                     continue
  136.                 } else {
  137.                     break
  138.                 }
  139.             }
  140.             personChannel <- person
  141.         }()
  142.     }
  143. }
  144.  
  145. func (central *ControlLogic) elevator(elevatorChannels []chan Elevator) {
  146.     for i := range elevatorChannels {
  147.         ii := i
  148.         go func(central *ControlLogic) {
  149.             elevatorChannels[ii] = make(chan Elevator)
  150.             for {
  151.                 elevator := &central.building.elevators[ii]
  152.  
  153.                 tempElevatorPeople := []Person{}
  154.                 if len(elevator.people) != 0 {
  155.                     for _, person := range elevator.people {
  156.                         if elevator.currentLevel == person.wantingToLevel {
  157.                             fmt.Println("person who wanted to level", person.wantingToLevel, "left the elevator #", elevator.identifier)
  158.                             counter--
  159.                         } else {
  160.                             person.timeSpend = 1
  161.                             tempElevatorPeople = append(tempElevatorPeople, person)
  162.                         }
  163.                     }
  164.                     elevator.people = tempElevatorPeople
  165.                 }
  166.  
  167.                 for len(elevator.people) < elevator.capacity {
  168.                     central.building.muTex.Lock()
  169.                     if len(central.building.levels[elevator.currentLevel].waitingPeople) != 0 {
  170.                         longestWaiter := central.building.levels[elevator.currentLevel].waitingPeople[0]
  171.                         central.building.levels[elevator.currentLevel].waitingPeople = central.building.levels[elevator.currentLevel].waitingPeople[1:]
  172.                         elevator.people = append(elevator.people, longestWaiter)
  173.                         fmt.Println("person who wants to", longestWaiter.wantingToLevel, "went in the elevator #", elevator.identifier)
  174.                         central.building.muTex.Unlock()
  175.                     } else {
  176.                         central.building.muTex.Unlock()
  177.                         break
  178.                     }
  179.  
  180.                 }
  181.  
  182.                 if elevator.currentLevel < elevator.goingToLevel {
  183.                     elevator.currentLevel++
  184.                     elevator.distance++
  185.                     fmt.Println("Elevator #", elevator.identifier, "is now on", elevator.currentLevel)
  186.                 } else if elevator.currentLevel > elevator.goingToLevel {
  187.                     elevator.currentLevel--
  188.                     elevator.distance++
  189.                     fmt.Println("Elevator #", elevator.identifier, "is now on", elevator.currentLevel)
  190.                 } else {
  191.                     elevatorChannels[ii] <- *elevator
  192.                 }
  193.             }
  194.         }(central)
  195.     }
  196. }
  197.  
  198. // LongestWaitingorWanting : Hallo
  199. func LongestWaitingorWanting(elevator *Elevator, central *ControlLogic) {
  200.     longestWaitingorWantingPerson := Person{}
  201.     if len(elevator.people) == 0 {
  202.         for _, level := range central.building.levels {
  203.             for _, person := range level.waitingPeople {
  204.                 if person.timeWaited > longestWaitingorWantingPerson.timeWaited {
  205.                     longestWaitingorWantingPerson = person
  206.                 }
  207.             }
  208.         }
  209.         central.building.elevators[elevator.identifier].goingToLevel = longestWaitingorWantingPerson.waitingOnLevel
  210.     } else {
  211.         for _, person := range elevator.people {
  212.             if person.timeSpend >= longestWaitingorWantingPerson.timeSpend {
  213.                 longestWaitingorWantingPerson = person
  214.             }
  215.         }
  216.         central.building.elevators[elevator.identifier].goingToLevel = longestWaitingorWantingPerson.wantingToLevel
  217.     }
  218.  
  219.     if elevator.currentLevel != central.building.elevators[elevator.identifier].goingToLevel {
  220.         fmt.Println("Elevator #", elevator.identifier, "is now on level", elevator.currentLevel, "and is going to level", central.building.elevators[elevator.identifier].goingToLevel)
  221.     }
  222.  
  223. }
  224.  
  225. // LongestWanting : Hallo
  226. func LongestWanting(elevator *Elevator, central *ControlLogic) {
  227.     longestWantingPerson := Person{}
  228.     for _, person := range elevator.people {
  229.         if person.timeSpend > longestWantingPerson.timeSpend {
  230.             longestWantingPerson = person
  231.         }
  232.     }
  233.     elevator.goingToLevel = longestWantingPerson.wantingToLevel
  234.     if len(elevator.people) == 0 {
  235.         for _, level := range central.building.levels {
  236.             for _, person := range level.waitingPeople {
  237.                 if person.timeWaited > longestWantingPerson.timeWaited {
  238.                     longestWantingPerson = person
  239.                 }
  240.             }
  241.         }
  242.         elevator.goingToLevel = longestWantingPerson.waitingOnLevel
  243.     }
  244.     fmt.Println("Elevator #", elevator.identifier, "is now on level", elevator.currentLevel, "and is going to level", elevator.goingToLevel)
  245. }
  246.  
  247. /* func (central *ControlLogic) toString() {
  248.     fmt.Println()
  249.     fmt.Println("Name:\t", central.building.name)
  250.     fmt.Println("Capacity:\t", central.building.capacity)
  251.     fmt.Println()
  252.     fmt.Println("Levels:")
  253.     for _, level := range central.building.levels {
  254.         fmt.Println("\tIdentifier:\t", level.identifier)
  255.         fmt.Println("\tWaiting people:\t", level.waitingPeople)
  256.         fmt.Println()
  257.     }
  258.     fmt.Println("Elevators:")
  259.     for _, elevator := range central.building.elevators {
  260.         fmt.Println("\tIdentifier:\t", elevator.identifier)
  261.         fmt.Println("\tCapacity:\t", elevator.capacity)
  262.         fmt.Println("\tDistance:\t", elevator.distance)
  263.         fmt.Println("\tPeople:\t", elevator.people)
  264.         fmt.Println()
  265.     }
  266.     fmt.Println("People:")
  267.     for _, human := range central.building.people {
  268.         fmt.Println("\tTime waited:\t", human.timeWaited)
  269.         fmt.Println("\tTime spend:\t", human.timeSpend)
  270.         fmt.Println()
  271.     }
  272. } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement