func add_order_to_best_elev(rcvList map[rcvOrder][]rcvCost, newOrder rcvOrder) { if this_elevator_has_the_lowest_cost(rcvList[newOrder]) { //fmt.Printf("%sNew order added to floor = %d with cost = %d\n%s",def.ColB,helpFunc.Order_dir(newOrder.floor,newOrder.button),Cost(queue.Get_Orders(),helpFunc.Order_dir(newOrder.floor,newOrder.button)),def.ColN) queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), queue.Order_direction(newOrder.floor, newOrder.button))) //fmt.Printf("%sUpdated orders = %v\n%s\n",def.ColB,queue.Get_Orders(),def.ColN) queue.Save_backup_to_file() } }
func handle_new_order(buttontype, floor int, outgoingMsg chan def.Message) { if external_order(buttontype) && ImConnected { msg := def.Message{Category: def.NewOrder, Floor: floor, Button: buttontype, Cost: -1} outgoingMsg <- msg } else if !external_order(buttontype) { // Internal orders: if the desired floor is under the elevator it is set as a order down if floor == driver.Get_cur_floor() && driver.Get_dir() == 1 { queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), -floor)) } else if floor < driver.Get_cur_floor() { queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), -floor)) } else if floor == def.NumFloors-1 { queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), -floor)) } else { queue.Set_Orders(queue.Update_orderlist(queue.Get_Orders(), floor)) } queue.Save_backup_to_file() } }
func Cost(orderlist []int, newOrder int) int { new_orderlist := queue.Update_orderlist(orderlist, newOrder) index := helpFunc.Get_index(new_orderlist, newOrder) // Cost is initially the distance to first floor in list var cost = helpFunc.Difference_abs(driver.Get_cur_floor(), newOrder) // For every order in orderlist, the difference between two orders is // added to the cost if len(orderlist) > 0 { cost = helpFunc.Difference_abs(driver.Get_cur_floor(), orderlist[0]) for i := 0; i < index-1; i++ { cost += helpFunc.Difference_abs(orderlist[i], orderlist[i+1]) } } // The cost is penalized by the number of orders in the queue return int(cost) + 2*len(queue.Get_Orders()) }