/CircuitreeOfLife

Genetic algorithm to automatically design analog circuits. (Undergrad senior design project)

Primary LanguagePython

CircuitreeOfLife

Ngspice is a circuit simulator based on the popular SPICE (Simulation Program with Integrated Circuit Emphasis) program developed at UC Berkeley. It has spawned many derivations and products with various front-ends, notably PSpice and LTSpice. Using a SPICE variant to verify circuit design before manufacture has become standard practice in the electronics industry. It was clear that a circuit simulator would be necessary in order to evaluate the outputs of potential solutions. Ngspice was chosen because it could be easily automated from the command line, because it waswell-documented in the Ngspice Manual, and because it utilizes netlists to describe circuits. In all genetic algorithms, it is necessary to encode information about the characteristics of solutions somehow. This is handled in different ways depending on the problem. Binary encoding is popular, as is tree encoding for the evolution of programming. For the problem of evolving circuits, though, netlists are a handy method of describing the topology and contents of circuits. Each line in a netlist represents a component. The first element in the line is the name of the component, and must represent the type, as well. (R for resistor, L for inductor, etc.) The next two numbers represent the nodes at each end of the component. Each end is at a node in the circuit; the numbering tracks the circuit topology. Multiple components can be between the same nodes, but names must be unique. The final element in the line is the value. While the rules for voltage source values are a little more complicated, other component values are can simply be represented by the number. Order of magnitude must be specified, as the base unit is assumed to be farads, henrys, or ohms. Abbreviations such as u for micro-, m for milli-, or M for mega- can be used instead of typing the entire number. Ngspice allows for many types of analysis (DC, pole-zero, transient, etc). The one required for frequency response is AC analysis. In all test cases, AC analysis was done from 1 Hz to 100 kHz, with 10 data points per decade. Because many circuits are basically impossible to run, a number of fail-safes had to be implemented in the control code so that a failure in Ngspice would not result in the entire program exiting. All of the control code––for evaluating, for writing netlists, for running the genetic algorithm itself–– was written in Python. Circuits were implemented as a class so they could be instantiated in batches and mutated, crossed-over, etc.