WIP Initial configuration generation - Disabled parallel parking spots

This commit is contained in:
Dawid Pietrykowski 2022-08-28 22:59:22 +02:00
parent 528be18635
commit bcd6b66d1f

View File

@ -7,7 +7,7 @@ using UnityEngine;
public class ParkingManager : MonoBehaviour public class ParkingManager : MonoBehaviour
{ {
private static readonly float[] AvailableSizesCombinations = {0, 2.5f, 4, 4.5f, 5, 7.5f}; private static readonly float[] AvailableSizesCombinations = {0, 2.5f, 4, 4.5f, 5};//, 7.5f};
private static readonly List<int[]> combinations = new(3000); private static readonly List<int[]> combinations = new(3000);
[SerializeField] private float width = 68; [SerializeField] private float width = 68;
@ -45,7 +45,7 @@ public class ParkingManager : MonoBehaviour
int maxCount = 0; int maxCount = 0;
if (PlaceNCars(60, spotCountsPerpendicular, spotCountsParallel, spotsCreated)) if (PlaceNCars(30, spotCountsPerpendicular, spotCountsParallel, spotsCreated))
{ {
count++; count++;
while (PlaceNCars(count, spotCountsPerpendicular, spotCountsParallel, spotsCreated)) while (PlaceNCars(count, spotCountsPerpendicular, spotCountsParallel, spotsCreated))
@ -71,10 +71,10 @@ public class ParkingManager : MonoBehaviour
private bool PlaceNCars(int carsToPlace, int[] spotCountsPerpendicular, int[,] spotCountsParallel, int[,] spotsCreated) private bool PlaceNCars(int carsToPlace, int[] spotCountsPerpendicular, int[,] spotCountsParallel, int[,] spotsCreated)
{ {
int[] counts = {0, 0, 0, 0}; int[] counts = {0, 0, 0};
counts[(int) Size.A] += 2; counts[(int) Size.A] += 2;
counts[(int) Size.B] += 2; counts[(int) Size.B] += 2;
counts[(int) Size.D] += 1; // counts[(int) Size.D] += 1;
var fixedCarSpots = 0; var fixedCarSpots = 0;
foreach (var c in counts) fixedCarSpots += c; foreach (var c in counts) fixedCarSpots += c;
Debug.Log($"Fixed car spots: {fixedCarSpots}"); Debug.Log($"Fixed car spots: {fixedCarSpots}");
@ -85,7 +85,9 @@ public class ParkingManager : MonoBehaviour
counts[(int) DataImporter.Drivers[i].Size]++; counts[(int) DataImporter.Drivers[i].Size]++;
} }
Debug.Log($"A: {counts[0]} B: {counts[1]} C: {counts[2]} D: {counts[3]} " + // Debug.Log($"A: {counts[0]} B: {counts[1]} C: {counts[2]} D: {counts[3]} " +
// $"Sum: {counts.Sum()}");
Debug.Log($"A: {counts[0]} B: {counts[1]} C: {counts[2]} " +
$"Sum: {counts.Sum()}"); $"Sum: {counts.Sum()}");
@ -113,17 +115,17 @@ public class ParkingManager : MonoBehaviour
private void PreProcessCombinations(out int[] spotCountsPerpendicular, out int[,] spotCountsParallel) private void PreProcessCombinations(out int[] spotCountsPerpendicular, out int[,] spotCountsParallel)
{ {
Debug.Log("Calculating spot counts..."); Debug.Log("Calculating spot counts...");
float[] spotLengthsParallel = {4f, 4.5f, 5f, 7.5f}; float[] spotLengthsParallel = {4f, 4.5f, 5f};//, 7.5f};
spotCountsPerpendicular = new[] {0, 0, 0, 0}; spotCountsPerpendicular = new[] {0, 0, 0, 0};
spotCountsPerpendicular[0] = Mathf.FloorToInt((width - 5.5f) / 2.25f); spotCountsPerpendicular[0] = Mathf.FloorToInt((width - 5.5f) / _spotWidth);
spotCountsPerpendicular[1] = Mathf.FloorToInt((width - 5.5f) / 2.25f); spotCountsPerpendicular[1] = Mathf.FloorToInt((width - 5.5f) / _spotWidth);
spotCountsPerpendicular[2] = Mathf.FloorToInt((width - 5.5f) / 2.25f); spotCountsPerpendicular[2] = Mathf.FloorToInt((width - 5.5f) / _spotWidth);
spotCountsPerpendicular[3] = Mathf.FloorToInt((width - 5.5f - (3.6f * 2) - (2.5f * 4)) / 2.25f); spotCountsPerpendicular[3] = Mathf.FloorToInt((width - 5.5f - (3.6f * 2) - (2.5f * 4)) / _spotWidth);
spotCountsParallel = new int[4, 4]; spotCountsParallel = new int[4, 4];
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++) for (int j = 0; j < spotLengthsParallel.Length; j++)
spotCountsParallel[i, j] = Mathf.FloorToInt((width - 5.5f) / spotLengthsParallel[j]); spotCountsParallel[i, j] = Mathf.FloorToInt((width - 5.5f) / spotLengthsParallel[j]);
for (int j = 0; j < 4; j++) for (int j = 0; j < spotLengthsParallel.Length; j++)
spotCountsParallel[3, j] = Mathf.FloorToInt((width - 5.5f - (3.6f * 2) - (2.5f * 4)) / spotLengthsParallel[j]); spotCountsParallel[3, j] = Mathf.FloorToInt((width - 5.5f - (3.6f * 2) - (2.5f * 4)) / spotLengthsParallel[j]);
Debug.Log($"" + Debug.Log($"" +
$"P1: {spotCountsPerpendicular[0]}/ P2: {spotCountsPerpendicular[1]} " + $"P1: {spotCountsPerpendicular[0]}/ P2: {spotCountsPerpendicular[1]} " +
@ -131,7 +133,7 @@ public class ParkingManager : MonoBehaviour
$"Sum: {spotCountsPerpendicular.Sum()}"); $"Sum: {spotCountsPerpendicular.Sum()}");
Debug.Log("Generating combinations..."); Debug.Log("Generating combinations...");
int[] arr = {0, 1, 2, 3, 4, 5}; int[] arr = {0, 1, 2, 3, 4};
var n = arr.Length; var n = arr.Length;
var r = 4; var r = 4;
CombinationRepetition(arr, n, r); CombinationRepetition(arr, n, r);
@ -144,7 +146,7 @@ public class ParkingManager : MonoBehaviour
private bool TestCombination(int[] sizeIds, int[] spotCountsPerpendicularRef, int[,] spotCountsParallelRef, int[] requiredCountsRef, int[,] spotsCreated) private bool TestCombination(int[] sizeIds, int[] spotCountsPerpendicularRef, int[,] spotCountsParallelRef, int[] requiredCountsRef, int[,] spotsCreated)
{ {
float[] sizes = sizeIds.Select(x => AvailableSizesCombinations[x]).ToArray(); float[] sizes = sizeIds.Select(x => AvailableSizesCombinations[x]).ToArray();
Debug.Log($"Testing: {sizes[0]} {sizes[1]} {sizes[2]} {sizes[3]} sum: {sizes.Sum()}"); Debug.Log($"Testing: {sizes[0]} {sizes[1]} {sizes[2]} sum: {sizes.Sum()}");
for(int i = 0; i < spotsCreated.GetLength(0); i++) for(int i = 0; i < spotsCreated.GetLength(0); i++)
for (int j = 0; j < spotsCreated.GetLength(1); j++) for (int j = 0; j < spotsCreated.GetLength(1); j++)
@ -161,7 +163,7 @@ public class ParkingManager : MonoBehaviour
for(int j = 0; j < spotCountsParallelRef.GetLength(1); j++) for(int j = 0; j < spotCountsParallelRef.GetLength(1); j++)
spotCountsParallel[i,j] = spotCountsParallelRef[i, j]; spotCountsParallel[i,j] = spotCountsParallelRef[i, j];
for (int spotSize = 3; spotSize >= 0; spotSize--) for (int spotSize = 2; spotSize >= 0; spotSize--)
{ {
for (int laneId = 0; laneId < 4; laneId++) for (int laneId = 0; laneId < 4; laneId++)
{ {
@ -174,22 +176,23 @@ public class ParkingManager : MonoBehaviour
spotsCreated[laneId, spotSize] += spotsTaken; spotsCreated[laneId, spotSize] += spotsTaken;
// TODO: Allow modified configuration // TODO: Allow modified configuration
for(int x = 0; x < 4; x++) for(int x = 0; x < 3; x++)
spotCountsParallel[laneId, x] = 0; spotCountsParallel[laneId, x] = 0;
}else if (sizeIds[laneId] == 0 && spotCountsParallel[laneId, spotSize] != 0) { // parking parallel
int spotsTaken = Math.Min(requiredCounts[spotSize], spotCountsParallel[laneId, spotSize]);
spotCountsParallel[laneId, spotSize] -= spotsTaken;
requiredCounts[spotSize] -= spotsTaken;
spotsCreated[laneId, spotSize] += spotsTaken;
// TODO: Allow modified configuration
spotCountsPerpendicular[laneId] = 0;
for(int x = 0; x < 4; x++)
if(x != spotSize)
spotCountsParallel[laneId, x] = 0;
} }
// else if (sizeIds[laneId] == 0 && spotCountsParallel[laneId, spotSize] != 0) { // parking parallel
// int spotsTaken = Math.Min(requiredCounts[spotSize], spotCountsParallel[laneId, spotSize]);
//
// spotCountsParallel[laneId, spotSize] -= spotsTaken;
// requiredCounts[spotSize] -= spotsTaken;
//
// spotsCreated[laneId, spotSize] += spotsTaken;
//
// // TODO: Allow modified configuration
// spotCountsPerpendicular[laneId] = 0;
// for(int x = 0; x < 3; x++)
// if(x != spotSize)
// spotCountsParallel[laneId, x] = 0;
// }
if(requiredCounts[spotSize] == 0) if(requiredCounts[spotSize] == 0)
break; break;
} }