Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Go to top

JavaScript Machine Learning and Neural Networks with Encog

, 16 Oct 2012
Use Encog genetic algorithms, simulated annealing, neural networks and more with HTML5 Javascript.
heaton-javascript-ml.zip
encog-encog-javascript-6da0a8b
EncogJavascript.iml
EncogJavascript.ipr
README
encog.ipr
examples
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<style type="text/css"><!--
#example-holder
{
    border: 1px solid #000;
    padding:5px;
    background: #c0c0c0;
    width: 500px;
    height: 560px;
    position: relative;
}

#drawing-area
{
    border: 1px solid #000;
    background: white;
    width:500px;
    height:500px;
    position: absolute;
    display: inline;
}


#example-btn1
{
    border: 1px solid #000;
    position: absolute;
    display: inline;
    width: 500px;
    top:510px;
}

.test { color: red }
--></style><div id="example-holder">
    <div id="drawing-area"></div><div id="example-btn1"><input id="btnStart" type="button" value="Start" /><input id="btnStop" type="button" value="Stop" /><input id="btnSingle" type="button" value="Single" /><input id="btnClear" type="button" value="Clear" /><input id="btnBigBang" type="button" value="Big Bang" /><input id="btnShowAff" type="checkbox" checked="checked" /> Show Affinity<br />Separation: <input id="txtSeparation" type="text" size="5" value="0.25" />, Alignment: <input id="txtAlignment" type="text" size="5" value="0.5" />, Cohesion: <input id="txtCohesion" type="text" size="5" value="0.01" /><input id="btnSet" type="button" value="Set" /></div>
</div>

<script src="../encog.js"></script>
<script src="../encog-widget.js"></script>


<script type="text/javascript">
<!--//--><![CDATA[// ><!--

"use strict";
if(window.addEventListener) {
    window.addEventListener('load', function () {

        var backgroundTimer,universe;
        var btnStart, btnStop, btnClear, btnSingle, btnSet, btnBigBang;
        var txtSeparation,txtAlignment,txtCohesion;
        var constSeparation,constAlignment,constCohesion;
        var repelSpot = new Array(0,0);
        var shouldRepel = false;
        var btnShowAff;
        var COUNT = 50;
        var PARTICLE_SIZE = 10;
        var PARTICLE_SPEED = 5;
        var flock;

        function init () {
            'use strict';
            // Find the canvas element.
            universe = ENCOG.GUI.Agents2D.create('drawing-area',500,500);
            universe.reset(COUNT);

            // repel
            universe.pointerDown = function(x,y) {
                'use strict';
                var repelSpot = [x,y];
                var repulse = ENCOG.MathUtil.kNearest(repelSpot,universe.agents,COUNT,100,0,2);
                for(var i=0;i<repulse.length;i++)
                {
                    var dx = repelSpot[0] - repulse[i][0];
                    var dy = repelSpot[1] - repulse[i][1];
                    var repulseAngle = ((Math.atan2(dx, dy) * 180 / Math.PI))+180;
                    repulse[i][2]=repulseAngle;
                }
            };

            flock = ENCOG.Swarm.create(universe.agents);
            flock.callbackNeighbors = function(i,neighbors) {
                if( btnShowAff.checked ) {
                    universe.plotGroup(i,neighbors);
                }
            };

            // Attach the mousedown, mousemove and mouseup event listeners.
            btnStart = document.getElementById('btnStart');
            btnStop = document.getElementById('btnStop');
            btnClear = document.getElementById('btnClear');
            btnSingle = document.getElementById('btnSingle');
            btnBigBang = document.getElementById('btnBigBang');
            btnShowAff = document.getElementById('btnShowAff');
            btnSet = document.getElementById('btnSet');

            txtSeparation = document.getElementById('txtSeparation');
            txtAlignment = document.getElementById('txtAlignment');
            txtCohesion = document.getElementById('txtCohesion');

            btnStart.addEventListener('click', ev_start, false);
            btnStop.addEventListener('click', ev_stop, false);
            btnClear.addEventListener('click', ev_clear, false);
            btnSingle.addEventListener('click', ev_single, false);
            btnSet.addEventListener('click', ev_set, false);
            btnBigBang.addEventListener('click', ev_bigBang, false);

            ev_set();
            ev_clear();
            ev_start();
        }

        /////////////////////////////////////////////////////////////////////////////
        // Event functions
        /////////////////////////////////////////////////////////////////////////////

        // The user has started dragging (or touching), this will begin to repel
        // particles that are too close to the event location.
        function ev_start(ev)
        {
            'use strict';
            backgroundTimer = self.setInterval(ev_animate,50);
            btnStart.disabled = true;
            btnStop.disabled = false;
            btnSingle.disabled = true;
        }

        // The user has stopped dragging (or touching), this will stop repeling particles.
        function ev_stop(ev)
        {
            'use strict';
            self.clearInterval(backgroundTimer);
            btnStart.disabled = false;
            btnStop.disabled = true;
            btnSingle.disabled = false;
        }

        // Set the three flocking constants.
        function ev_set(ev)
        {
            'use strict';
            constSeparation = parseFloat(txtSeparation.value);
            constAlignment = parseFloat(txtAlignment.value);
            constCohesion = parseFloat(txtCohesion.value);
        }

        // Clear the universe to a "random" state.
        function ev_clear(ev)
        {
            'use strict';
            universe.reset(COUNT);
            flock.agents = universe.agents;
        }

        // Clear the universe by moving every particle to the center, with common
        // angles.  This creates a 100% deterministic (the same result each time),
        // yet seemingly random universe.
        function ev_bigBang(ev)
        {
            'use strict';
            universe.agents = [];
            flock.agents = universe.agents;
            for(var i=0;i<COUNT;i++) {
                universe.agents[i] = [ universe.canvas.width/2,universe.canvas.height/2,0];
            }

            ev_animate();
        }

        // Move forward by a single time slice.
        function ev_single(ev)
        {
            'use strict';
            ev_animate();
        }

        // If we are repeling, find everything that is within a 100 unit radius of the
        // repel spot, repel it.  The repel occurs by calculating the ideal angle
        // to take us to the repel spot, and then use the 180deg opposit of that angle.
        function performRepel()
        {


        }

        // This is the main loop of the program.  It is called by a timer and drives
        // the animation.  This method loops over all of the particles and performs
        // the following.
        function ev_animate()
        {
            'use strict';
            universe.advance();
            universe.render();
            flock.iteration();
        }

        /////////////////////////////////////////////////////////////////////////////
        // Downsampling functions
        /////////////////////////////////////////////////////////////////////////////


        /////////////////////////////////////////////////////////////////////////////
        // Drawing functions
        /////////////////////////////////////////////////////////////////////////////

        // cause the init function to be called.
        init();

    }, false); }


//--><!]]>
</script>

</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

JeffHeaton
Architect
United States United States
Jeff Heaton is a data scientist, PhD student and indy publisher. Jeff works primarily in the programming languages Python, R, C#, Java and C/C++. He is an active technology blogger, open source contributor, and author of more than ten books. His areas of expertise include Data Science, Predictive Modeling, Data Mining, Big Data, Business Intelligence and Artificial Intelligence. He is the lead developer for the Encog Machine Learning Framework open source project. Jeff holds a Masters Degree in Information Management from Washington University, is a Senior Member of the IEEE, a Sun Certified Java Programmer and a Fellow of the Life Management Institute.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 16 Oct 2012
Article Copyright 2012 by JeffHeaton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid