Click here to Skip to main content
15,881,967 members
Articles / Artificial Intelligence / Machine Learning

JavaScript Machine Learning and Neural Networks with Encog

Rate me:
Please Sign up or sign in to vote.
4.88/5 (7 votes)
16 Oct 2012Apache18 min read 88.4K   1.4K   27  
Use Encog genetic algorithms, simulated annealing, neural networks and more with HTML5 Javascript.
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<style type="text/css"><!--
#example-holder {
    border: 1px solid #000;
    padding: 5px;
    background: #c0c0c0;
    width: 650px;
    height: 445px;
    position: relative;
}

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

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

#example-btn2 {
    border: 1px solid #000;
    position: absolute;
    display: inline;
    width: 650px;
    top: 340px;
}

#example-btn3 {
    border: 1px solid #000;
    position: absolute;
    display: inline;
    width: 650px;
    top: 397px;
}

.test {
    color: red
}

--></style>
<div id="example-holder">
    <div id="universe-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="btnRandomCities" type="button" value="Random Cities"/><input id="btnCircleCities" type="button"
                                                                             value="Circle Cities"/><input
            id="btnRandomPath" type="button" value="Random Path"/></div>
    <div id="example-btn2">Cities: <input id="txtCities" type="text" size="2" value="50"/>, Stop after <input
            id="txtStable" type="text" size="2" value="50"/> stable iterations.<br/>Start Temp: <input id="txtStartTemp"
                                                                                                       type="text"
                                                                                                       size="2"
                                                                                                       value="20"/>, End
        Temp: <input id="txtEndTemp" type="text" size="2" value="2"/>, Cycles: <input id="txtCycles" type="text"
                                                                                      size="2" value="10"/><input
                id="btnSet" type="button" value="Set"/></div>
    <div id="example-btn3">
        <p id="paraStatus">Ready.</p>
    </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 MARGIN = 10;

        var backgroundTimer;
        var btnStart, btnStop, btnSet, btnSingle, paraStatus, btnRandomCities, btnCircleCities, btnRandomPath;
        var txtCities, txtStable, txtStartTemp, txtEndTemp, txtCycles;
        var universe;
        var anneal;
        var iteration;
        var constCities = 50;
        var constStable = 50;
        var lastBest;
        var stableFor;
        var constStartTemp = 10.0;
        var constStopTemp = 2.0;
        var constCycles = 10;

        function init() {

            // Find the canvas element.
            universe = ENCOG.GUI.TSP.create('universe-area', 650, 300);

            // Attach the mousedown, mousemove and mouseup event listeners.
            btnStart = document.getElementById('btnStart');
            btnStop = document.getElementById('btnStop');
            btnSet = document.getElementById('btnSet');
            btnSingle = document.getElementById('btnSingle');
            btnRandomCities = document.getElementById('btnRandomCities');
            btnCircleCities = document.getElementById('btnCircleCities');
            btnRandomPath = document.getElementById('btnRandomPath');

            paraStatus = document.getElementById('paraStatus');

            btnStart.addEventListener('click', ev_start, false);
            btnStop.addEventListener('click', ev_stop, false);
            btnSet.addEventListener('click', ev_set, false);
            btnSingle.addEventListener('click', ev_single, false);
            btnRandomCities.addEventListener('click', ev_randomCities, false);
            btnCircleCities.addEventListener('click', ev_circleCities, false);
            btnRandomPath.addEventListener('click', ev_startOver, false);

            txtCities = document.getElementById('txtCities');
            txtStable = document.getElementById('txtStable');
            txtStartTemp = document.getElementById('txtStartTemp');
            txtEndTemp = document.getElementById('txtEndTemp');
            txtCycles = document.getElementById('txtCycles');

            ev_set();
            universe.render();

        }

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


        function ev_start(ev) {
            backgroundTimer = self.setInterval(ev_animate, 100);
            btnStart.disabled = true;
            btnStop.disabled = false;
            btnSingle.disabled = true;
            btnRandomCities.disabled = true;
            btnCircleCities.disabled = true;
            btnRandomPath.disabled = true;
            btnSet.disabled = true;
        }

        function ev_stop(ev) {
            self.clearInterval(backgroundTimer);
            btnStart.disabled = false;
            btnStop.disabled = true;
            btnSingle.disabled = false;
            btnRandomCities.disabled = false;
            btnCircleCities.disabled = false;
            btnRandomPath.disabled = false;
            btnSet.disabled = false;
        }

        function ev_startOver(ev) {
            iteration = 1;
            universe.bestPath = universe.generatePath();
            lastBest = universe.calculatePathLength(universe.bestPath);
            stableFor = 0;

            anneal = ENCOG.Anneal.create(universe.bestPath);
            anneal.constStartTemp = parseFloat(txtStartTemp.value);
            anneal.constStopTemp = parseFloat(txtEndTemp.value);
            anneal.constCycles = parseInt(txtCycles.value);

            anneal.scoreSolution = function(path) {
                return universe.calculatePathLength(path);
            }

            anneal.randomize = function(path, temperature) {
                var length = path.length - 1;

                // make adjustments to city order(annealing)
                for (var i = 0; i < temperature; i++) {
                    var index1 = Math.floor(length * Math.random());
                    var index2 = Math.floor(length * Math.random());
                    var d = universe.pathDistance(path, index1, index1 + 1)
                            + universe.pathDistance(path, index2, index2 + 1)
                            - universe.pathDistance(path, index1, index2)
                            - universe.pathDistance(path, index1 + 1, index2 + 1);
                    if (d > 0) {
                        // sort index1 and index2 if needed
                        if (index2 < index1) {
                            var temp = index1;
                            index1 = index2;
                            index2 = temp;
                        }
                        for (; index2 > index1; index2--) {
                            var temp = path[index1 + 1];
                            path[index1 + 1] = path[index2];
                            path[index2] = temp;
                            index1++;
                        }
                    }
                }

            }

            universe.render();
        }

        function ev_circleCities() {
            universe.resetCircle(constCities);
            ev_startOver(ev);
        }

        function ev_set(ev) {
            // collect parameters

            constCities = parseInt(txtCities.value);
            constStartTemp = parseFloat(txtStartTemp.value);
            constStopTemp = parseFloat(txtEndTemp.value);
            constCycles = parseInt(txtCycles.value);
            constStable = parseInt(txtStable.value);
            ev_randomCities();
            ev_startOver();
            universe.render();
        }

        function ev_single(ev) {
            ev_animate();
        }
        function ev_randomCities() {
            universe.reset(constCities);
            ev_startOver(ev);
        }


        function ev_animate() {
            anneal.iteration();
            universe.currentPath = anneal.solution;
            universe.render();
            iteration++;

            var l = Math.floor(universe.calculatePathLength(universe.currentPath));
            paraStatus.innerHTML = "Iteration "+iteration+": Path length = " + l;

            if (l == lastBest) {
                stableFor++;
                if (stableFor > constStable) {
                    paraStatus.innerHTML = "Stable solution found after " + iteration + " iterations: Path length = " + l;
                    ev_stop();
                }
            }
            else {
                lastBest = l;
                stableFor = 0;
            }

        }

        /////////////////////////////////////////////////////////////////////////////
        // 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


Written By
Publisher
United States United States
Jeff Heaton, Ph.D., is a data scientist, an adjunct instructor for the Sever Institute at Washington University, and the author of several books about artificial intelligence. Jeff holds a Master of Information Management (MIM) from Washington University and a PhD in computer science from Nova Southeastern University. Over twenty years of experience in all aspects of software development allows Jeff to bridge the gap between complex data science problems and proven software development. Working primarily with the Python, R, Java/C#, and JavaScript programming languages he leverages frameworks such as TensorFlow, Scikit-Learn, Numpy, and Theano to implement deep learning, random forests, gradient boosting machines, support vector machines, T-SNE, and generalized linear models (GLM). Jeff holds numerous certifications and credentials, such as the Johns Hopkins Data Science certification, Fellow of the Life Management Institute (FLMI), ACM Upsilon Pi Epsilon (UPE), a senior membership with IEEE. He has published his research through peer reviewed papers with the Journal of Machine Learning Research and IEEE.

Comments and Discussions