Code cleanup
This commit is contained in:
parent
a2c9647681
commit
7517f9e374
@ -2,9 +2,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Parking
|
||||
{
|
||||
@ -14,29 +11,26 @@ namespace Parking
|
||||
|
||||
public static void ReadFile(string path)
|
||||
{
|
||||
var fileData = File.ReadAllText(path);
|
||||
var lines = fileData.Split("\n"[0]);
|
||||
var counter = 0;
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if(line == "")
|
||||
string fileData = File.ReadAllText(path);
|
||||
string[] lines = fileData.Split("\n"[0]);
|
||||
int counter = 0;
|
||||
foreach (string line in lines) {
|
||||
if (line == "")
|
||||
continue;
|
||||
|
||||
if (counter == 0)
|
||||
{
|
||||
if (counter == 0) {
|
||||
counter++;
|
||||
continue;
|
||||
}
|
||||
|
||||
var lineData = line.Trim().Split(',');
|
||||
var preference = lineData[0] == ""
|
||||
string[] lineData = line.Trim().Split(',');
|
||||
ParkingPreference preference = lineData[0] == ""
|
||||
? ParkingPreference.Any
|
||||
: lineData[1] == "P"
|
||||
? ParkingPreference.Front
|
||||
: ParkingPreference.Back;
|
||||
Size size;
|
||||
switch (lineData[2])
|
||||
{
|
||||
switch (lineData[2]) {
|
||||
case "Mały":
|
||||
size = Size.A;
|
||||
break;
|
||||
@ -54,18 +48,23 @@ namespace Parking
|
||||
break;
|
||||
}
|
||||
|
||||
DateTime realArrival = DateTime.Parse(lineData[5].Contains(':') ? lineData[5] : lineData[5] + ":00", CultureInfo.InvariantCulture);
|
||||
DateTime realDeparture = DateTime.Parse(lineData[6].Contains(':') ? lineData[6] : lineData[6] + ":00", 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);
|
||||
DateTime realArrival = DateTime.Parse(lineData[5].Contains(':') ? lineData[5] : lineData[5] + ":00",
|
||||
CultureInfo.InvariantCulture);
|
||||
DateTime realDeparture = DateTime.Parse(lineData[6].Contains(':') ? lineData[6] : lineData[6] + ":00",
|
||||
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;
|
||||
|
||||
if (!Int32.TryParse(lineData[7], out var updateInterval))
|
||||
if (!int.TryParse(lineData[7], out int updateInterval))
|
||||
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};
|
||||
Drivers.Add(driver);
|
||||
|
||||
|
@ -1,20 +1,20 @@
|
||||
using System;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Parking
|
||||
{
|
||||
public class Driver
|
||||
{
|
||||
public readonly ParkingPreference ParkingPreference;
|
||||
public readonly int Number;
|
||||
public readonly ParkingPreference ParkingPreference;
|
||||
public readonly Size Size;
|
||||
public DateTime[] Times;
|
||||
public Spot Spot;
|
||||
public bool Parked = false;
|
||||
public GameObject GameObject;
|
||||
public bool Rejected = false;
|
||||
public bool Parked;
|
||||
public int Priority;
|
||||
public bool Rejected;
|
||||
public Spot Spot;
|
||||
public DateTime[] Times;
|
||||
public int UpdateInterval;
|
||||
|
||||
public Driver(Size size, int number, ParkingPreference parkingPreference, int priority, int updateInterval)
|
||||
@ -30,26 +30,26 @@ namespace Parking
|
||||
{
|
||||
Parked = false;
|
||||
Rejected = false;
|
||||
if(Spot != null)
|
||||
if (Spot != null)
|
||||
Spot.Free = true;
|
||||
Spot = null;
|
||||
if(GameObject != null)
|
||||
GameObject.Destroy(GameObject);
|
||||
if (GameObject != null)
|
||||
Object.Destroy(GameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public class Spot : IComparable<Spot>
|
||||
{
|
||||
public Size Size;
|
||||
public bool AlignToTop = true;
|
||||
public bool Flipped;
|
||||
public GameObject GameObject;
|
||||
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 int ReservedPriority = 0;
|
||||
public ParkingPreference ParkingDirection = ParkingPreference.Any;
|
||||
public int Lane = 0;
|
||||
public bool Perpendicular = true;
|
||||
public bool AlignToTop = true;
|
||||
public Size Size;
|
||||
|
||||
public Spot(Size size, bool flipped)
|
||||
{
|
||||
@ -61,6 +61,7 @@ namespace Parking
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int CompareTo(Spot obj)
|
||||
{
|
||||
return Size.CompareTo(obj.Size);
|
||||
|
@ -13,23 +13,21 @@ namespace Parking
|
||||
|
||||
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}");
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
int[] counts = {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() + 1}";
|
||||
string countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " +
|
||||
$"Suma: {counts.Sum() + 1}";
|
||||
ParkingManager.Instance.UpdateText(countsString);
|
||||
Debug.Log(countsString);
|
||||
|
||||
@ -42,20 +40,19 @@ namespace Parking
|
||||
int[] counts = {0, 0, 0};
|
||||
counts[(int) Size.A] += 2;
|
||||
counts[(int) Size.B] += 2;
|
||||
var fixedCarSpots = 0;
|
||||
foreach (var c in counts) fixedCarSpots += c;
|
||||
int fixedCarSpots = 0;
|
||||
foreach (int c in counts) fixedCarSpots += c;
|
||||
|
||||
var countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " +
|
||||
$"Suma: {counts.Sum() + 1}";
|
||||
string countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " +
|
||||
$"Suma: {counts.Sum() + 1}";
|
||||
ParkingManager.Instance.UpdateText(countsString);
|
||||
Debug.Log(countsString);
|
||||
|
||||
int maxCount = 0;
|
||||
int[] maxComb = new []{1, 1, 1, 1};
|
||||
int[] maxComb = {1, 1, 1, 1};
|
||||
|
||||
// Debug.Log("Printing top 5 combinations...");
|
||||
foreach (var comb in Combinations)
|
||||
{
|
||||
foreach (int[] comb in Combinations) {
|
||||
int res = TryFillCombination(comb.ToArray(), spotCountsPerpendicular, spotCountsParallel, counts,
|
||||
spotsCreated);
|
||||
if (res > maxCount) {
|
||||
@ -70,35 +67,36 @@ namespace Parking
|
||||
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 (var j = 0; j < spotsCreatedTemp.GetLength(1); j++)
|
||||
for (int i = 0; i < spotsCreatedTemp.GetLength(0); i++)
|
||||
for (int j = 0; j < spotsCreatedTemp.GetLength(1); j++)
|
||||
spotsCreatedTemp[i, j] = 0;
|
||||
|
||||
var spotCountsPerpendicular = new int[spotCountsPerpendicularRef.Length];
|
||||
int[] spotCountsPerpendicular = new int[spotCountsPerpendicularRef.Length];
|
||||
spotCountsPerpendicularRef.CopyTo((Span<int>) spotCountsPerpendicular);
|
||||
|
||||
var spotCountsParallel = new int[spotCountsParallelRef.GetLength(0), spotCountsParallelRef.GetLength(1)];
|
||||
for (var i = 0; i < spotCountsParallelRef.GetLength(0); i++)
|
||||
for (var j = 0; j < spotCountsParallelRef.GetLength(1); j++)
|
||||
int[,] spotCountsParallel = new int[spotCountsParallelRef.GetLength(0), spotCountsParallelRef.GetLength(1)];
|
||||
for (int i = 0; i < spotCountsParallelRef.GetLength(0); i++)
|
||||
for (int j = 0; j < spotCountsParallelRef.GetLength(1); j++)
|
||||
spotCountsParallel[i, j] = spotCountsParallelRef[i, j];
|
||||
|
||||
float[] spotSizes = {4, 4.5f, 5};
|
||||
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)
|
||||
laneIds.Reverse();
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int laneId = laneIds[i];
|
||||
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) {
|
||||
spotCountsPerpendicular[laneId]--;
|
||||
spotsCreatedTemp[laneId, (int)driver.Size]++;
|
||||
spotsCreatedTemp[laneId, (int) driver.Size]++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -107,15 +105,15 @@ namespace Parking
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (var i = 0; i < spotsCreatedTemp.GetLength(0); i++)
|
||||
for (var j = 0; j < spotsCreatedTemp.GetLength(1); j++)
|
||||
for (int i = 0; i < spotsCreatedTemp.GetLength(0); i++)
|
||||
for (int j = 0; j < spotsCreatedTemp.GetLength(1); j++)
|
||||
count += spotsCreatedTemp[i, j];
|
||||
|
||||
|
||||
if (!copyToArray)
|
||||
return count;
|
||||
|
||||
for (var laneId = 3; laneId >= 0; laneId--) {
|
||||
for (int laneId = 3; laneId >= 0; laneId--)
|
||||
if (spotCountsPerpendicular[laneId] != 0) {
|
||||
if (sizeIds[laneId] == 0) {
|
||||
// empty
|
||||
@ -129,11 +127,10 @@ namespace Parking
|
||||
spotCountsPerpendicular[laneId] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (var i = 0; i < spotsCreatedTemp.GetLength(0); i++)
|
||||
for (var j = 0; j < spotsCreatedTemp.GetLength(1); j++)
|
||||
for (int i = 0; i < spotsCreatedTemp.GetLength(0); i++)
|
||||
for (int j = 0; j < spotsCreatedTemp.GetLength(1); j++)
|
||||
spotsCreated[i, j] = spotsCreatedTemp[i, j];
|
||||
|
||||
|
||||
@ -151,10 +148,10 @@ namespace Parking
|
||||
spotCountsPerpendicular[3] =
|
||||
Mathf.FloorToInt((ParkingManager.Width - 5.5f - 3.6f * 2 - 2.5f * 4) / _spotWidth);
|
||||
spotCountsParallel = new int[4, 4];
|
||||
for (var i = 0; i < 3; i++)
|
||||
for (var j = 0; j < spotLengthsParallel.Length; j++)
|
||||
for (int i = 0; i < 3; i++)
|
||||
for (int j = 0; j < spotLengthsParallel.Length; 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] =
|
||||
Mathf.FloorToInt((ParkingManager.Width - 5.5f - 3.6f * 2 - 2.5f * 4) / spotLengthsParallel[j]);
|
||||
// Debug.Log("" +
|
||||
@ -164,8 +161,8 @@ namespace Parking
|
||||
|
||||
// Debug.Log("Generating combinations...");
|
||||
int[] arr = {0, 1, 2, 3, 4};
|
||||
var n = arr.Length;
|
||||
var r = 4;
|
||||
int n = arr.Length;
|
||||
int r = 4;
|
||||
CombinationRepetition(arr, n, r);
|
||||
// Debug.Log($"Found {Combinations.Count} available combinations");
|
||||
|
||||
@ -195,14 +192,13 @@ namespace Parking
|
||||
private void CombinationRepetitionUtil(int[] chosen, int[] arr,
|
||||
int index, int r, int start, int end)
|
||||
{
|
||||
if (index == r)
|
||||
{
|
||||
if (index == r) {
|
||||
// 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() <=
|
||||
ParkingManager.Height - 11;
|
||||
var contains5 = tempArr.Contains(4);
|
||||
for (int i = 0; i < r; i++) tempArr.Add(arr[chosen[i]]);
|
||||
bool hasEnoughSpace = tempArr.Select(x => AvailableSizesCombinations[x]).Sum() <=
|
||||
ParkingManager.Height - 11;
|
||||
bool contains5 = tempArr.Contains(4);
|
||||
tempArr.Sort();
|
||||
if (hasEnoughSpace && contains5)
|
||||
Combinations.Add(tempArr.ToArray());
|
||||
@ -210,8 +206,7 @@ namespace Parking
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = start; i <= end; i++)
|
||||
{
|
||||
for (int i = start; i <= end; i++) {
|
||||
chosen[index] = i;
|
||||
CombinationRepetitionUtil(chosen, arr, index + 1,
|
||||
r, i, end);
|
||||
@ -220,7 +215,7 @@ namespace Parking
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -109,6 +109,7 @@ namespace Parking
|
||||
ReserveInitialSpots();
|
||||
ResetDrivers();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -243,8 +244,7 @@ namespace Parking
|
||||
|
||||
private void ReconfigureSpots()
|
||||
{
|
||||
List<GameObject> prefabs = new List<GameObject>()
|
||||
{spotPrefabA, spotPrefabB, spotPrefabC};
|
||||
var prefabs = new List<GameObject> {spotPrefabA, spotPrefabB, spotPrefabC};
|
||||
int[] freeSpots = GetFreeSpotCount();
|
||||
var nextCars = GetNextCars(10);
|
||||
int[] plannedSpots = {0, 0, 0, 0};
|
||||
@ -263,33 +263,32 @@ namespace Parking
|
||||
bool foundReplacement = true;
|
||||
while (foundReplacement && neededSpots[size] != 0) {
|
||||
foundReplacement = false;
|
||||
foreach (var t in _spotMap) {
|
||||
foreach (Spot spot in t) {
|
||||
if ((int) spot.Size > size && spot.Free && spot.Lane != 3 && spot.Size != Size.D) {
|
||||
foundReplacement = true;
|
||||
foreach (var t in _spotMap)
|
||||
foreach (Spot spot in t)
|
||||
if ((int) spot.Size > size && spot.Free && spot.Lane != 3 && spot.Size != Size.D) {
|
||||
foundReplacement = true;
|
||||
|
||||
float diff = (_spotHeights[(int)spot.Size] - _spotHeights[size]) / 2.0f;
|
||||
if (!spot.AlignToTop)
|
||||
diff *= -1;
|
||||
float diff = (_spotHeights[(int) spot.Size] - _spotHeights[size]) / 2.0f;
|
||||
if (!spot.AlignToTop)
|
||||
diff *= -1;
|
||||
|
||||
spot.Size = (Size) size;
|
||||
Vector3 position = Vector3.zero;
|
||||
Quaternion rotation = Quaternion.identity;
|
||||
if (spot.GameObject != null) {
|
||||
position = spot.GameObject.transform.position;
|
||||
rotation = spot.GameObject.transform.rotation;
|
||||
Destroy(spot.GameObject);
|
||||
}
|
||||
|
||||
|
||||
spot.GameObject = Instantiate(prefabs[size],
|
||||
position + new Vector3(0, diff, 0), rotation, mainPlanContainer);
|
||||
|
||||
goto whileEnd;
|
||||
spot.Size = (Size) size;
|
||||
Vector3 position = Vector3.zero;
|
||||
Quaternion rotation = Quaternion.identity;
|
||||
if (spot.GameObject != null) {
|
||||
position = spot.GameObject.transform.position;
|
||||
rotation = spot.GameObject.transform.rotation;
|
||||
Destroy(spot.GameObject);
|
||||
}
|
||||
|
||||
|
||||
spot.GameObject = Instantiate(prefabs[size],
|
||||
position + new Vector3(0, diff, 0), rotation, mainPlanContainer);
|
||||
|
||||
goto whileEnd;
|
||||
}
|
||||
}
|
||||
whileEnd:;
|
||||
|
||||
whileEnd: ;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -437,19 +436,19 @@ namespace Parking
|
||||
|
||||
GameObject spawnedSpot;
|
||||
|
||||
int[] emergencyMap = new[] {12, 27, 27, 20};
|
||||
|
||||
for (int j = 0; j < emergencyMap[i]; j++) {
|
||||
int[] emergencyMap = {12, 27, 27, 20};
|
||||
|
||||
for (int j = 0; j < emergencyMap[i]; j++)
|
||||
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,
|
||||
Quaternion.Euler(new Vector3(0, 0, 90)), emergencyPlanContainer);
|
||||
|
||||
currentX -= 5;
|
||||
}else {
|
||||
}
|
||||
else {
|
||||
bool alignTop = i % 2 != 0;
|
||||
Vector3 position = new Vector3(currentX, currentY, 0);
|
||||
Vector3 position = new(currentX, currentY, 0);
|
||||
spawnedSpot = Instantiate(Instance.spotPrefabC, position,
|
||||
Quaternion.Euler(new Vector3(0, 0, 0)), emergencyPlanContainer);
|
||||
|
||||
@ -460,7 +459,6 @@ namespace Parking
|
||||
|
||||
currentX += 2.25f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user