C# read only multidimensional array and tilemaps - c#

I made a tile map class, that only contains the data and methods to get or set tiles.
Now I have the problem, that I want to make a tile map renderer.
But I thing its not very fast to call the methods GetTile(int layerIndex, int x, int y); to iterate threw the tiles and draw them.
So I want to give the possibility to get the full array with all data.
In this case tiledMapData.
But the problem is, that I want that someone cant change the values of the array, because I want make events for changing tiles for physics.
How I can do this.
public class TiledMap
{
public int LayerCount
{
get;
private set;
}
public int TileCountX
{
get;
private set;
}
public int TileCountY
{
get;
private set;
}
public int TotalTileCount
{
get;
private set;
}
private int[,] tiledMapData;
public TiledMap(int layerCount, int tileCountX, int tileCountY)
{
LayerCount = layerCount;
TileCountX = tileCountX;
TileCountY = tileCountY;
TotalTileCount = TileCountX * TileCountY;
tiledMapData = new int[LayerCount, TotalTileCount];
for (int layerIndex = 0; layerIndex < TotalTileCount; layerIndex++)
{
for (int tileIndex = 0; tileIndex < TotalTileCount; tileIndex++)
{
tiledMapData[layerIndex, tileIndex] = 0;
}
}
}
public int GetTile(int layerIndex, int x, int y)
{
return tiledMapData[layerIndex, y * TileCountX + x];
}
public void SetTile(int layerIndex, int x, int y, int tileType)
{
tiledMapData[layerIndex, y * TileCountX + x] = tileType;
}
}
And generally, how you would design a tilemap 100 x 100 tiles 16 bit graphics and destroy able/build able tiles, how handle physics player interaction and so on....?
Maybe someone know good tutorials(The language is not important)
Thanks for helping me :)

Okay I have to reject this, it makes a difference event when not using Length directly.
const int countX = 100;
const int countY = 100;
const int countLayer = 5;
TiledMap tm = new TiledMap(countLayer, countX, countY);
const int testRuns = 10000;
Console.WriteLine("Run 1, start");
DateTime start1 = DateTime.Now;
for (int test = 0; test < testRuns; test++)
{
for (int i = 0; i < countLayer; i++)
{
for (int j = 0; j < countX; j++)
{
for (int k = 0; k < countY; k++)
{
int data = tm.GetTile(i, j, k);
data++;
}
}
}
}
DateTime end1 = DateTime.Now;
Console.WriteLine(String.Format("Run 1, time: {0}", end1-start1));
Console.WriteLine("Run 2, start");
DateTime start2 = DateTime.Now;
for (int test = 0; test < testRuns; test++)
{
for (int i = 0; i < countLayer; i++)
{
for (int j = 0; j < countX; j++)
{
for (int k = 0; k < countY; k++)
{
int data = tm.DirectArray[i, k*countX + j];
data++;
}
}
}
}
DateTime end2 = DateTime.Now;
Console.WriteLine(String.Format("Run 1, time: {0}", end2 - start2));
Console.ReadLine();
The array access variant performed much better. Still not sure if it would when you use some kind of read only array. So make sure you check it out.

Related

how can i sort a matrix's each row from smallest to largest and then find the matrix's peak value?

