⬆️ ⬇️

Standard algorithms in practice. Calculation of chains. Part 1

During my studies I wondered more than once: all these good and cool algorithms can help in practice? And so, a couple of days ago I was faced with the task of calculating an electric current circuit. The way it was decided and what - under the cut.







I will say right away that the incomplete version of the algorithm is described below, which I will add in the next part.



â–Ť Task setting



The task is to read from the file the location of the elements of the electrical circuit, read the resistance of the whole circuit and for each section of the circuit set the strength of the current flowing through it. For simplicity, let's assume that the elements are placed on a 5x3 board in free cells.



For the first time, we confine ourselves to 4 elements: a resistor, a battery, a light bulb, and a pacifier that connects the circuit section (without internal resistance). Now, when the task is more / less set, you can begin to implement.

')

â–Ť Internal designations



For definiteness, we denote 0 for a dummy, 1 for a resistor of 200 ohm, 2 for 500 ohm and 3 for 1k ohm. Minus means space. We put the battery and the light bulb in advance at points 1.2 and 5.2, respectively:







â–ŤRead File



Since the file name is not a constant, and the algorithm needs to be made universal, the file name will be a transmitted parameter.



View of the level stored with the file:



0 0 2 0 0

0 - - - 0

0 0 0 0 0


We read line by line (means through an array of strings) and then parse each line separately. The result for each empty space in the board is entered into the PlayerPrefs, so that you can work with the elements regardless of the source file reader.



Code to read from file
public string name;

private string [] s;

private string str;

void Start ()

{

s = System.IO.File.ReadAllLines (name);

r = s.Length;

i = 0;

while (i <s.Length) {

str = s [i];

j = 0;

while (j <str.Length)

{

if (str [j] == '-') PlayerPrefs.SetInt ((i + 1) .ToString () + “x” + ((jj% 2) / 2 + 1) .ToString (), - 1);

if (str [j] == '0') PlayerPrefs.SetInt ((i + 1) .ToString () + “x” + ((jj% 2) / 2 + 1) .ToString (), 0);

if (str [j] == '1') PlayerPrefs.SetInt ((i + 1) .ToString () + “x” + ((jj% 2) / 2 + 1) .ToString (), 1);

if (str [j] == '2') PlayerPrefs.SetInt ((i + 1) .ToString () + "x" + ((jj% 2) / 2 + 1) .ToString (), 2);

if (str [j] == '3') PlayerPrefs.SetInt ((i + 1) .ToString () + “x” + ((jj% 2) / 2 + 1) .ToString (), 3);

j ++; j ++;

}

i ++;

}

}



Traversal algorithm



Here it is necessary to make a reservation. Since working with the stack caused some difficulties, implementation through queues and stacks will be discussed in the second part. Now we will analyze the case when there is no branching (extremely limited, but for sizes 5x3, this is the most).



Let's set the starting point of the round (we have it from the battery to the other entrance to the battery). In order not to go in the opposite direction from each new point in the board, we will make variables into which we will record our previous dislocation. And another will need a variable in which we will remember all the resistances that we encountered. For safety, let's make a variable that will be responsible for the number of iterations done in case the circuit is not closed or there are cycles in it.



Let's run forward a bit and limit the movements along the resistors: if the resistor is horizontal (1-3), then you can only move vertically, and if horizontal (4-6 and this is in the next chapter), then the movements are only available horizontally.



Practical implementation
xr = 2; yr = 1;

xn = 1; yn = 1;

r = 0;

step = 0; tick = 0;



while ((! ((xn == 2) && (yn == 1))) && (step <500))

{

step ++;

p = PlayerPrefs.GetInt (xn.ToString () + "x" + yn.ToString ());

if (p == 1) r = r + 200;

if (p == 2) r = r + 500;

if (p == 3) r = r + 1000;



PlayerPrefs.SetInt (xn.ToString () + "x" + yn.ToString () + "R", r);



if ((((xn + 1) <= 3) && (PlayerPrefs.GetInt ((xn + 1) .ToString () + "x" + yn.ToString ())! = - 1)) && ((xn + 1)! = Xr) && ((p == 0) || (p> = 4)))

{

tick ++;

xr = xn;

yr = yn;

xn = xn + 1;

}

else

if ((((xn-1)> = 1) && (PlayerPrefs.GetInt ((xn-1) .ToString () + "x" + yn.ToString ())! = - 1)) && ((xn- 1)! = Xr) && ((p == 0) || (p> = 4)))

{

tick ++;

xr = xn;

yr = yn;

xn = xn-1;

}

else

if ((((yn + 1) <= 5) && (PlayerPrefs.GetInt (xn.ToString () + "x" + (yn + 1) .ToString ())! = - 1)) && ((yn + 1)! = Yr) && (p <= 4))

{

tick ++;

yr = yn;

xr = xn;

yn = yn + 1;

}

else

if ((((yn-1)> = 1) && (PlayerPrefs.GetInt (xn.ToString () + "x" + (yn-1) .ToString ())! = - 1)) && ((yn- 1)! = Yr) && (p <= 4))

{

tick ++;

yr = yn;

xr = xn;

yn = yn-1;

}

}



The resulting resistance is processed and saved in PlayerPrefs. If the resistance is zero, then we pre-set the current equal to 1 amp. If more than 500 steps were completed, then there is a cycle in the scheme or the circuit is not closed.



Processing and calculation
if (r == 0) ir = 1.0f;

PlayerPrefs.SetInt ("SxemeR", r);

if ((step <500) && (r! = 0)) ir = 9.0f / r; else

if (step> = 500)

{

PlayerPrefs.SetInt ("SxemeR", -1);

ir = 0.001f;

}



To get the current strength, we divide the voltage in the battery (9V) by the resistance in the circuit. And we will write the result into each element of the chain (for this we will go through the chain once again according to the algorithm described above).



â–ŤIn the conclusion of the first part



- The described techniques allow you to create a system in which the user himself plans and places the elements of the chain;

- The considered method is intuitive, but does not cover all the requirements for calculations;

- The next chapter will describe how to bypass the chain, using the stack, in order not only to simplify the code, but also to solve the problem with loops and forks (parallel connections in the chain);



PS Models used in the project can be downloaded here .

Source: https://habr.com/ru/post/312578/



All Articles