# Make a Jupyter notebook tutorial for the analysis of a simple circuit #202

Closed
opened this Issue Aug 10, 2018 · 25 comments

Projects
6 participants
Collaborator

## Goal

PSO can be utilized in a wide variety of fields. To broaden our collection of tutorials, we'd like to have another example where we analyse a simple circuit with PSO. For some inspiration, you can visit the example section. I propose that, for the beginning, we start by analysing the circuit shown below. It has a resistor and a diode. The end-goal is to have a nice Jupyter notebook that goes through the whole process of writing this optimization program.

## Method

As there are many models for diodes, let us use a more realistic one (a simplified Shockley equation) for this tutorial:

where:

• : diode current
• : reverse bias saturation current
• : diode voltage
• : thermal voltage (use 25.3 mV in this tutorial)

to use it in the tutorial I'd recommend to solve for :

Using the Kirchhoff voltage law we get this:

where denotes the voltage over the resistor. We can restructure it to be our cost function (optimally it is 0) for the optimization:

The absolute value is necessary because we don't want to obtain negative currents. If we write this more verbosely we see that the current is the parameter we want to optimize:

These are some sample values for the other parameters:

• : 10 V
• : 9.4 pA
• : 100 Ohm

Of course, if you know an alternative way to solve this or you have any questions about the issue don't hesitate to write a comment! I'm looking forward to seeing a first-timer writing this Jupyter notebook! 💯

## Notes

Please work on the development branch. You can find a good StackOverflow question about forking here. For a more advanced beginner guide to the GitHub workflow, there is this cheatsheet available. It is quite detailed and gives a simple overview.

### pranav1698 commented Sep 1, 2018

 Heya ljvmiranda921, I have started working on open source recently I would like to contribute on this issue

### pranav1698 commented Sep 1, 2018

 Can I have a detailed guide to how to make a jupyter notebook using python Also, I have been getting this error: Traceback (most recent call last): File "setup.py", line 8, in with open("README.md", encoding="utf8") as readme_file: TypeError: 'encoding' is an invalid keyword argument for this function when I am trying to build pyswarms in my local machine
Owner

### ljvmiranda921 commented Sep 1, 2018

 Hi @pranav1698 , welcome to PySwarms! I will hand you over to @whzup for this, since he’s more knowledgeable on this side of the codebase :) As for your setup problem, what is your Python version again? :)
Collaborator Author

### whzup commented Sep 1, 2018 • edited

Hello @pranav1698, welcome from my side as well 😄 🎊 🎉

### Regarding the creation of a Jupyter notebook:

1. Install jupyter notebook via pip (may require sudo on Unix or administrator rights on Windows):
pip install jupyter notebook

1. Navigate to the pyswarms/examples folder in your command line and type:
jupyter notebook


This will open the Jupyter notebook interface in your default browser. You'll see that it automatically opened the folder in which you executed the command jupyter notebook. Now you can create a new Jupyter notebook by clicking on New in the top right corner (choose Python 3).
You can then start working on the notebook. In the tool bar you will find a drop down menu with the default set to Code you can choose Markdown (here you can find a Markdown cheatsheet) to write text and Code to write code. 👍
The In [ ] on the left side indicates the order in which the cells were run. If it is empty it hasn't been run yet, if it contains a number n it's the nth cell that was run. To run a cell click on Run 😄 The kernel is the current "shell" your cells are running in. If you restart the kernel you restart the scope in which the cells were run so you have to rerun them again. For a more detailed description you can also consult the Jupyter documentation

You can use the Inverse Kinematics Tutorial as a reference for the layout and formatting (you can open them in the Jupyter notebook interface in your browser and doubleclick on the Markdown cells to see the formatting).

### Regarding your issue with the setup.py:

It seems like you use Python 2.X. The open() function does not have an encoding parameter in Python 2 and pyswarms does not support Python 2 anymore so make sure you use Python 3 for the development. If you use a Unix system try installing it using python3 instead of python since on some Linux distributions python is an alias for python2 (e.g. OpenSUSE):
python3 setup.py install

If you use Windows install a Python 3 version. You can have different Python versions on your PC without any problems.

#### Alternatively:

In case this does not work you can try the following command:
pip install git+git://github.com/ljvmiranda921/pyswarms.git@development#egg=pyswarms
This usually works for me.

In case you have any question don't hesitate to ask in this thread 😃 In the issue description above you find more information about the GitHub workflow in case you need them (I use them as well). I appreciate your commitment to this issue and hope you have fun working on it! 😄 🥇

