Initial configuration rework - Assignment by size over prioriy

This commit is contained in:
Dawid Pietrykowski 2022-09-04 23:12:48 +02:00
parent 6fba1735f9
commit 6bd1b53eda
3 changed files with 80 additions and 13 deletions

View File

@ -0,0 +1 @@
,davp,arch-desktop,04.09.2022 23:00,file:///home/davp/.config/libreoffice/4;

View File

@ -17,16 +17,16 @@ namespace Parking
var spotsCreated = new int[4, 4];
var count = 100;
var count = 77;
var maxCount = 0;
var maxCount = count;
if (PlaceNCars(count, spotCountsPerpendicular, spotCountsParallel, spotsCreated))
{
count++;
while (PlaceNCars(count, spotCountsPerpendicular, spotCountsParallel, spotsCreated)) count++;
maxCount = count--;
maxCount = count - 1;
}
else
{
@ -38,6 +38,19 @@ namespace Parking
Debug.Log($"Best solution count {maxCount}");
int[] counts = new int[] {0, 0, 0, 0};
for (int i = 0; i < spotsCreated.GetLength(0); i++) {
for (int j = 0; j < spotsCreated.GetLength(1); j++) {
counts[j] += spotsCreated[i, j];
}
}
var countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " +
$"Suma: {counts.Sum()}";
ParkingManager.Instance.UpdateText(countsString);
Debug.Log(countsString);
return spotsCreated;
}
@ -54,11 +67,34 @@ namespace Parking
// Debug.Log($"Fixed car spots: {fixedCarSpots}");
// Debug.Log("Calculating required spot counts...");
int maxI = 0;
List<Driver> driversLowerPrioritySorted = new List<Driver>(DataImporter.Drivers.Count);
for (var i = 0; i < DataImporter.Drivers.Count && i + fixedCarSpots < carsToPlace; i++)
counts[(int) DataImporter.Drivers[i].Size]++;
if (DataImporter.Drivers[i].Priority == 1) {
counts[(int) DataImporter.Drivers[i].Size]++;
maxI = i;
}
foreach (Driver driver in DataImporter.Drivers)
if(driver.Priority != 1)
driversLowerPrioritySorted.Add(driver);
for(int i = maxI + 1; counts[(int) Size.C] < 20 && i < DataImporter.Drivers.Count; i++)
if (DataImporter.Drivers[i].Size == Size.C) {
counts[(int) DataImporter.Drivers[i].Size]++;
driversLowerPrioritySorted.Remove(DataImporter.Drivers[i]);
}
driversLowerPrioritySorted.Sort(BiggerSizeComparator);
int singlePriorityCars = counts.Sum();
for (var i = 0; i < driversLowerPrioritySorted.Count && i + fixedCarSpots + singlePriorityCars < carsToPlace; i++)
counts[(int) driversLowerPrioritySorted[i].Size]++;
var countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " +
$"Suma: {counts.Sum()}";
$"Suma: {counts.Sum() + 1}";
ParkingManager.Instance.UpdateText(countsString);
Debug.Log(countsString);
@ -114,9 +150,11 @@ namespace Parking
var sizes = sizeIds.Select(x => AvailableSizesCombinations[x]).ToArray();
// Debug.Log($"Testing: {sizes[0]} {sizes[1]} {sizes[2]} {sizes[3]} sum: {sizes.Sum()}");
for (var i = 0; i < spotsCreated.GetLength(0); i++)
for (var j = 0; j < spotsCreated.GetLength(1); j++)
spotsCreated[i, j] = 0;
var spotsCreatedTemp = new int[4, 4];
for (var i = 0; i < spotsCreatedTemp.GetLength(0); i++)
for (var j = 0; j < spotsCreatedTemp.GetLength(1); j++)
spotsCreatedTemp[i, j] = 0;
var requiredCounts = new int[requiredCountsRef.Length];
requiredCountsRef.CopyTo((Span<int>) requiredCounts);
@ -143,7 +181,7 @@ namespace Parking
spotCountsPerpendicular[laneId] -= spotsTaken;
requiredCounts[spotSize] -= spotsTaken;
spotsCreated[laneId, spotSize] += spotsTaken;
spotsCreatedTemp[laneId, spotSize] += spotsTaken;
// TODO: Allow mixed configuration
for (var x = 0; x < 3; x++)
@ -155,7 +193,7 @@ namespace Parking
// spotCountsParallel[laneId, spotSize] -= spotsTaken;
// requiredCounts[spotSize] -= spotsTaken;
//
// spotsCreated[laneId, spotSize] += spotsTaken;
// spotsCreatedTemp[laneId, spotSize] += spotsTaken;
//
// // TODO: Allow mixed configuration
// spotCountsPerpendicular[laneId] = 0;
@ -170,6 +208,26 @@ namespace Parking
var sum = requiredCounts.Sum();
if (sum > 0)
return false;
for (var laneId = 3; laneId >= 0; laneId--) {
if (spotCountsPerpendicular[laneId] != 0) {
if (sizeIds[laneId] == 0) {
// empty
}
else if (sizeIds[laneId] == 1) {
// parallel only
}
else {
spotsCreatedTemp[laneId, 0] += spotCountsPerpendicular[laneId];
spotCountsPerpendicular[laneId] = 0;
}
}
}
for (var i = 0; i < spotsCreatedTemp.GetLength(0); i++)
for (var j = 0; j < spotsCreatedTemp.GetLength(1); j++)
spotsCreated[i, j] = spotsCreatedTemp[i, j];
return true;
}
@ -184,6 +242,14 @@ namespace Parking
return -1 * sum1.CompareTo(sum2);
}
private int BiggerSizeComparator(Driver d1, Driver d2)
{
int sizeComp = d1.Size.CompareTo(d2.Size);
if (sizeComp == 0)
return d1.Priority.CompareTo(d2.Priority);
return sizeComp;
}
private void CombinationRepetitionUtil(int[] chosen, int[] arr,
int index, int r, int start, int end)
{
@ -192,7 +258,7 @@ namespace Parking
// combinations.Add(new[] {arr[chosen[0]]});
var tempArr = new List<int>(r);
for (var i = 0; i < r; i++) tempArr.Add(arr[chosen[i]]);
var hasEnoughSpace = tempArr.Select(x => AvailableSizesCombinations[x]).Sum() <
var hasEnoughSpace = tempArr.Select(x => AvailableSizesCombinations[x]).Sum() <=
ParkingManager.Height - 11;
var contains5 = tempArr.Contains(4);
tempArr.Sort();

View File

@ -269,8 +269,8 @@ namespace Parking
{
var spotMap = GenerateSpotMap(spotsCreated);
var maxP3 = _spotHeights[(int) spotMap[2].Max().Size];
var maxP2 = _spotHeights[(int) spotMap[1].Max().Size];
var maxP3 = spotMap[2].Count == 0 ? 0 : _spotHeights[(int) spotMap[2].Max().Size];
var maxP2 = spotMap[1].Count == 0 ? 0 : _spotHeights[(int) spotMap[1].Max().Size];
for (var i = 0; i < 4; i++) {
float currentY;