Blue Theme Orange Theme Green Theme Red Theme
 
Safari Books Online
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » VB.NET » Solving Polynomial Equations with Complex Roots using Genetic Algorithms in VB.Net

Solving Polynomial Equations with Complex Roots using Genetic Algorithms in VB.Net


This article features a program in which the user can enter a polynomial equation and it will use GAs to determine the complex roots.

Author Rank:
Total page views :  13419
Total downloads :  238
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
GARootSolver.zip
 
Become a Sponsor

Edited by Nina Miller

Introduction

It always amazes me how effective the process of trial and error is at reaching the desired solution.  Genetic Algorithms are trial and error in its purest form, each generation getting closer and closer to the solution defined by a given set of fitness parameters. This article combines the CodeDOM Calculator with the Compact Genetic Algorithm to create a root solving application. Using this application, you can type in any polynomial, and the genetic algorithm will attempt to converge on the best solution for the equation. The solution can be real or complex. Below is a sample of converging on the solution to the equation:

x2 + 2x + 10 = 0

Figure 1 - Running GA Root Solver to find the complex roots of the equation x2 + 2x + 10 = 0

Essentially the solution derived by the Compact GA was (-1 ± 3i).  Let's test this solution with one of the roots (-1 +3i):

= (-1 + 3i)(-1 + 3i) + 2(-1 + 3i) + 10

 = (1 - 9 -6i) + (-2 + 6i) + 10

= (1 - 9 - 2) + (-6i + 6i) + 10

= -10 + 0  + 10

= 0

Let's even try  a more difficult equation:

x4 - 7x2 + 14x + 25 = 0

The GA Solver converges on a solution fairly quickly:

      

Figure 2 - Running GA Root Solver to find the complex roots of the equation x4 - 7x2 + 14x + 25 = 0

Again testing our GA solution:

= (-1.20797729492188)4 - 7(-1.20797729492188)2 + 14(-1.20797729492188) + 25

= 2.129291328988 - 10.2144640153275 + -16.91168212890632 + 25

= -24.997 + 25

= .003

This gives us a solution off by 3/1000.  Note that if we compare the fitnesses for the figure 1 solution and the figure 2 solution,  figure 1 had a fitness of 14913.  Figure 2 had a fitness of approximately 101 which is much lower than figure 1's fitness. The fitness measurement reflects how confident we are of our converged solution.

The Design

The design consists of two namespaces: the CompactGA and the CodeDomStuff. The CompactGA executes a compact genetic algorithm on a BinaryChromosome. Each binary chromosome contains a string of 1's and 0's representing a complex number. Because manipulating complex numbers requires complex math, we've also included a Complex class to handle multiplication, addition, and subtraction of complex numbers.

Figure 3 - UML Design of GARootSolver Reverse Engineered from VB.Net using WithClass

Fitness Function

The fitness function in the BinaryChromosome class, CalculateFitness, calls CalculateEquationRoot to determine the fitness of the binary chromosome representing a possible complex root solution. The fitness is determined by plugging the complex chromosome value back into the equation we are trying to solve. The closer the resulting value is to 0, the higher the fitness and the closer to our desired solution.  

Listing 1 - Calculating the fitness of the Complex Chromosome

Public Sub CalculateFitness()

    _format = "f"

    CalculateEquationRoot()

End Sub 'CalculateFitness

 

Sub CalculateEquationRoot()

    'form the complex number from the 1's and 0's in the binary array

    Dim root As Complex = FormComplexNumber(kChromosomeLength)

    ' plug into equation

    Dim result As Complex = Class1.DynamicFitnessFunction.RunCode(root)

    ' compare the value of the product to the actual input number

    ' and compute a fitness, result of 0 is best

    _fitness = 1.0F / Math.Abs(((result.real * result.real + result.imag * result.imag) * 1000))

End Sub 'CalculateEquationRoot

Forming the Complex Number

The complex number is formed by taking the binary chromosome and splitting it into two equal parts: a real binary part and a complex binary part. So for example, if our binary chromosome has the value shown below and we were only dealing in integers:

11000101

Then the resulting complex number would be:

1100 + 0101i

or

12 + 5i

In our case, we want to support fractional values so we scale our binary values by a factor of (2)chromosome_length/4.   Our chromosomes are 60 bits long, so the number would be scaled down by 2(60/4) or 215 or 32768.  Therefore the above value of 1100 + 0101i   would really be equivalent to 

 12/32768 + 5/32768i   or

 .00032199 + .000152587

The code for forming the complex number from the binary chromosome is shown in listing 2:

Listing 2 - Forming a complex number from a string of bits

