Skip to content

Commit f35c46c

Browse files
committed
optimal algo + enums
1 parent 775dc29 commit f35c46c

File tree

9 files changed

+1457
-204
lines changed

9 files changed

+1457
-204
lines changed

Assets/Scenes/FIFO.unity

Lines changed: 1172 additions & 138 deletions
Large diffs are not rendered by default.

Assets/Scripts/Algorithms/FifoAlgorithm.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public class FifoAlgorithm : MonoBehaviour
1717
public TMP_Text faultTextPrefab;
1818
public TMP_Text totalFaultText;
1919

20-
public static FifoAlgorithm instance;
21-
20+
/* public static FifoAlgorithm instance;
21+
*/
2222
// Awake is called before start
2323
private void Awake()
2424
{
25-
instance = this;
25+
//instance = this;
2626
}
2727

2828
// Start is called before the first frame update
@@ -32,11 +32,14 @@ void Start()
3232

3333
}
3434

35+
// Called when the simulate button is pressed
3536
public void SimulateFIFO()
3637
{
38+
// Get all the data from DataManager.cs
3739
int[] refString = DataManager.instance.GetRefStringArray();
3840
int frameCount = DataManager.instance.GetFrameCount();
3941

42+
// List is like an array in c#
4043
List<int> frameList = new List<int>(frameCount);
4144
HashSet<int> frameSet = new HashSet<int>();
4245
int pointer = 0;
@@ -48,9 +51,9 @@ public void SimulateFIFO()
4851
.ToArray();
4952

5053
// Get all the FrameGui
51-
/* frameGui = FindObjectsOfType<FrameGui>();*/
54+
/* frameGui = FindObjectsOfType<FrameGui>();*/
5255

53-
// Clear old children
56+
// Clear old children [to prevent them from stacking up]
5457
for (int i = 0; i < frameGui.Length; i++)
5558
{
5659
foreach (Transform child in frameGui[i].frameSlotParent.transform)
@@ -67,7 +70,7 @@ public void SimulateFIFO()
6770

6871
// Create column UI
6972
GameObject column = new GameObject("Step " + i);
70-
column.transform.SetParent(frameGui[i].frameSlotParent.transform);
73+
column.transform.SetParent(frameGui[i].frameSlotParent.transform); // set the new object parent to prevent the gui from getting messed up
7174
GuiSettings(column);
7275

7376
// If there's a same number inside the frameSet then pageFault true
@@ -128,7 +131,6 @@ public void SimulateFIFO()
128131
DataManager.instance.SetPageFault(pageFaults);
129132
}
130133

131-
132134
private void GuiSettings(GameObject column)
133135
{
134136
// Layout Settings
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using TMPro;
4+
using UnityEngine;
5+
using UnityEngine.UI;
6+
7+
public class OptimalAlgorithm : MonoBehaviour
8+
{
9+
public Transform frameSlotParent;
10+
public GameObject frameSlot;
11+
public TMP_Text faultTextPrefab;
12+
public TMP_Text totalFaultText;
13+
public FrameGui[] frameGui;
14+
15+
public static OptimalAlgorithm instance;
16+
17+
private void Awake()
18+
{
19+
instance = this;
20+
}
21+
22+
public void SimulateOptimal()
23+
{
24+
int[] refString = DataManager.instance.GetRefStringArray();
25+
int frameCount = DataManager.instance.GetFrameCount();
26+
27+
List<int> frameList = new List<int>(frameCount);
28+
int pageFaults = 0;
29+
30+
// Sort FrameGui based on hierarchy
31+
frameGui = FindObjectsOfType<FrameGui>()
32+
.OrderBy(fg => fg.transform.GetSiblingIndex())
33+
.ToArray();
34+
35+
// Clear old children
36+
foreach (var gui in frameGui)
37+
{
38+
foreach (Transform child in gui.frameSlotParent.transform)
39+
{
40+
Destroy(child.gameObject);
41+
}
42+
}
43+
44+
for (int i = 0; i < refString.Length; i++)
45+
{
46+
int currentPage = refString[i];
47+
48+
// Create column for current step
49+
GameObject column = new GameObject("Step " + i);
50+
column.transform.SetParent(frameGui[i].frameSlotParent.transform);
51+
GuiSettings(column);
52+
53+
bool isPageFault = !frameList.Contains(currentPage);
54+
55+
if (isPageFault)
56+
{
57+
pageFaults++;
58+
59+
if (frameList.Count < frameCount)
60+
{
61+
frameList.Add(currentPage);
62+
}
63+
else
64+
{
65+
// Find optimal page to replace
66+
int indexToReplace = -1;
67+
int farthestUse = -1;
68+
69+
for (int j = 0; j < frameList.Count; j++)
70+
{
71+
int futureIndex = int.MaxValue;
72+
for (int k = i + 1; k < refString.Length; k++)
73+
{
74+
if (refString[k] == frameList[j])
75+
{
76+
futureIndex = k;
77+
break;
78+
}
79+
}
80+
81+
if (futureIndex > farthestUse)
82+
{
83+
farthestUse = futureIndex;
84+
indexToReplace = j;
85+
}
86+
}
87+
88+
frameList[indexToReplace] = currentPage;
89+
}
90+
}
91+
92+
// Render current frame state
93+
for (int j = 0; j < frameCount; j++)
94+
{
95+
GameObject slot = Instantiate(frameSlot, column.transform);
96+
TMP_Text txt = slot.GetComponentInChildren<TMP_Text>();
97+
98+
if (j < frameList.Count)
99+
{
100+
txt.text = frameList[j].ToString();
101+
102+
if (isPageFault && frameList[j] == currentPage)
103+
{
104+
slot.GetComponent<Image>().color = Color.red;
105+
}
106+
}
107+
else
108+
{
109+
txt.text = "";
110+
}
111+
}
112+
113+
if (isPageFault)
114+
{
115+
Instantiate(faultTextPrefab, column.transform);
116+
}
117+
}
118+
119+
totalFaultText.text = "Total Page Faults: " + pageFaults;
120+
DataManager.instance.SetPageFault(pageFaults);
121+
}
122+
123+
private void GuiSettings(GameObject column)
124+
{
125+
VerticalLayoutGroup vlg = column.AddComponent<VerticalLayoutGroup>();
126+
vlg.spacing = 15;
127+
vlg.childForceExpandWidth = true;
128+
vlg.childForceExpandHeight = false;
129+
vlg.childControlHeight = false;
130+
vlg.childControlWidth = false;
131+
vlg.childAlignment = TextAnchor.UpperCenter;
132+
133+
RectTransform rectTransform = column.GetComponent<RectTransform>();
134+
if (rectTransform != null)
135+
{
136+
rectTransform.anchorMin = new Vector2(0, 0);
137+
rectTransform.anchorMax = new Vector2(1, 1);
138+
rectTransform.offsetMin = Vector2.zero;
139+
rectTransform.offsetMax = Vector2.zero;
140+
}
141+
else
142+
{
143+
Debug.LogError("RectTransform component not found.");
144+
}
145+
}
146+
}

Assets/Scripts/Algorithms/OptimalAlgorithm.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/AppManager/DataManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5+
6+
7+
public enum AlgorithmEnum
8+
{
9+
fifo,
10+
lru,
11+
opt
12+
}
13+
514
/// <summary>
615
/// This is where all the datas are saved
716
/// </summary>
@@ -17,6 +26,8 @@ public class DataManager : MonoBehaviour
1726
[SerializeField]
1827
private int pageFault;
1928

29+
public AlgorithmEnum algorithmEnum;
30+
2031
public static DataManager instance;
2132

2233
// Awake is called before the first frame updata
@@ -36,6 +47,7 @@ void Update()
3647
{
3748

3849
}
50+
3951

4052
public int GetFrameCount()
4153
{

Assets/Scripts/Controller/FifoFrameGuiController.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)