### pranav1698 commented Sep 1, 2018

 Thanks for the help Sure, The concept is new and unique henceforth I will try my best to solve it

### pranav1698 commented Sep 1, 2018

 Hey again, I think the circuit-diagram is a bit of confusing, Seeing the diagram, If the arrow on the voltage source U, I think this points from lower voltage to higher voltage (If it is not the direction of electric field) so this should be the direction of current but the direction of current I is opposite.
Collaborator Author

### whzup commented Sep 1, 2018 • edited

 Hello, @pranav1698. Good question! There are two types of electric components: active and passive ones. For passive components it is true that the arrow of the voltage and the current arrow point in the same direction. This is because they "consume" power, that is if voltage and current have the same sign the power is always positive. Resistors and silicium diodes are examples for passive elements. Contrary to passive elements are active ones. They have the voltage arrow point in the opposite direction of the current. As you may imagine this is because active elements "produce" power, that is the power is always negative (voltage and current always have different signs). The voltage source is exactly one of these active elements and thus the voltage arrow points from the higher voltage node to the lower voltage node. This all is a convention, though. If you want to read more about it's called Passive sign convention.

### pranav1698 commented Sep 2, 2018 • edited

 Hello whzup, I have worked on some of the changes locally, so can I create a pull request for my working branch So that you can see if they are fine. One more doubt, After researching more about the particle swarm optimisation and other optimisation techniques, I have considered some things: *We are having a function c = |U - vtlog(I / Is) - RI| Here, U, Is, R can be seen as parameters that would be the target for all the particles for a given values of U, Is, R So the target vector must be T: [U, vt, Is, R](like [10, 0.00253, 9.4pA, 100]) ** I am still stuck on what my parameter vector and how to use it? Please check if I am right or not?
Collaborator Author