Public Function FormComplexNumber(ByVal length As Integer) As Complex

    Dim result1 As Double = 0

    Dim result2 As Double = 0

    Dim real As Long = 0

    Dim imag As Long = 0

    ' compute the real part of the binary chromosome

    ' in the top half of the binary array

    Dim i As Integer

    For i = 0 To (length / 2 - 1) - 1

        real = Machine.Shift.Left(real, 1)

        real = real + CLng(_geneArray((length - i - 1)))

    Next i

    ' scale the real part by 2 ^ (genome_length/4)

    result1 = CDbl(real)

    Dim scale As Long = Machine.Shift.Left(1L.ToUInt32(), (length / 4))

    result1 = result1 / scale

    ' first bit is a sign bit

    If _geneArray((length / 2 + 1)) = 1 Then

        ' negative

        result1 = -result1

    End If

    ' compute the imaginary part of the binary chromosome

    ' in the bottom half of the binary array

    Dim i As Integer

    For i = length / 2 To (length - 1) - 1

        imag = Machine.Shift.Left(imag, 1)

        imag = imag + CLng(_geneArray((length - i - 1)))

    Next i

    ' scale the imaginary part by 2 ^ (genome_length/4)

    result2 = CDbl(imag)

    scale = Machine.Shift.Left(1L.ToUInt32(), (length / 4))

    result2 = result2 / scale

    ' first bit is a sign bit

    If _geneArray(0) = 1 Then

        ' negative

        result2 = -result2

    End If

    ' return a complex number instance formed

    ' from the real and imaginary binary parts

    Return New Complex(result1, result2)

End Function 'FormComplexNumber

Equation Input Features 

As in our  CodeDOM Calculator, we take the input equation string from the user and refine it so that .NET can compile it dynamically. One of the optimizations we make is to allow the user to enter x^3 as opposed to x*x*x.  Therefore, internally, we need to make the change before it reaches the compiler. The method RefineEvaluationString in the ClassExpressionCreator shows some of the substitutions we need to make before compiling the code dynamically using CodeDOM. 

Listing 3 - Refining our input equation to make it compileable

Function RefineEvaluationString(ByVal eval As String) As String

    ' look for regular expressions with only letters

    Dim regularExpression As New Regex("[a-zA-Z_]+")

    ' track all functions and constants in the evaluation expression we already replaced

    Dim replacelist As New ArrayList()

    ' find all alpha words inside the evaluation function that are possible functions

    Dim matches As MatchCollection = regularExpression.Matches(eval)

    Dim m As Match

    For Each m In matches

        ' if the word is found in the math member map, add a Math prefix to it

        Dim isContainedInMathLibrary As Boolean = Not (_mathMembersMap(m.Value.ToUpper()) Is Nothing)

        If replacelist.Contains(m.Value) = False AndAlso isContainedInMathLibrary Then

            eval = eval.Replace(m.Value, "Math." + _mathMembersMap(m.Value.ToUpper()))

        End If

        ' we matched it already, so don't allow us to replace it again

        replacelist.Add(m.Value)

    Next m

    Dim regularExpression2 As New Regex("[0-9]x")

    ' find all matches of type numx

    matches = regularExpression2.Matches(eval)

    Dim evalOld As String = eval

    Dim count As Integer = 0

    Dim m As Match

    For Each m In matches

        count += 1

        ' if the word is found in the math member map, add a Math prefix to it

        eval = eval.Substring(0, m.Index + count) + "*" + eval.Substring((m.Index + count))

    Next m

    Dim regularExpression3 As New Regex("x\^[0-9\.\-]+")

    ' find all matches of type numx

    matches = regularExpression3.Matches(eval)

    Dim m As Match

    For Each m In matches

        count += 1

        ' if the word is found in the math member map, add a Math prefix to it

        ' eval = eval.Replace(m.Value, "Math.Pow(x," + m.Value.Substring(m.Value.IndexOf("^") + 1) + ")");

        Dim replaceString As String = "x"

        Dim i As Integer

        For i = 0 To (Convert.ToInt32(m.Value.Substring((m.Value.IndexOf("^") + 1))) - 1) - 1

            replaceString = replaceString + "*x"

        Next i

        eval = eval.Replace(m.Value, replaceString)

    Next m

    ' return the modified evaluation string

    Return eval

End Function 'RefineEvaluationString

Conclusion

Genetic Algorithms give us a powerful way to test possible solutions to complex equations and quickly converge onto a final solution. CodeDom allows to dynamically create compiled code containing the equation we wish to test and use this code in the method of our fitness function. Together, we used these two technologies to create a GA Root Solver that allowed us to input a polynomial equation and find the complex root. In testing this program, we found that the GA performed best with equation containing complex solutions with magnitudes between 0 and 1. Feel free to experiment with the code in this download. Perhaps you will find other ways to refine the input or improving upon the genetic algorithm.  Either way, we trust that you will get to the root of the problem you are trying to solve with VB.NET.  

Other GA Algorithm Articles by the Author:

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).


Login to add your contents and source code to this article
 About the author
 
Mike Gold
Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank, JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently working on a book for using .NET with embedded systems. He can be reached at mike@c-sharpcorner.com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
GaRootSolver Execution by Dan On October 14, 2007
I have downloaded the zip file and extracted the contents. I am unable to execute the method using the application file GARootSolver. What else must be done to run the procedure?
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.