i need to write a program that takes 1 to 100 integers randomly, then i need to take this program's transpose, then I need to sort this matrix (each row in itself) from smallest to largest.
and finally, i need to find this matrix's peak value. here is what i have written for the program. for now, it creates a random matrix (20x5) and then it takes its transpose. can you help me with finding its peak value and then sort its each row?
PS.: using classes is mandatory!
using System;
namespace my_matrix;
class Program
{
public int[,] Create(int[,] myarray, int Row, int Clm)
{
Random value = new Random();
myarray = new int[Row, Clm];
int i = 0;
int j = 0;
while (i < Row)
{
while (j < Clm)
{
myarray[i, j] = value.Next(1, 100);
j++;
}
i++;
j = 0;
}
return myarray;
}
public int[,] Print(int[,] myarray, int Row, int Clm)
{
Console.WriteLine("=====ARRAY=====");
for (int a = 0; a < Row; a++)
{
for (int b = 0; b < Clm; b++)
{
Console.Write(myarray[a, b] + " ");
}
Console.WriteLine();
}
return null;
}
public int[,] Transpose(int[,] myarray, int Row, int Clm)
{
for (int b = 0; b < Clm; b++)
{
for (int a = 0; a < Row; a++)
{
Console.Write(myarray[a, b] + " ");
}
Console.WriteLine();
}
return myarray;
}
public int[,] Print_Transpose(int[,] myarray, int Row, int Clm)
{
Console.WriteLine("=====TRANSPOSE=====");
for (int b = 0; b < Clm; b++)
{
for (int a = 0; a < Row; a++)
{
Console.Write(myarray[a, b] + " ");
}
Console.WriteLine();
}
return null;
}
static void Main(string[] args)
{
Program x = new Program();
int[,] myarray = new int[20, 5];
int[,] a = x.Create(myarray, 20, 5);
x.Print(a, 20, 5);
x.Print_Transpose(a, 20, 5);
}
}
i don't know how to make a class which sorta each row from smallest to largest and also i dont know how to create a class which finds the matrix's peak value.
You can run over the array, keeping the maxNum:
public int getMax(int[,] fromArray)
{
int maxNum = 0;
for (int b = 0; b < fromArray.GetUpperBound(1); b++)
{
for (int a = 0; a < fromArray.GetUpperBound(0); a++)
{
if (maxNum < fromArray[a,b])
{
maxNum = fromArray[a, b];
}
}
}
return maxNum;
}
Call the function like:
Console.Write("Max number = {0:D}", x.getMax (a));
If you want a sort method, you can place all values in a List, sort them and convert back to array.
public int[] SortArray(int[,] fromArray)
{
List<int> newList = new List<int>(fromArray.Length);
for (int b = 0; b < fromArray.GetUpperBound(1); b++)
{
for (int a = 0; a < fromArray.GetUpperBound(0); a++)
{
newList.Add (fromArray[a, b]);
}
}
newList.Sort();
return newList.ToArray<int>();
}

How to initialize 2D object

I'm trying to figure out how to start a two dimensional array of an object.
I have point class and I want to create Rectangle class that build from 2D arrays of points.
I want to initialize the public Point[,] StartPoint somting like this = new [,]Point();
What the right way to do it?
{
class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point()
{
X = 0;
Y = 0;
}
public Point(int x , int y)
{
X = x;
Y = y;
}
}
}
Rectangele class :
{
class SRectangle
{
public Point[,] StartPoint;
public int Rows ;
public int Cols ;
public SRectangle(Point start , int row, int col)
{
Rows = row;
Cols = col;
for (int i = 0; i <= Rows; i++)
{
for (int j = 0; j <= Cols; j++)
{
StartPoint[i, j] = new Point(Rows + i, Cols + j);
}
}
}
}
}
You don't need a multidimensional array to keep a store of points, they themselves store x and y. You only need a simple array f points for a rectangle, But I see your concern, what you seem to want is:
but because it is class I can't write something like this 'public Point[,] StartPoint = new Point,;
You want to access points using an index as if you were storing X and Y combinations. But You aren't, you're storing an array of objects which have information about X and Y's however there is a way to get the point by (x,y)index
Insist on using a 2d array, in which Points[x,y] will have an object with the same X and Y i.e. you might as well have stored int[,] and it wouldn't have mattered.
Treat the array as a 2d but really having a 1d array.
//my rectangle class
class Rectangle
{
public Point[,] Points;
public int Rows ;
public int Cols ;
public Rectangle(Point start, int row, int col)
{
Rows = row;
Cols = col;
Points = new Point[Rows, Cols];
for (int i = 0; i <= Rows; i++)
{
for (int j = 0; j <= Cols; j++)
{
Points[i, j] = new Point(Rows + i, Cols + j);
}
}
//now use it like Points[x, y]
}
}
//my rectangle class
class Rectangle
{
public Point[] Points;
public int Rows;
public int Cols;
public Rectangle(Point start, int row, int col)
{
Rows = row;
Cols = col;
Points = new Point[Rows * Cols];
for (int i = 0; i <= Rows; i++)
{
for (int j = 0; j <= Cols; j++)
{
Points[(i * Cols) + j] = new Point(Rows + i, Cols + j);
}
}
}
//return the point if it exists else null
public Point? GetPoint(int x, int y)
{
int index = (i * Cols) + j;
if (index < Points.Length)
return Points[index];
return null;
}
}

My neural networks works well only with no hidden layers

You may think I am crazy, but I decided to write a neural network from scratch in C# for studying purposes. Please, be patient, I still have little experience)). English is not my first language, so I am sorry for it in advance.
I started with a program for handwritten digit recognizing with the MNIST database. I've read through a book about the algorithms inside the process and wrote this code.
public class NeuralNetwork
{
private List<Matrix<double>> weights = new List<Matrix<double>>();
private List<Vector<double>> biases = new List<Vector<double>>();
private Random random = new Random();
private List<Image> test_data;
private int[] layer_sizes;
public NeuralNetwork(params int[] layers)
{
layer_sizes = layers;
for (int i = 0; i < layers.Length - 1; i++)
{
var weigthLayer = Matrix<double>.Build.Dense(layers[i + 1], layers[i], (k, j) => random.NextDouble());
weights.Add(weigthLayer);
}
for (int i = 1; i < layers.Length; i++)
{
var biasesLayer = Vector<double>.Build.Dense(layers[i], (k) => random.NextDouble());
biases.Add(biasesLayer);
}
}
public Vector<double> FeedForward(Vector<double> a)
{
for (int i = 0; i < weights.Count; i++)
{
a = Sigmoid(weights[i].Multiply(a) + biases[i]);
}
return Sigmoid(a);
}
public void SGD(ITrainingDataProvider dataProvider, int epochs, int chuck_size, double eta)
{
test_data = new MnistReader().ReadTestData();
Console.WriteLine("SGD algorithm started");
var training_data = dataProvider.ReadTrainingData();
Console.WriteLine("Training data has beeen read");
Console.WriteLine($"Training data test: {Test(training_data)}%");
Console.WriteLine($"Test data test: {Test(test_data)}%");
for (int epoch = 0; epoch < epochs; epoch++)
{
training_data = training_data.OrderBy(item => random.Next()).ToList();
List<List<Image>> chunks = training_data.ChunkBy(chuck_size);
foreach (List<Image> chunk in chunks)
{
ProcessChunk(chunk, eta);
}
Console.WriteLine($"Epoch: {epoch + 1}/{epochs}");
Console.WriteLine($"Training data test: {Test(training_data)}%");
Console.WriteLine($"Test data test: {Test(test_data)}%");
}
Console.WriteLine("Done!");
}
private double Test(List<Image> data)
{
int count = 0;
foreach (Image im in data)
{
var output = FeedForward(im.DataToVector());
int number = output.MaximumIndex();
if (number == (int)im.Label)
{
count++;
}
}
return (double)count / data.Count * 100;
}
private void ProcessChunk(List<Image> chunk, double eta)
{
Delta[] deltas = new Delta[chunk.Count];
for (int i = 0; i < chunk.Count; i++)
{
Image image = chunk[i];
var input = image.DataToVector();
var desired_output = Vector<double>.Build.Dense(layer_sizes[layer_sizes.Length - 1]);
desired_output[(int)image.Label] = 1;
Delta delta = BackPropagation(input, desired_output);
deltas[i] = delta;
}
Delta sum = deltas[0];
for (int i = 1; i < deltas.Length; i++)
{
sum += deltas[i];
}
Delta average_delta = sum / deltas.Length;
for (int i = 0; i < layer_sizes.Length - 1; i++)
{
weights[i] += average_delta.d_weights[i].Multiply(eta);
biases[i] += average_delta.d_biases[i].Multiply(eta);
}
}
private Delta BackPropagation(Vector<double> input, Vector<double> desired_output)
{
List<Vector<double>> activations = new List<Vector<double>>();
List<Vector<double>> zs = new List<Vector<double>>();
Vector<double> a = input;
activations.Add(input);
for (int i = 0; i < layer_sizes.Length - 1; i++)
{
var z = weights[i].Multiply(a) + biases[i];
zs.Add(z);
a = Sigmoid(z);
activations.Add(a);
}
List<Vector<double>> errors = new List<Vector<double>>();
List<Matrix<double>> delta_weights = new List<Matrix<double>>();
List<Vector<double>> delta_biases = new List<Vector<double>>();
var error = CDerivative(activations[activations.Count - 1], desired_output).HProd(SigmoidDerivative(zs[^1]));
errors.Add(error);
int steps = 0;
for (int i = layer_sizes.Length - 2; i >= 1; i--)
{
var layer_error = weights[i].Transpose().Multiply(errors[steps]).HProd(SigmoidDerivative(zs[i - 1]));
errors.Add(layer_error);
steps++;
}
errors.Reverse();
for (int i = layer_sizes.Length - 1; i >= 1; i--)
{
var delta_layer_weights = (errors[i - 1].ToColumnMatrix() * activations[i - 1].ToRowMatrix()).Multiply(-1);
delta_weights.Add(delta_layer_weights);
var delta_layer_biases = errors[i - 1].Multiply(-1);
delta_biases.Add(delta_layer_biases);
}
delta_biases.Reverse();
delta_weights.Reverse();
return new Delta { d_weights = delta_weights, d_biases = delta_biases };
}
private Vector<double> CDerivative(Vector<double> x, Vector<double> y)
{
return x - y;
}
private Vector<double> Sigmoid(Vector<double> x)
{
for (int i = 0; i < x.Count; i++)
{
x[i] = 1.0 / (1.0 + Math.Exp(-x[i]));
}
return x;
}
private Vector<double> SigmoidDerivative(Vector<double> x)
{
for (int i = 0; i < x.Count; i++)
{
x[i] = Math.Exp(-x[i]) / Math.Pow(1.0 + Math.Exp(-x[i]), 2);
}
return x;
}
}
Delta class. A simple DTO to store weights and biases changes in a single object.
public class Delta
{
public List<Matrix<double>> d_weights { get; set; }
public List<Vector<double>> d_biases { get; set; }
public static Delta operator +(Delta d1, Delta d2)
{
Delta result = d1;
for (int i = 0; i < d2.d_weights.Count; i++)
{
result.d_weights[i] += d2.d_weights[i];
}
for (int i = 0; i < d2.d_biases.Count; i++)
{
result.d_biases[i] += d2.d_biases[i];
}
return result;
}
public static Delta operator /(Delta d1, double d)
{
Delta result = d1;
for (int i = 0; i < d1.d_weights.Count; i++)
{
result.d_weights[i] /= d;
}
for (int i = 0; i < d1.d_biases.Count; i++)
{
result.d_biases[i] /= d;
}
return result;
}
}
Everything ended up working fine, however complex networks with 1 or more hidden layers don't show any significant results. They are getting best 70% accuracy and then the learning curve drops. The accuracy returns to its 20-30%. Typically, the graph looks like a square root function, but in my case it is more like a turned around quadratic parabola the graph of my tries with different amounts of neurons in the first hidden layer
After a few tries, I found out, that without any hidden layers the algorithm works just fine. It learns up to 90% of accuracy and then the graph never falls down. Apparently, the bug is somewhere in back-propagation algorithm. It doesn't cause any problems with only input and output layers, but it does, when I add a hidden layer.
I have been trying to find the problem for a long time and I hope that someone, smarter than me, will be able to help.
Thanks in advance!

