Standard algorithms in practice. Circuit Design. Part 1

  • Tutorial
During the training, I repeatedly asked the question: all these good and cool algorithms than can help in practice? And now, a couple of days ago I was faced with the task of calculating an electric current circuit. How this was decided and by 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 the location of the elements of the electric circuit from the file, read the resistance of the entire circuit and for each section of the circuit to establish the strength of the current flowing through it. To simplify, we assume that the elements are placed on a 5x3 board in free cells.

For the first time, we restrict ourselves to 4 elements: a resistor, a battery, a light bulb, and a dummy that connects a portion of the circuit (without internal resistance). Now that the task is more / less posed, we can proceed to implementation.

▍ Internal designations


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



▍Read file


Since the file name is not constant, but the algorithm needs to be made universal, the file name will be the passed parameter.

Type of level storing with file:

0 0 2 0 0
0 - - - 0
0 0 0 0 0 0

We read it line by line (which means through an array of lines) and then parse each line separately. The result for each empty space in the board is entered in PlayerPrefs, so that you can work with elements regardless of the source program for reading from a file.

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 str = s [i];
j is 0;
while (j {
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 ++;
}
}

▍ Bypass Algorithm


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

Let's set the initial place of the bypass (we have it from the battery to another input to the battery). In order not to go in the opposite direction from each new point in the board, we will make the variables in which we will record our previous location. And still need a variable in which we will remember all the resistance that met. To secure, we will make a variable that will be responsible for the number of iterations done in case the circuit is not closed or has cycles in it.

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

Practical implementation
xr = 2; yr = 1;
xn = 1; yn = 1;
r is 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)) &&
{
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 stored in PlayerPrefs. If the resistance is zero, then set the current to 1 ampere in advance. If more than 500 steps have been completed, then there is a cycle in the circuit or the circuit is not closed.

Processing and settlement
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 write the result in each element of the chain (for this we will go through the chain 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 seems intuitive, but does not cover all the needs for calculations;
- The next chapter will describe how to bypass the circuit using the stack in order not only to simplify the code, but also to solve the problem with loops and branches (parallel connections in the circuit);

PS Models used in the project can be downloaded here .

Also popular now: