Code cleanup

This commit is contained in:
Dawid Pietrykowski 2022-09-05 22:01:52 +02:00
parent a2c9647681
commit 7517f9e374
4 changed files with 132 additions and 139 deletions

View File

@ -2,9 +2,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Runtime.Serialization;
using Unity.VisualScripting;
using UnityEngine;
namespace Parking namespace Parking
{ {
@ -14,29 +11,26 @@ namespace Parking
public static void ReadFile(string path) public static void ReadFile(string path)
{ {
var fileData = File.ReadAllText(path); string fileData = File.ReadAllText(path);
var lines = fileData.Split("\n"[0]); string[] lines = fileData.Split("\n"[0]);
var counter = 0; int counter = 0;
foreach (var line in lines) foreach (string line in lines) {
{ if (line == "")
if(line == "")
continue; continue;
if (counter == 0) if (counter == 0) {
{
counter++; counter++;
continue; continue;
} }
var lineData = line.Trim().Split(','); string[] lineData = line.Trim().Split(',');
var preference = lineData[0] == "" ParkingPreference preference = lineData[0] == ""
? ParkingPreference.Any ? ParkingPreference.Any
: lineData[1] == "P" : lineData[1] == "P"
? ParkingPreference.Front ? ParkingPreference.Front
: ParkingPreference.Back; : ParkingPreference.Back;
Size size; Size size;
switch (lineData[2]) switch (lineData[2]) {
{
case "Mały": case "Mały":
size = Size.A; size = Size.A;
break; break;
@ -54,18 +48,23 @@ namespace Parking
break; break;
} }
DateTime realArrival = DateTime.Parse(lineData[5].Contains(':') ? lineData[5] : lineData[5] + ":00", CultureInfo.InvariantCulture); DateTime realArrival = DateTime.Parse(lineData[5].Contains(':') ? lineData[5] : lineData[5] + ":00",
DateTime realDeparture = DateTime.Parse(lineData[6].Contains(':') ? lineData[6] : lineData[6] + ":00", CultureInfo.InvariantCulture); CultureInfo.InvariantCulture);
DateTime plannedArrival = DateTime.Parse(lineData[3].Contains(':') ? lineData[3] : lineData[3] + ":00", CultureInfo.InvariantCulture); DateTime realDeparture = DateTime.Parse(lineData[6].Contains(':') ? lineData[6] : lineData[6] + ":00",
DateTime plannedDeparture = DateTime.Parse(lineData[4].Contains(':') ? lineData[4] : lineData[4] + ":00", CultureInfo.InvariantCulture); CultureInfo.InvariantCulture);
DateTime plannedArrival = DateTime.Parse(lineData[3].Contains(':') ? lineData[3] : lineData[3] + ":00",
CultureInfo.InvariantCulture);
DateTime plannedDeparture =
DateTime.Parse(lineData[4].Contains(':') ? lineData[4] : lineData[4] + ":00",
CultureInfo.InvariantCulture);
if (!Int32.TryParse(lineData[0], out var priority)) if (!int.TryParse(lineData[0], out int priority))
priority = 3; priority = 3;
if (!Int32.TryParse(lineData[7], out var updateInterval)) if (!int.TryParse(lineData[7], out int updateInterval))
updateInterval = 3; updateInterval = 3;
Driver driver = new Driver(size, counter, preference, priority, updateInterval); Driver driver = new(size, counter, preference, priority, updateInterval);
driver.Times = new[] {plannedArrival, plannedDeparture, realArrival, realDeparture}; driver.Times = new[] {plannedArrival, plannedDeparture, realArrival, realDeparture};
Drivers.Add(driver); Drivers.Add(driver);

View File

@ -1,20 +1,20 @@
using System; using System;
using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using Object = UnityEngine.Object;
namespace Parking namespace Parking
{ {
public class Driver public class Driver
{ {
public readonly ParkingPreference ParkingPreference;
public readonly int Number; public readonly int Number;
public readonly ParkingPreference ParkingPreference;
public readonly Size Size; public readonly Size Size;
public DateTime[] Times;
public Spot Spot;
public bool Parked = false;
public GameObject GameObject; public GameObject GameObject;
public bool Rejected = false; public bool Parked;
public int Priority; public int Priority;
public bool Rejected;
public Spot Spot;
public DateTime[] Times;
public int UpdateInterval; public int UpdateInterval;
public Driver(Size size, int number, ParkingPreference parkingPreference, int priority, int updateInterval) public Driver(Size size, int number, ParkingPreference parkingPreference, int priority, int updateInterval)
@ -30,26 +30,26 @@ namespace Parking
{ {
Parked = false; Parked = false;
Rejected = false; Rejected = false;
if(Spot != null) if (Spot != null)
Spot.Free = true; Spot.Free = true;
Spot = null; Spot = null;
if(GameObject != null) if (GameObject != null)
GameObject.Destroy(GameObject); Object.Destroy(GameObject);
} }
} }
public class Spot : IComparable<Spot> public class Spot : IComparable<Spot>
{ {
public Size Size; public bool AlignToTop = true;
public bool Flipped; public bool Flipped;
public GameObject GameObject;
public bool Free = true; public bool Free = true;
public GameObject GameObject;
public int Lane = 0;
public ParkingPreference ParkingDirection = ParkingPreference.Any;
public bool Perpendicular = true;
public bool Reserved = false; public bool Reserved = false;
public int ReservedPriority = 0; public int ReservedPriority = 0;
public ParkingPreference ParkingDirection = ParkingPreference.Any; public Size Size;
public int Lane = 0;
public bool Perpendicular = true;
public bool AlignToTop = true;
public Spot(Size size, bool flipped) public Spot(Size size, bool flipped)
{ {
@ -61,6 +61,7 @@ namespace Parking
{ {
} }
public int CompareTo(Spot obj) public int CompareTo(Spot obj)
{ {
return Size.CompareTo(obj.Size); return Size.CompareTo(obj.Size);

View File

@ -13,22 +13,20 @@ namespace Parking
public int[,] FindSolution() public int[,] FindSolution()
{ {
PreProcessCombinations(out var spotCountsPerpendicular, out var spotCountsParallel); PreProcessCombinations(out int[] spotCountsPerpendicular, out int[,] spotCountsParallel);
var spotsCreated = new int[4, 4]; int[,] spotsCreated = new int[4, 4];
var maxCount = TestCombinations(spotCountsPerpendicular, spotCountsParallel, spotsCreated); int maxCount = TestCombinations(spotCountsPerpendicular, spotCountsParallel, spotsCreated);
Debug.Log($"Best solution count {maxCount}"); Debug.Log($"Best solution count {maxCount}");
int[] counts = new int[] {0, 0, 0, 0}; int[] counts = {0, 0, 0, 0};
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++)
counts[j] += spotsCreated[i, j]; counts[j] += spotsCreated[i, j];
}
}
var countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " + string countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " +
$"Suma: {counts.Sum() + 1}"; $"Suma: {counts.Sum() + 1}";
ParkingManager.Instance.UpdateText(countsString); ParkingManager.Instance.UpdateText(countsString);
Debug.Log(countsString); Debug.Log(countsString);
@ -42,20 +40,19 @@ namespace Parking
int[] counts = {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;
var fixedCarSpots = 0; int fixedCarSpots = 0;
foreach (var c in counts) fixedCarSpots += c; foreach (int c in counts) fixedCarSpots += c;
var countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " + string countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " +
$"Suma: {counts.Sum() + 1}"; $"Suma: {counts.Sum() + 1}";
ParkingManager.Instance.UpdateText(countsString); ParkingManager.Instance.UpdateText(countsString);
Debug.Log(countsString); Debug.Log(countsString);
int maxCount = 0; int maxCount = 0;
int[] maxComb = new []{1, 1, 1, 1}; int[] maxComb = {1, 1, 1, 1};
// Debug.Log("Printing top 5 combinations..."); // Debug.Log("Printing top 5 combinations...");
foreach (var comb in Combinations) foreach (int[] comb in Combinations) {
{
int res = TryFillCombination(comb.ToArray(), spotCountsPerpendicular, spotCountsParallel, counts, int res = TryFillCombination(comb.ToArray(), spotCountsPerpendicular, spotCountsParallel, counts,
spotsCreated); spotsCreated);
if (res > maxCount) { if (res > maxCount) {
@ -70,35 +67,36 @@ namespace Parking
return maxCount; return maxCount;
} }
private int TryFillCombination(int[] sizeIds, int[] spotCountsPerpendicularRef, int[,] spotCountsParallelRef, int[] counts, int[,] spotsCreated, bool copyToArray = false) private int TryFillCombination(int[] sizeIds, int[] spotCountsPerpendicularRef, int[,] spotCountsParallelRef,
int[] counts, int[,] spotsCreated, bool copyToArray = false)
{ {
var spotsCreatedTemp = new int[4, 4]; int[,] spotsCreatedTemp = new int[4, 4];
for (var i = 0; i < spotsCreatedTemp.GetLength(0); i++) for (int i = 0; i < spotsCreatedTemp.GetLength(0); i++)
for (var j = 0; j < spotsCreatedTemp.GetLength(1); j++) for (int j = 0; j < spotsCreatedTemp.GetLength(1); j++)
spotsCreatedTemp[i, j] = 0; spotsCreatedTemp[i, j] = 0;
var spotCountsPerpendicular = new int[spotCountsPerpendicularRef.Length]; int[] spotCountsPerpendicular = new int[spotCountsPerpendicularRef.Length];
spotCountsPerpendicularRef.CopyTo((Span<int>) spotCountsPerpendicular); spotCountsPerpendicularRef.CopyTo((Span<int>) spotCountsPerpendicular);
var spotCountsParallel = new int[spotCountsParallelRef.GetLength(0), spotCountsParallelRef.GetLength(1)]; int[,] spotCountsParallel = new int[spotCountsParallelRef.GetLength(0), spotCountsParallelRef.GetLength(1)];
for (var i = 0; i < spotCountsParallelRef.GetLength(0); i++) for (int i = 0; i < spotCountsParallelRef.GetLength(0); i++)
for (var 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];
float[] spotSizes = {4, 4.5f, 5}; float[] spotSizes = {4, 4.5f, 5};
foreach (Driver driver in DataImporter.Drivers) { foreach (Driver driver in DataImporter.Drivers) {
List<int> laneIds = new List<int>(){0, 1, 2, 3}; var laneIds = new List<int> {0, 1, 2, 3};
if (driver.Size == Size.C) if (driver.Size == Size.C)
laneIds.Reverse(); laneIds.Reverse();
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
int laneId = laneIds[i]; int laneId = laneIds[i];
bool freeSpotsAvailable = spotCountsPerpendicular[laneId] != 0; bool freeSpotsAvailable = spotCountsPerpendicular[laneId] != 0;
bool carFits = AvailableSizesCombinations[sizeIds[laneId]] >= spotSizes[(int)driver.Size]; bool carFits = AvailableSizesCombinations[sizeIds[laneId]] >= spotSizes[(int) driver.Size];
if (carFits && freeSpotsAvailable) { if (carFits && freeSpotsAvailable) {
spotCountsPerpendicular[laneId]--; spotCountsPerpendicular[laneId]--;
spotsCreatedTemp[laneId, (int)driver.Size]++; spotsCreatedTemp[laneId, (int) driver.Size]++;
break; break;
} }
} }
@ -107,15 +105,15 @@ namespace Parking
int count = 0; int count = 0;
for (var i = 0; i < spotsCreatedTemp.GetLength(0); i++) for (int i = 0; i < spotsCreatedTemp.GetLength(0); i++)
for (var j = 0; j < spotsCreatedTemp.GetLength(1); j++) for (int j = 0; j < spotsCreatedTemp.GetLength(1); j++)
count += spotsCreatedTemp[i, j]; count += spotsCreatedTemp[i, j];
if (!copyToArray) if (!copyToArray)
return count; return count;
for (var laneId = 3; laneId >= 0; laneId--) { for (int laneId = 3; laneId >= 0; laneId--)
if (spotCountsPerpendicular[laneId] != 0) { if (spotCountsPerpendicular[laneId] != 0) {
if (sizeIds[laneId] == 0) { if (sizeIds[laneId] == 0) {
// empty // empty
@ -129,11 +127,10 @@ namespace Parking
spotCountsPerpendicular[laneId] = 0; spotCountsPerpendicular[laneId] = 0;
} }
} }
}
for (var i = 0; i < spotsCreatedTemp.GetLength(0); i++) for (int i = 0; i < spotsCreatedTemp.GetLength(0); i++)
for (var j = 0; j < spotsCreatedTemp.GetLength(1); j++) for (int j = 0; j < spotsCreatedTemp.GetLength(1); j++)
spotsCreated[i, j] = spotsCreatedTemp[i, j]; spotsCreated[i, j] = spotsCreatedTemp[i, j];
@ -151,10 +148,10 @@ namespace Parking
spotCountsPerpendicular[3] = spotCountsPerpendicular[3] =
Mathf.FloorToInt((ParkingManager.Width - 5.5f - 3.6f * 2 - 2.5f * 4) / _spotWidth); Mathf.FloorToInt((ParkingManager.Width - 5.5f - 3.6f * 2 - 2.5f * 4) / _spotWidth);
spotCountsParallel = new int[4, 4]; spotCountsParallel = new int[4, 4];
for (var i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
for (var j = 0; j < spotLengthsParallel.Length; j++) for (int j = 0; j < spotLengthsParallel.Length; j++)
spotCountsParallel[i, j] = Mathf.FloorToInt((ParkingManager.Width - 5.5f) / spotLengthsParallel[j]); spotCountsParallel[i, j] = Mathf.FloorToInt((ParkingManager.Width - 5.5f) / spotLengthsParallel[j]);
for (var j = 0; j < spotLengthsParallel.Length; j++) for (int j = 0; j < spotLengthsParallel.Length; j++)
spotCountsParallel[3, j] = spotCountsParallel[3, j] =
Mathf.FloorToInt((ParkingManager.Width - 5.5f - 3.6f * 2 - 2.5f * 4) / spotLengthsParallel[j]); Mathf.FloorToInt((ParkingManager.Width - 5.5f - 3.6f * 2 - 2.5f * 4) / spotLengthsParallel[j]);
// Debug.Log("" + // Debug.Log("" +
@ -164,8 +161,8 @@ namespace Parking
// Debug.Log("Generating combinations..."); // Debug.Log("Generating combinations...");
int[] arr = {0, 1, 2, 3, 4}; int[] arr = {0, 1, 2, 3, 4};
var n = arr.Length; int n = arr.Length;
var r = 4; int r = 4;
CombinationRepetition(arr, n, r); CombinationRepetition(arr, n, r);
// Debug.Log($"Found {Combinations.Count} available combinations"); // Debug.Log($"Found {Combinations.Count} available combinations");
@ -195,14 +192,13 @@ namespace Parking
private void CombinationRepetitionUtil(int[] chosen, int[] arr, private void CombinationRepetitionUtil(int[] chosen, int[] arr,
int index, int r, int start, int end) int index, int r, int start, int end)
{ {
if (index == r) if (index == r) {
{
// combinations.Add(new[] {arr[chosen[0]]}); // combinations.Add(new[] {arr[chosen[0]]});
var tempArr = new List<int>(r); var tempArr = new List<int>(r);
for (var i = 0; i < r; i++) tempArr.Add(arr[chosen[i]]); for (int i = 0; i < r; i++) tempArr.Add(arr[chosen[i]]);
var hasEnoughSpace = tempArr.Select(x => AvailableSizesCombinations[x]).Sum() <= bool hasEnoughSpace = tempArr.Select(x => AvailableSizesCombinations[x]).Sum() <=
ParkingManager.Height - 11; ParkingManager.Height - 11;
var contains5 = tempArr.Contains(4); bool contains5 = tempArr.Contains(4);
tempArr.Sort(); tempArr.Sort();
if (hasEnoughSpace && contains5) if (hasEnoughSpace && contains5)
Combinations.Add(tempArr.ToArray()); Combinations.Add(tempArr.ToArray());
@ -210,8 +206,7 @@ namespace Parking
return; return;
} }
for (var i = start; i <= end; i++) for (int i = start; i <= end; i++) {
{
chosen[index] = i; chosen[index] = i;
CombinationRepetitionUtil(chosen, arr, index + 1, CombinationRepetitionUtil(chosen, arr, index + 1,
r, i, end); r, i, end);
@ -220,7 +215,7 @@ namespace Parking
private void CombinationRepetition(int[] arr, int n, int r) private void CombinationRepetition(int[] arr, int n, int r)
{ {
var chosen = new int[r + 1]; int[] chosen = new int[r + 1];
CombinationRepetitionUtil(chosen, arr, 0, r, 0, n - 1); CombinationRepetitionUtil(chosen, arr, 0, r, 0, n - 1);
} }
} }

View File

@ -109,6 +109,7 @@ namespace Parking
ReserveInitialSpots(); ReserveInitialSpots();
ResetDrivers(); ResetDrivers();
} }
return; return;
} }
@ -243,8 +244,7 @@ namespace Parking
private void ReconfigureSpots() private void ReconfigureSpots()
{ {
List<GameObject> prefabs = new List<GameObject>() var prefabs = new List<GameObject> {spotPrefabA, spotPrefabB, spotPrefabC};
{spotPrefabA, spotPrefabB, spotPrefabC};
int[] freeSpots = GetFreeSpotCount(); int[] freeSpots = GetFreeSpotCount();
var nextCars = GetNextCars(10); var nextCars = GetNextCars(10);
int[] plannedSpots = {0, 0, 0, 0}; int[] plannedSpots = {0, 0, 0, 0};
@ -263,12 +263,12 @@ namespace Parking
bool foundReplacement = true; bool foundReplacement = true;
while (foundReplacement && neededSpots[size] != 0) { while (foundReplacement && neededSpots[size] != 0) {
foundReplacement = false; foundReplacement = false;
foreach (var t in _spotMap) { foreach (var t in _spotMap)
foreach (Spot spot in t) { foreach (Spot spot in t)
if ((int) spot.Size > size && spot.Free && spot.Lane != 3 && spot.Size != Size.D) { if ((int) spot.Size > size && spot.Free && spot.Lane != 3 && spot.Size != Size.D) {
foundReplacement = true; foundReplacement = true;
float diff = (_spotHeights[(int)spot.Size] - _spotHeights[size]) / 2.0f; float diff = (_spotHeights[(int) spot.Size] - _spotHeights[size]) / 2.0f;
if (!spot.AlignToTop) if (!spot.AlignToTop)
diff *= -1; diff *= -1;
@ -287,9 +287,8 @@ namespace Parking
goto whileEnd; goto whileEnd;
} }
}
} whileEnd: ;
whileEnd:;
} }
} }
} }
@ -437,19 +436,19 @@ namespace Parking
GameObject spawnedSpot; GameObject spawnedSpot;
int[] emergencyMap = new[] {12, 27, 27, 20}; int[] emergencyMap = {12, 27, 27, 20};
for (int j = 0; j < emergencyMap[i]; j++) {
for (int j = 0; j < emergencyMap[i]; j++)
if (i == 0) { if (i == 0) {
Vector3 position = new Vector3(currentX, currentY + 2.25f/2.0f, 0); Vector3 position = new(currentX, currentY + 2.25f / 2.0f, 0);
spawnedSpot = Instantiate(Instance.spotPrefabC, position, spawnedSpot = Instantiate(Instance.spotPrefabC, position,
Quaternion.Euler(new Vector3(0, 0, 90)), emergencyPlanContainer); Quaternion.Euler(new Vector3(0, 0, 90)), emergencyPlanContainer);
currentX -= 5; currentX -= 5;
}else { }
else {
bool alignTop = i % 2 != 0; bool alignTop = i % 2 != 0;
Vector3 position = new Vector3(currentX, currentY, 0); Vector3 position = new(currentX, currentY, 0);
spawnedSpot = Instantiate(Instance.spotPrefabC, position, spawnedSpot = Instantiate(Instance.spotPrefabC, position,
Quaternion.Euler(new Vector3(0, 0, 0)), emergencyPlanContainer); Quaternion.Euler(new Vector3(0, 0, 0)), emergencyPlanContainer);
@ -462,7 +461,6 @@ namespace Parking
} }
} }
} }
}
private List<List<Spot>> GenerateSpotMap(int[,] spotsCreated) private List<List<Spot>> GenerateSpotMap(int[,] spotsCreated)
{ {