### whzup commented Sep 2, 2018

 @pranav1698, sure go ahead! Make sure to update your branch before doing the PR (you can use the links in the issue description for information on how to do that). There is actually no target vector in this optimization (I guess you're referring to the Inverse Kinematics tutorial by saying target vector). I think it's not clear from the description what it means to analyse a circuit, that's a mistake on my part 😕 The goal of the analysis of this circuit is to find out what the current I is. The cost function c is only dependent on the parameter I. The other ones (U, vT, Is and R) are all constant values. Hence, this is a one dimensional optimization problem for the parameter I (i.e. your optimization scalar). So you don't have a target vector that you have to optimize to, you just want to minimize the cost function c. Regarding the "how to use it" question: You can write a cost function c which takes the current I as an input and returns a scalar (!) you can then use the cost decorator to vectorize the cost function which makes it ready for the optimization. I hope this answers your questions. If there are still things that are not clear just ask 😄.

### pranav1698 commented Sep 3, 2018

 @whzup, I was not conferring to the target vector given in the inverse kinematics problem, I was confused on the target of the particles in the swarm, Can you give me a example in which pso is used for a mathematical function, I am having a hard time, on finding what pbest, gbest, would be I have recognised that we have to find a value of I for which the value of cost function is minimum(given the values of U,Is,Vt,R) but how will value of I change; is it some kind of range that I have to provide or is it preconfigured
Collaborator Author

### whzup commented Sep 4, 2018

 Hey @pranav1698, excuse my late respone I'm on vacation and only have access to my laptop in the evening 😄 The target of the particles in the swarm is the minimum of the function c (see above). We don't know this value though! So the particles memorise their personal best position (pbest) and the best position of all particles (gbest) in order to find the best position (minimum) in the search space. These two positions gbest and pbest are used as a reference for the particles. For a brief, more in-depth explanation look here. PSO is always used with mathematical functions. If you have a very basic function say for example x^2 you can write it directly in your cost function: @pyswarms.cost def my_cost_func(x): return x**2 If you have a more complex function like for example in the Inverse Kinematics tutorial it may be more sensible to make several helper functions so you don't cram full your cost function. The value of I changes as the particles move around. While they move around they memorise the pbest and gbest and at the end of the optimization loop the best value that has been found by the swarm is returned. I had a look at your PR and the next steps for you are: Set up an optimization loop with pyswarms (you can use this example as a reference for how this is achieved) Write a cost function c. For example: @pyswarms.cost def my_cost_func(x): cost = do_something(x) return cost you can use the function c in the issue description for this one. When the steps 1 and 2 work you can try to use the GeneralOptimizerPSO. It has almost the same API as the GlobalBestPSO except the fact that you have to pass a topology as an argument at initialization (see link for an example). Describe the steps you made to write the optimisation in detail. If you want to you can add some other experiments. For example you could plot the cost history or any idea that comes to your mind 😄 I hope I understood your questions and could answer them to your satisfaction if not feel free to let me know!

### pranav1698 commented Sep 4, 2018 • edited

 You liked the changes and the introduction and description up until now?

### pranav1698 commented Sep 6, 2018

 I am getting NameError when I am using @pyswarms.cost It says; name 'pyswarms' is not defined Is it because of the python version? Also, I have made the commmits with the present changes? Please have a look..
Owner

### ljvmiranda921 commented Sep 6, 2018 • edited

 Hi @pranav1698 , I am getting NameError when I am using @pyswarms.cost It is because in the earlier cell, you already called: import pyswarms as ps It then sets an alias to the pyswarms module. To call pyswarms.cost, just say ps.cost. I think everything looks OK so far! Keep at it! 👍

### pranav1698 commented Sep 6, 2018

 Hi @ljvmiranda921 , Nopes I am getting the same error with ps; I had tried that earlier too Looks Like its the preoblem for the python version, as I am working on ubuntu and I cannot delete the default python version(2.7.15) as it is not suggested
Owner

### ljvmiranda921 commented Sep 6, 2018 • edited

 Hi @pranav1698 , Looks Like its the preoblem for the python version, as I am working on ubuntu and I cannot delete the default python version(2.7.15) as it is not suggested Yes, you cannot delete the python 2 version in your system (it's dangerous). What you can do instead is install a Python 3.X distribution like Anaconda, set that as your default python whenever you type python in the terminal, and use that for developing PySwarms 👍
Collaborator Author

### whzup commented Sep 6, 2018 • edited

 Hello @pranav1698, it looks good until now! Let's focus on finishing the first draft. Afterwards we can focus on the details. As for the NameError. It is probably because you didn't rerun the cells in which you define pyswarms (You define import pyswarms as ps in In[17] and use it in In[2] the number tells you the order in which you ran the cells this is important!). You already run the Jupyter notebook with Python 3.6.5 you don't need to do anything to your Python installation. But of course if you wish to install Anaconda you're free to do so it might not solve the problem, though. In the menubar there should be a Kernel menu in which you find the option "Restart kernel and run all cells", try to run this. Although, make sure you use @ps.cost instead of @pyswarms.cost when you import it like so import pyswarms as ps.

Closed

### aakritigupta0408 commented Dec 7, 2018

 @whzup May I contribute to this project? I am well versed with Python and intricacies of the Jupyter Notebook
Collaborator Author

### whzup commented Dec 7, 2018

 @aakritigupta0408 Welcome to pyswarms! Sure go ahead, it seems like the previous contributor gave up on this 🙈. If you have any questions feel free to ask. I'll gladly answer them as soon as I can 😄.

### abdurrehman11 commented Dec 18, 2018

 @whzup I am fresh in open source contribution but I have experience in Python and Anaconda and I want to contribute in this project. Can I contribute to this project ?
Collaborator Author

### whzup commented Dec 19, 2018

 Hey, @abdurrehman11 thank you for your interest in pyswarms. Unfortunately, at the moment the development of this project is a bit stale as the owner of the repository is quite busy at work and I am going to study for my winter finals. If you feel like you could handle a PR for any of the open issues you can leave a comment there. Otherwise, maybe you and @aakritigupta0408 could jointly work on this Jupyter notebook here, what do you think? 😄

### abdurrehman11 commented Dec 20, 2018

Collaborator Author

### whzup commented Dec 28, 2018

 So it seems like @aakritigupta0408 is not active at the moment 😞. If you are still interested @abdurrehman11 you may work on this issue 😄. If you have any questions feel free to ask any time 👍

### miguelcocruz commented Feb 5, 2019

 Is this still up for grabs?
Owner

### ljvmiranda921 commented Feb 9, 2019

 Hi @miguelcocruz , I think this is up for grabs now! Wanna make a PR?

Merged

### whzup added a commit that referenced this issue Feb 17, 2019

 Added notebook with electric circuit (#288) 
Add a new example for the usage of pyswarms. It shows how a simple circuit can be analyzed by using PSO.

Resolves #202
 5df7cc5 

### ljvmiranda921 added a commit that referenced this issue Feb 28, 2019

 Add notebook with electric circuit (#288) 
Add a new example for the usage of pyswarms. It shows how a simple circuit can
be analyzed by using PSO.

Resolves #202
 5b6ced9