-1

I'm stuck at something that seemed easy but became a headache pretty fast:

Here is a class that represent a structure I'm using:

public class LocumJobDistanceDifferenceObject {

    public LocumJobDistanceDifferenceObject(Int64 ALocumID, Int64 AJobID, Decimal ADistanceMiles, Int32 ARateDifference, Boolean AIsDistanceUnderMax) {
        LocumID = ALocumID;
        JobID = AJobID;
        DistanceMiles = ADistanceMiles;
        RateDifference = ARateDifference;
        IsDistanceUnderMax = AIsDistanceUnderMax;
    }

    public Int64 LocumID {
        get;
        set;
    }

    public Int64 JobID {
        get;
        set;
    }

    public Decimal DistanceMiles {
        get;
        set;
    }

    public Int32 RateDifference {
        get;
        set;
    }

    public Boolean IsDistanceUnderMax {
        get;
        set;
    }
}

I create a List to store a matrix of information. Locum is a worker and he needs to be placed at a Job. Lest say I have 50 Jobs and 75 Locums. I build my matrix by running a Locums x Jobs algo that stores LocumID + JobID + Detrmine DistanceMiles between Locum and Job + Determine Rate that Job pays/hour and Locum wants/hour + If dostance to Job exceeds Locum's max distance he/she willing to travel

So, basically, since it's a Locums (75) x Jobs (50) number of rows in the Matrix.

Now, I need to run a loop (ForEach) on my Matrix (I call it MindMapTier01) as follows:

foreach (LocumJobDistanceDifferenceObject LocumJobDistanceDifferenceItem in MindMapTier01.OrderBy(order=>order.JobID)) {
    /**
     * Build a list (KeyValuePair<JobID, LocumID>) such that for each unique JobID,
     * I can assign the Locum closest to that Job. I need to keep in mind that
     * once a job is assigned, I dont want that JobID or LocumID for the next iteration
    **/
}

I hope I explained myself. I need to get over this within an hour or two. Please help.

Regards.

Hassan Gulzar
  • 3,852
  • 4
  • 35
  • 69

1 Answers1

0

I don't know that I fully understand your problem, but if you want to ensure that a job is assigned to the closest locum then your code could look like this:

Dictionary<Int64, Int64> dicJobLocum = New Dictionary<Int64, Int64>(); // This is the key value pair list
Dictionary<Int64, Int64> dicJobDistance = New Dictionary<Int64, Decimal>(); // This is to track the distance of the currently assigned locum

foreach (LocumJobDistanceDifferenceObject locum in MindMapTier01) {
    if (dicJobDistance.ContainsKey(locum.JobID) {
       Decimal distance = dicJobDistance(locum.JobID);
       // If the job has been assigned, check if the current locum is closer
       if (locum.DistanceMiles < distance) {
            dicJobDistance(locum.JobID) = locum.Distance;
            dicJobLocum(locum.JobID) = locum.LocumID;
       }
    }
    else {
       // If the job has not been assigned yet
       dicJobDistance.Add(locum.JobID, locum.DistanceMiles);
       dicJobLocum.Add(locum.JobID, locum.LocumID);
    }
}

Please excuse any minor syntax errors, I have not been using c# recently.

Bill Dozer
  • 36
  • 1