Why is bubble sort running faster than selection sort?

I am working on a project that compares the time bubble and selection sort take. I made two separate programs and combined them into one and now bubble sort is running much faster than selection sort. I checked to make sure that the code wasn't just giving me 0s because of some conversion error and was running as intended. I am using System.Diagnostics; to measure the time. I also checked that the machine was not the problem, I ran it on Replit and got similar results.
{
class Program
{
public static int s1 = 0;
public static int s2 = 0;
static decimal bubblesort(int[] arr1)
{
int n = arr1.Length;
var sw1 = Stopwatch.StartNew();
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr1[j] > arr1[j + 1])
{
int tmp = arr1[j];
// swap tmp and arr[i] int tmp = arr[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = tmp;
s1++;
}
}
}
sw1.Stop();
// Console.WriteLine(sw1.ElapsedMilliseconds);
decimal a = Convert.ToDecimal(sw1.ElapsedMilliseconds);
return a;
}
static decimal selectionsort(int[] arr2)
{
int n = arr2.Length;
var sw1 = Stopwatch.StartNew();
// for (int e = 0; e < 1000; e++)
// {
for (int x = 0; x < arr2.Length - 1; x++)
{
int minPos = x;
for (int y = x + 1; y < arr2.Length; y++)
{
if (arr2[y] < arr2[minPos])
minPos = y;
}
if (x != minPos && minPos < arr2.Length)
{
int temp = arr2[minPos];
arr2[minPos] = arr2[x];
arr2[x] = temp;
s2++;
}
}
// }
sw1.Stop();
// Console.WriteLine(sw1.ElapsedMilliseconds);
decimal a = Convert.ToDecimal(sw1.ElapsedMilliseconds);
return a;
}
static void Main(string[] args)
{
Console.WriteLine("Enter the size of n");
int n = Convert.ToInt32(Console.ReadLine());
Random rnd = new System.Random();
decimal bs = 0M;
decimal ss = 0M;
int s = 0;
int[] arr1 = new int[n];
int tx = 1000; //tx is a variable that I can use to adjust sample size
decimal tm = Convert.ToDecimal(tx);
for (int i = 0; i < tx; i++)
{
for (int a = 0; a < n; a++)
{
arr1[a] = rnd.Next(0, 1000000);
}
ss += selectionsort(arr1);
bs += bubblesort(arr1);
}
bs = bs / tm;
ss = ss / tm;
Console.WriteLine("Bubble Sort took " + bs + " miliseconds");
Console.WriteLine("Selection Sort took " + ss + " miliseconds");
}
}
}
What is going on? What is causing bubble sort to be fast or what is slowing down Selection sort? How can I fix this?
I found that the problem was that the Selection Sort was looping 1000 times per method run in addition to the 1000 runs for sample size, causing the method to perform significantly worse than bubble sort. Thank you guys for help and thank you TheGeneral for showing me the benchmarking tools. Also, the array that was given as a parameter was a copy instead of a reference, as running through the loop manually showed me that the bubble sort was doing it's job and not sorting an already sorted array.
To solve your initial problem you just need to copy your arrays, you can do this easily with ToArray():
Creates an array from a IEnumerable.
ss += selectionsort(arr1.ToArray());
bs += bubblesort(arr1.ToArray());
However let's learn how to do a more reliable benchmark with BenchmarkDotNet:
BenchmarkDotNet Nuget
Official Documentation
Given
public class Sort
{
public static void BubbleSort(int[] arr1)
{
int n = arr1.Length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr1[j] > arr1[j + 1])
{
int tmp = arr1[j];
// swap tmp and arr[i] int tmp = arr[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = tmp;
}
}
}
}
public static void SelectionSort(int[] arr2)
{
int n = arr2.Length;
for (int x = 0; x < arr2.Length - 1; x++)
{
int minPos = x;
for (int y = x + 1; y < arr2.Length; y++)
{
if (arr2[y] < arr2[minPos])
minPos = y;
}
if (x != minPos && minPos < arr2.Length)
{
int temp = arr2[minPos];
arr2[minPos] = arr2[x];
arr2[x] = temp;
}
}
}
}
Benchmark code
[SimpleJob(RuntimeMoniker.Net50)]
[MemoryDiagnoser()]
public class SortBenchmark
{
private int[] data;
[Params(100, 1000)]
public int N;
[GlobalSetup]
public void Setup()
{
var r = new Random(42);
data = Enumerable
.Repeat(0, N)
.Select(i => r.Next(0, N))
.ToArray();
}
[Benchmark]
public void Bubble() => Sort.BubbleSort(data.ToArray());
[Benchmark]
public void Selection() => Sort.SelectionSort(data.ToArray());
}
Usage
static void Main(string[] args)
{
BenchmarkRunner.Run<SortBenchmark>();
}
Results
Method
N
Mean
Error
StdDev
Bubble
100
8.553 us
0.0753 us
0.0704 us
Selection
100
4.757 us
0.0247 us
0.0231 us
Bubble
1000
657.760 us
7.2581 us
6.7893 us
Selection
1000
300.395 us
2.3302 us
2.1796 us
Summary
What have we learnt? Your bubble sort code is slower ¯\_(ツ)_/¯
It looks like you're passing in the sorted array into Bubble Sort. Because arrays are passed by reference, the sort that you're doing on the array is editing the same contents of the array that will be eventually passed into bubble sort.
Make a second array and pass the second array into bubble sort.

