public Task FindAlmostNearestTask(Task fromTask, List<Task> tasks, double tolerance) { int minDistance = Int32.MaxValue; Task minTask = null; foreach (Task task in tasks) { //TODO check this is correct if(task == fromTask) continue; int distance = task.CostTo(fromTask); if (distance < minDistance && (minTask == null || _rand.Next(2) == 0)) { var adjustedDistance = (int)(distance * tolerance); if (adjustedDistance < minDistance) { minDistance = adjustedDistance; } minTask = task; } } return minTask; }
/// <summary> /// Used for cloning a task /// </summary> /// <param name="task">The task to clone</param> public Task(Task task) { Id = task.Id; Coordinate = task.Coordinate; UserId = task.UserId; Time = task.Time; Problem = task.Problem; }
/// <summary> /// Create tasks for a problem from a set of coordinates /// </summary> public static List<Task> GetTasks(IEnumerable<Coordinate> coordinates, Problem problem) { var tasks = new List<Task>(); int id = 0; foreach (var coordinate in coordinates) { var newTask = new Task(id, coordinate.lat, coordinate.lon) { Time = 30 * 60, Problem = problem }; tasks.Add(newTask); id++; } return tasks; }
public int CostTo(Task task) { if (task == null || task == this) { //TODO add depot task, and return cost from here to there return 1; } if (Problem.GetCachedCost(this.Id, task.Id) == 0) { //cache the calculation in memory //_mDistances[task.Id] = Problem.Osrm.GetDistanceTime(new Coordinate(Lat, Lon), new Coordinate(task.Lat, task.Lon))[0]; Problem.SetCachedCost(this.Id, task.Id, Problem.CostFunction.Calculate(this, task, true)); } return Problem.GetCachedCost(this.Id, task.Id); }
public int Calculate(Task origin, Task destination, bool considerTaskTime) { if (origin == null || destination == null || origin == destination) return 0; #if DEBUG if (origin.Problem != destination.Problem) throw new System.Exception("Tasks have to belong to the same problem!"); #endif // Get time & Distance int[] distanceTime = origin.Problem.Osrm.GetDistanceTime(origin.Coordinate, destination.Coordinate); // Calculate cost //miles / milesPerGallon * gallonGas var distCost = ((distanceTime[0] / 1609.34) / MilesPerGallon) * PricePerGallon; if (considerTaskTime) distanceTime[1] += (origin.Time + destination.Time)/2; var timeCost = (distanceTime[1] / 3600.0) * HourlyWage; var cost = (timeCost + distCost) * 100; return (int)cost; }
private int FindCheapestInsert(Task t1, Task t2, IEnumerable<Task> remainingTasks, out Task newTask) { int minCost = Int32.MaxValue; int constantCost = t1.CostTo(t2); newTask = null; foreach (Task task in remainingTasks) { int cost = t1.CostTo(task) + task.CostTo(t2); if (cost < minCost) { minCost = cost; newTask = task; } } return minCost - constantCost; }
private int FindNearestMean(Task task) { int minDistance = Int32.MaxValue; int minIndex = -1; int index = 0; foreach (Task mean in _means) { int cost = task.Problem.CostFunction.Calculate(task, mean, false); if (cost < minDistance) { minIndex = index; minDistance = cost; } index++; } return minIndex; }
private void ConfigureCrossoverData() { int maxTasks = _tasks.Count; if (_crossoverAddedTasks != null && _crossoverAddedTasks.Length >= maxTasks) { // enough space! return; } _crossoverAddedTasks = new bool[maxTasks]; _crossoverTaskNeighborMatrix = new bool[maxTasks][]; _crossoverTaskNeighbors = new Task[maxTasks][]; _crossoverTaskNeighborCount = new int[maxTasks]; for (int t = 0; t < maxTasks; t++) { _crossoverTaskNeighborMatrix[t] = new bool[maxTasks]; _crossoverTaskNeighbors[t] = new Task[4]; } }