Null Reference Excepetion when trying to assign value to two dimensional array

Hi I have an assignment in which I need to resolve a system of linear equations using Gausian elimination method.
This is my code so far:
private static double[][] equation = new double[][]
{
new double[] {0.0, 8.0, 2.0, -7.0},
new double[] {3.0, 5.0, 2.0, 8.0},
new double[] {6.0, 2.0, 8.0, 26.0}
};
private const int rowSize = 3;
private const int columnSize = 4;
private static int pivotRow;
private static int curentRowIndex;
public static void GetTriangularMatrix()
public static void GetTriangularMatrix()
{
PrintMatrix();
for (int k = 0; k <= columnSize - 2; k++)
{
curentRowIndex = k;
double pivot = GetPivot(k);
SwapCurentRowWithPivotRow(pivot);
FindTriangularMatrix();
}
}
public static double GetPivot(int curentPivot)
{
double pivot = equation[curentPivot][curentPivot];
pivotRow = curentPivot;
for (int i = curentPivot; i <= rowSize - 1; i++)
{
if (pivot < equation[i][curentPivot])
{
pivot = equation[i][curentPivot];
pivotRow = i;
}
}
Console.WriteLine("Pivot Row is " + (pivotRow + 1));
Console.WriteLine("Pivot is " + pivot);
return pivot;
}
private static void SwapCurentRowWithPivotRow(double pivot)
{
if(pivot != equation[curentRowIndex][curentRowIndex])
{
double temp;
for (int i = 0; i <= columnSize - 1; i++)
{
temp = equation[curentRowIndex][i];
equation[curentRowIndex][i] = equation[pivotRow][i];
equation[pivotRow][i] = temp;
}
}
PrintMatrix();
}
public static void FindTriangularMatrix()
{
double[][] trangularMatrix = new double[rowSize][];
for (int j = curentRowIndex + 1; j <= rowSize - 1; j++)
{
trangularMatrix[j][curentRowIndex] = equation[j][curentRowIndex] / equation[curentRowIndex][curentRowIndex];
}
}
At FindTriangularMatrix() method when I try to assign some value to the triangularMatrix I get back a null reference exception.
I believe this is more of a syntax problem , it may be something I am missing because I have never worked with two dimensional arrays before.
Can anyone tell me what I am doing wrong and how can I correct it?

Categories