예제 #1
0
        public double[][] Recognize(double[] item)
        {
            if (_storedItems.Count == 0)
            {
                return(null);
            }

            double[] retVal = ConvertToLocal(item, _midValue);

            bool hadChange = false;

            do
            {
                hadChange = false;

                foreach (int index in UtilityCore.RandomRange(0, item.Length))
                {
                    double newValue = GetValueAtIndex(index, retVal, _weights);

                    if (newValue != retVal[index])
                    {
                        hadChange     = true;
                        retVal[index] = newValue;
                    }
                }
            } while (hadChange);

            retVal = ConvertToExternal(retVal, _lowValue, _highValue);

            return(new[] { retVal });
        }
예제 #2
0
        private void DrawLines_RandomInstant(int numSamples, double half, double lineThickness)
        {
            double[] velX = _field.VelocityX;
            double[] velY = _field.VelocityY;
            double[] velZ = _field.VelocityZ;

            bool[] blocked = _field.Blocked;

            int totalSamples = numSamples * numSamples * numSamples;        // numsamples is per axis, so cube it
            int counter      = 0;

            _velocityLines.BeginAddingLines();

            foreach (int index1D in UtilityCore.RandomRange(0, _field.Size1D))
            {
                if (blocked[index1D])
                {
                    continue;
                }

                counter++;

                var index3D = _field.Get3DIndex(index1D);

                DrawLinesSprtAddLine(index3D.Item1, index3D.Item2, index3D.Item3, index1D, half, lineThickness, velX, velY, velZ);

                if (counter >= totalSamples - 1)
                {
                    // Enough lines have been drawn
                    break;
                }
            }

            _velocityLines.EndAddingLines();
        }
예제 #3
0
        private static int[] GetNeuronPositionsInitial_InitialPlateBreakdown(int count, int numPlates)
        {
            int[] retVal = new int[numPlates];

            int average   = count / numPlates;
            int remaining = count;

            for (int cntr = 0; cntr < numPlates; cntr++)
            {
                // Figure out how many to make
                if (remaining == 0)
                {
                    retVal[cntr] = 0;
                    continue;
                }

                retVal[cntr] = average;
                if (remaining % numPlates > 0)
                {
                    // Count isn't evenly distributable by numPlates, and there's enough room for a remainder, so give it to this one
                    retVal[cntr]++;
                }

                remaining -= retVal[cntr];
            }

            // Some of the plates may have remainders, and the above loop always puts them in the first plates, so shuffle it
            retVal = UtilityCore.RandomRange(0, numPlates).Select(o => retVal[o]).ToArray();

            return(retVal);
        }
예제 #4
0
        public T[] GetSamples(int count)
        {
            lock (_lock)
            {
                //TODO: May want to consider when count > one of these lists.  But probably not

                // This is used when the front of the list should be favored
                Random rand     = StaticRandom.GetRandomForThread();
                var    randFunc = new Func <int, int, int>((min, max) => min + UtilityCore.GetIndexIntoList(rand.NextPow(2), max - min));

                if (_longTermIndex < 0 && _tentativeBuffer.Count == 0)
                {
                    // Empty
                    return(new T[0]);
                }
                else if (_longTermIndex >= _abandonTentativeSize)
                {
                    #region all from longterm

                    return(UtilityCore.RandomRange(0, _longTermIndex + 1, count).       // the randrange function takes care of reducing count if greater than range
                           Select(o => _longTermBuffer[o].Item).
                           ToArray());

                    #endregion
                }
                else if (_longTermIndex < 0)
                {
                    #region all from tentative

                    // Don't pull uniform randomly from tentative.  Favor items at the front of the list
                    return(UtilityCore.RandomRange(0, _tentativeBuffer.Count, count, randFunc).
                           Select(o => _tentativeBuffer[o].Item).
                           ToArray());

                    #endregion
                }
                else
                {
                    #region mixture

                    // Pull from long term then tentative
                    return(UtilityCore.RandomRange(0, _longTermIndex + 1 + _tentativeBuffer.Count, count, randFunc).
                           Select(o =>
                    {
                        if (o <= _longTermIndex)
                        {
                            return _longTermBuffer[o].Item;
                        }
                        else
                        {
                            return _tentativeBuffer[o - (_longTermIndex + 1)].Item;
                        }
                    }).
                           ToArray());

                    #endregion
                }
            }
        }
예제 #5
0
        private static SOMResult ProcessNewItemBatch(SOMItem[] newItemBatch, bool discardDupes, double dupeDistSquared, SOMResult existing)
        {
            const int TOTALMAX = BATCHMAX * 10;

            // Items only make it here when they aren't too similar to the som nodes, but items within this list may be dupes
            if (discardDupes)
            {
                newItemBatch = DedupeItems(newItemBatch, dupeDistSquared);
            }

            if (newItemBatch.Length > BATCHMAX)
            {
                // There are too many, just take a sample
                newItemBatch = UtilityCore.RandomRange(0, newItemBatch.Length, BATCHMAX).
                               Select(o => newItemBatch[o]).
                               ToArray();
            }

            SOMItem[] existingItems = null;
            if (existing != null)
            {
                existingItems = existing.InputsByNode.
                                SelectMany(o => o).
                                Select(o => ((SOMInput <SOMItem>)o).Source).
                                ToArray();
            }

            SOMItem[] allItems = UtilityCore.ArrayAdd(existingItems, newItemBatch);



            //TODO: This is too simplistic.  See if existingItems + newItemBatch > total.  If so, try to draw down the existing nodes evenly.  Try
            //to preserve previous images better.  Maybe even throw in a timestamp to get a good spread of times
            //
            //or get a SOM of the new, independent of the old.  Then merge the two pulling representatives to keep the most diversity.  Finally,
            //take a SOM of the combined
            if (allItems.Length > TOTALMAX)
            {
                allItems = UtilityCore.RandomRange(0, allItems.Length, TOTALMAX).
                           Select(o => allItems[o]).
                           ToArray();
            }



            SOMInput <SOMItem>[] inputs = allItems.
                                          Select(o => new SOMInput <SOMItem>()
            {
                Source = o, Weights = o.Weights,
            }).
                                          ToArray();

            //TODO: May want rules to persist from run to run
            SOMRules rules = GetSOMRules_Rand();

            return(SelfOrganizingMaps.TrainSOM(inputs, rules, true));
        }
예제 #6
0
        public double[][] Recognize_RAND(double[] item)
        {
            if (_storedItems.Count == 0)
            {
                return(null);
            }

            int returnCount = StaticRandom.Next(Math.Min(4, _storedItems.Count));

            return(UtilityCore.RandomRange(0, _storedItems.Count, returnCount).
                   Select(o => ConvertToExternal(_storedItems[o].Item2, _lowValue, _highValue)).
                   ToArray());
        }
예제 #7
0
        public double[][] Recognize(double[] item)
        {
            if (_items.Count == 0)
            {
                return(null);
            }

            int returnCount = StaticRandom.Next(Math.Min(4, _items.Count));

            return(UtilityCore.RandomRange(0, _items.Count, returnCount).
                   Select(o => _items[o]).
                   ToArray());
        }
예제 #8
0
        /// <summary>
        /// This gets the raw values that will be used for RGB (could return negative)
        /// </summary>
        private static Tuple <double, double, double> GetNodeColor_RGB(VectorND nodeWeights)
        {
            int size = nodeWeights.Size;

            if (size == 0)
            {
                return(Tuple.Create(0d, 0d, 0d));
            }
            else if (size == 3)
            {
                return(Tuple.Create(nodeWeights[0], nodeWeights[1], nodeWeights[2]));
            }
            else if (size < 3)
            {
                double left  = nodeWeights[0];
                double right = nodeWeights[size - 1];

                return(Tuple.Create(left, Math1D.Avg(left, right), right));
            }

            int div = size / 3;
            int rem = size % 3;

            // Start them all off with the same amount
            int[] widths = Enumerable.Range(0, 3).
                           Select(o => div).
                           ToArray();

            // Randomly distribute the remainder
            foreach (int index in UtilityCore.RandomRange(0, 3, rem))
            {
                widths[index]++;
            }

            double r = nodeWeights.
                       Take(widths[0]).
                       Average();

            double g = nodeWeights.
                       Skip(widths[0]).
                       Take(widths[1]).
                       Average();

            double b = nodeWeights.
                       Skip(widths[0] + widths[1]).
                       Average();

            return(Tuple.Create(r, g, b));
        }
예제 #9
0
        public bool Add(Cargo cargo)
        {
            lock (_lock)
            {
                // Add it to a random cargo bay
                foreach (int index in UtilityCore.RandomRange(0, _cargoBays.Length))
                {
                    if (_cargoBays[index].Add(cargo))
                    {
                        return(true);
                    }
                }

                return(false);
            }
        }
예제 #10
0
        private void DrawLines_RandomPersist(int numSamples, double half, double lineThickness)
        {
            const double ELAPSEDURATIONSECONDS = 10;

            if (_randPersistIndices == null || DateTime.UtcNow > _sceneRemaining)
            {
                // Rebuild the indices
                bool[] blocked      = _field.Blocked;
                int    totalSamples = numSamples * numSamples * numSamples;     // numsamples is per axis, so cube it

                _randPersistIndices = UtilityCore.RandomRange(0, _field.Size1D).
                                      Where(o => !blocked[o]).
                                      Take(totalSamples).
                                      Select(o =>
                {
                    var index3D = _field.Get3DIndex(o);
                    return(new Mapping_3D_1D(index3D.Item1, index3D.Item2, index3D.Item3, o));
                }).
                                      ToArray();

                _sceneRemaining = DateTime.UtcNow + TimeSpan.FromSeconds(ELAPSEDURATIONSECONDS);
            }

            double[] velX = _field.VelocityX;
            double[] velY = _field.VelocityY;
            double[] velZ = _field.VelocityZ;

            _velocityLines.BeginAddingLines();

            foreach (var index in _randPersistIndices)
            {
                DrawLinesSprtAddLine(index.X, index.Y, index.Z, index.Offset1D, half, lineThickness, velX, velY, velZ);
            }

            _velocityLines.EndAddingLines();
        }
예제 #11
0
        private static void GetInitialKMeansNodes(out SOMNode[] returnNodes, out ISOMInput[][] inputsByNode, int numNodes, ISOMInput[] inputs)
        {
            // Can't have more nodes than inputs
            numNodes = Math.Min(numNodes, inputs.Length);

            List <ISOMInput>[] lists = Enumerable.Range(0, numNodes).
                                       Select(o => new List <ISOMInput>()).
                                       ToArray();

            // Randomly assign inputs to nodes
            //NOTE: K-Means requires that all nodes contain inputs
            int listIndex = 0;

            foreach (int inputIndex in UtilityCore.RandomRange(0, inputs.Length))
            {
                lists[listIndex].Add(inputs[inputIndex]);

                listIndex++;
                if (listIndex >= numNodes)
                {
                    listIndex = 0;
                }
            }

            // Build final arrays
            returnNodes = Enumerable.Range(0, numNodes).
                          Select(o => new SOMNode()).
                          ToArray();

            inputsByNode = lists.
                           Select(o => o.ToArray()).
                           ToArray();

            // Set each node's center equal to the center of the inputs
            AdjustKMeansCenters(returnNodes, inputsByNode);
        }
예제 #12
0
        private void TimerAnyThread_Elapsed_ORIG(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (_isDisposed)
            {
                return;
            }

            double elapsedTime = (e.SignalTime - (DateTime)_lastAnyUpdate).TotalSeconds;

            _lastAnyUpdate = e.SignalTime;

            Type[] typesUnchecked = _typesAnyUnchecked;
            if (typesUnchecked != null)
            {
                #region Get intervals for types

                Tuple <Type, int>[] checkedTypes = GetTypeIntervals(typesUnchecked, _map, false);
                if (checkedTypes != null)
                {
                    lock (_lockTypesAny)
                    {
                        // Do it again with the volatile to be safe
                        checkedTypes = GetTypeIntervals(_typesAnyUnchecked, _map, false);
                        if (checkedTypes != null)
                        {
                            var transfer = TransferTypes(_typesAnyUnchecked, _typesAny, checkedTypes);
                            _typesAnyUnchecked = transfer.Item1;
                            _typesAny          = transfer.Item2;
                        }
                    }
                }

                #endregion
            }

            Tuple <Type, int>[] typesAny = _typesAny;
            long count = Interlocked.Increment(ref _updateCount_Any);

            #region non map items

            var nonmapItems = _nonmapItemsAny;

            if (nonmapItems != null)
            {
                foreach (var item in nonmapItems)
                {
                    if (count % (item.Item1 + 1) != 0)
                    {
                        continue;
                    }

                    item.Item2.Update_AnyThread(elapsedTime + (elapsedTime * item.Item1));        // if skips is greater than zero, then approximate how much time elapsed based on this tick's elapsed time
                }
            }

            #endregion
            #region items in map

            if (typesAny != null)
            {
                foreach (var type in typesAny)
                {
                    if (type.Item2 > 0 && count % (type.Item2 + 1) != 0)
                    {
                        continue;
                    }

                    // Update all the live instances in a random order
                    IMapObject[] items = _map.GetItems(type.Item1, false).ToArray();
                    foreach (int index in UtilityCore.RandomRange(0, items.Length))
                    {
                        ((IPartUpdatable)items[index]).Update_AnyThread(elapsedTime + (elapsedTime * type.Item2));        // if skips is greater than zero, then approximate how much time elapsed based on this tick's elapsed time
                    }
                }
            }

            #endregion

            _disposeWait.Set();      // Dispose will hang the main thread to make sure this tick has finished

            _timerAnyThread.Start(); // it doesn't matter if this class is disposed, the bool is checked at the beginning of this method (no need to take the expense of checking again)
        }
예제 #13
0
        public virtual void Update_AnyThread(double elapsedTime)
        {
            //NOTE: This method doesn't have a lock, so there could be a bit of reentry.  But the individual pieces should be ok with that

            double age   = this.Age;
            long   count = Interlocked.Increment(ref _partUpdateCount_AnyThread);

            #region Fill the converters

            if (_parts.Containers.ConvertMatterGroup != null && age >= (double)_nextMatterTransferTime)
            {
                if (_parts.Containers.ConvertMatterGroup.Transfer())
                {
                    this.ShouldRecalcMass_Large = true;
                }

                //NOTE: Throwing a min in there, because if I have breakpoints set elsewhere, and resume after awhile, elapsed time will be
                //huge, and the calculated next transer time will be obscene.  In those cases, the odds are good that the bot will be fairly young
                //so a simple 110% of age should be enough of a safeguard
                //_nextMatterTransferTime = Math.Min(age * 1.1, age + (elapsedTime * StaticRandom.Next(80, 120)));      // no need to spin the processor unnecessarily each tick
                _nextMatterTransferTime = Math.Min(age * 1.1, age + (elapsedTime * StaticRandom.Next(20, 60)));      // no need to spin the processor unnecessarily each tick
            }

            #endregion

            #region Update Parts

            foreach (int index in UtilityCore.RandomRange(0, _updatableParts_AnyThread.Length))
            {
                int skips = _updatableParts_AnyThread[index].IntervalSkips_AnyThread.Value;        // no need to check for null, nulls weren't added to the list
                if (skips > 0 && count % (skips + 1) != 0)
                {
                    continue;
                }

                _updatableParts_AnyThread[index].Update_AnyThread(elapsedTime + (elapsedTime * skips));       // if skips is greater than zero, then approximate how much time elapsed based on this tick's elapsed time
            }

            if (_lifeEvents != null)
            {
                _lifeEvents.Update_AnyThread(elapsedTime);
            }

            // Detect small change
            if (_hasMassChangingUpdatables_Small)
            {
                // There's a good chance that at least one of the parts changed the ship's mass a little bit
                this.ShouldRecalcMass_Small = true;
            }

            // Detect medium change
            if (_hasMassChangingUpdatables_Medium && !this.ShouldRecalcMass_Medium)
            {
                double?ammoVolumeAtRecalc = (double?)_ammoVolumeAtRecalc;
                double ammoMassCur        = _parts.Containers.AmmoGroup.QuantityCurrent;

                if (ammoVolumeAtRecalc != null && !Math1D.IsNearValue(ammoVolumeAtRecalc.Value, ammoMassCur))
                {
                    this.ShouldRecalcMass_Medium = true;
                }
            }

            #endregion

            #region Recalc mass

            if (this.ShouldRecalcMass_Large)
            {
                RecalculateMass();
            }
            else if (this.ShouldRecalcMass_Medium && age > (double)_lastMassRecalculateTime + (elapsedTime * 200d))
            {
                RecalculateMass();
            }
            else if (this.ShouldRecalcMass_Small && age > (double)_lastMassRecalculateTime + (elapsedTime * 1000d))     //NOTE: This age check isn't atomic, so multiple threads could theoretically interfere, but I don't think it's worth worrying about (the RecalculateMass method itself is threadsafe)
            {
                RecalculateMass();
            }

            #endregion
        }
예제 #14
0
        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region ANNOYING XML

                //                string xml = "<?xml version=\"1.0\" encoding=\"utf - 8\" ?>" + @"
                //  <Experiments>

                //    " + "<Experiment name=\"Pick a Number\"" + @">
                //  <AssemblyPath>SharpNeatDomains.dll</AssemblyPath >
                //  <ClassName>SharpNeat.Domains.FunctionRegression.FunctionRegressionExperiment</ClassName>
                //  <Config>
                //    <PopulationSize>150</PopulationSize>
                //    <SpecieCount>10</SpecieCount>
                //    <Activation>
                //      <Scheme>Acyclic</ Scheme >
                //    </Activation>
                //    <ComplexityRegulationStrategy>Absolute</ ComplexityRegulationStrategy >
                //    <ComplexityThreshold>20</ComplexityThreshold>
                //    <Description>Pick a Number</Description>
                //    </Config>
                //  </Experiment>

                //</Experiments>
                //";

                //                //< Function > Sine </ Function >
                //                //< SampleResolution > 21 </ SampleResolution >
                //                //< SampleMin > -3.14159 </ SampleMin >
                //                //< SampleMax > 3.14159 </ SampleMax >


                //                XmlDocument doc = new XmlDocument();
                //                doc.LoadXml(xml);

                //                XmlElement element = doc.SelectSingleNode("Config") as XmlElement;

                //                _experiment.Initialize("Pick a Number", element);

                #endregion

                int[] trueValues = UtilityCore.RandomRange(0, 8, StaticRandom.Next(1, 8)).ToArray();

                ExperimentInitArgs config = new ExperimentInitArgs()
                {
                    Description    = "Some numbers are chosen, the NN needs to train itself to return true when one of those numbers are presented",
                    InputCount     = 3,
                    OutputCount    = 1,
                    PopulationSize = 150,
                    SpeciesCount   = 10,
                    Activation     = new ExperimentInitArgs_Activation_Acyclic(),
                    Complexity_RegulationStrategy = ComplexityCeilingType.Absolute,
                    Complexity_Threshold          = 20,
                };

                PAN_Evaluator evaluator = new PAN_Evaluator(config.InputCount, PAN_Experiment.GetTrueVectors(config.InputCount, trueValues));

                _experiment = new PAN_Experiment(config.InputCount, trueValues);
                _experiment.Initialize("pick a number", config, evaluator);

                //_experiment.NeatGenomeParameters.InitialInterconnectionsProportion = .1;

                _ea = _experiment.CreateEvolutionAlgorithm();

                // Attach update event listener.
                _ea.UpdateEvent += EA_UpdateEvent;
                _ea.PausedEvent += EA_PausedEvent;

                ShowBestGenome();
                panPlot.ResetLabels(trueValues);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
예제 #15
0
        private void FinishLoad(FlyingBeanSession session, FlyingBeanOptions options, ItemOptions itemOptions, ShipDNA[] winningBeans, string saveFolder, bool startEmpty)
        {
            // Manually instantiate some of the properties that didn't get serialized
            options.DefaultBeanList = _defaultBeanList;

            if (options.NewBeanList == null)            // if this was called by new, it will still be null
            {
                options.NewBeanList = new SortedList <string, ShipDNA>();

                if (!startEmpty)
                {
                    string[] beanKeys = options.DefaultBeanList.Keys.ToArray();

                    foreach (int keyIndex in UtilityCore.RandomRange(0, beanKeys.Length, Math.Min(3, beanKeys.Length)))         // only do 3 of the defaults
                    {
                        string key = beanKeys[keyIndex];
                        options.NewBeanList.Add(key, options.DefaultBeanList[key]);
                    }
                }
            }

            options.MutateArgs = PanelMutation.BuildMutateArgs(options);

            options.GravityField = new GravityFieldUniform();
            options.Gravity      = options.Gravity;     // the property set modifies the gravity field

            options.WinnersLive = new WinnerList(true, options.TrackingMaxLineagesLive, options.TrackingMaxPerLineageLive);

            if (options.WinnersFinal == null)           // if a previous save had winning ships, this will already be loaded with them
            {
                options.WinnersFinal = new WinnerList(false, options.TrackingMaxLineagesFinal, options.TrackingMaxPerLineageFinal);
            }

            options.WinnerCandidates = new CandidateWinners();
            // These are already in the final list, there's no point in putting them in the candidate list as well
            //if (winningBeans != null)
            //{
            //    foreach (ShipDNA dna in winningBeans)
            //    {
            //        options.WinnerCandidates.Add(dna);
            //    }
            //}

            // Make sure the session folder is up to date
            if (saveFolder == null)
            {
                this.SessionFolder = null;
            }
            else
            {
                string dirChar = Regex.Escape(System.IO.Path.DirectorySeparatorChar.ToString());

                string pattern = dirChar + Regex.Escape(FlyingBeansWindow.SESSIONFOLDERPREFIX) + "[^" + dirChar + "]+(?<upto>" + dirChar + ")" + Regex.Escape(FlyingBeansWindow.SAVEFOLDERPREFIX);
                Match  match   = Regex.Match(saveFolder, pattern, RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    this.SessionFolder = saveFolder.Substring(0, match.Groups["upto"].Index);           // the session folder is everything before the save subfolder
                }
                else
                {
                    // They may have chosen a folder that they unziped onto their desktop, or wherever.  Leaving this null so that if they hit save, it will be saved
                    // in the appropriate location
                    this.SessionFolder = null;
                }
            }

            // Swap out the settings
            this.Session     = session;
            this.Options     = options;
            this.ItemOptions = itemOptions;

            // Inform the world
            if (this.SessionChanged != null)
            {
                this.SessionChanged(this, new EventArgs());
            }
        }
예제 #16
0
        private void Update_AnyThread_private(double elapsedTime)
        {
            if (_isDisposed)
            {
                return;
            }

            Type[] typesUnchecked = _typesAnyUnchecked;
            if (typesUnchecked != null)
            {
                #region Get intervals for types

                Tuple <Type, int>[] checkedTypes = GetTypeIntervals(typesUnchecked, _map, false);
                if (checkedTypes != null)
                {
                    lock (_lockTypesAny)
                    {
                        // Do it again with the volatile to be safe
                        checkedTypes = GetTypeIntervals(_typesAnyUnchecked, _map, false);
                        if (checkedTypes != null)
                        {
                            var transfer = TransferTypes(_typesAnyUnchecked, _typesAny, checkedTypes);
                            _typesAnyUnchecked = transfer.Item1;
                            _typesAny          = transfer.Item2;
                        }
                    }
                }

                #endregion
            }

            Tuple <Type, int>[] typesAny = _typesAny;
            long count = Interlocked.Increment(ref _updateCount_Any);

            #region non map items

            var nonmapItems = _nonmapItemsAny;

            if (nonmapItems != null)
            {
                foreach (var item in nonmapItems)
                {
                    if (count % (item.Item1 + 1) != 0)
                    {
                        continue;
                    }

                    item.Item2.Update_AnyThread(elapsedTime + (elapsedTime * item.Item1));        // if skips is greater than zero, then approximate how much time elapsed based on this tick's elapsed time
                }
            }

            #endregion
            #region items in map

            if (typesAny != null)
            {
                foreach (var type in typesAny)
                {
                    if (type.Item2 > 0 && count % (type.Item2 + 1) != 0)
                    {
                        continue;
                    }

                    // Update all the live instances in a random order
                    IMapObject[] items = _map.GetItems(type.Item1, false).ToArray();
                    foreach (int index in UtilityCore.RandomRange(0, items.Length))
                    {
                        ((IPartUpdatable)items[index]).Update_AnyThread(elapsedTime + (elapsedTime * type.Item2));        // if skips is greater than zero, then approximate how much time elapsed based on this tick's elapsed time
                    }
                }
            }

            #endregion

            _disposeWait.Set();     // Dispose will hang the main thread to make sure this tick has finished
        }
예제 #17
0
        private void ResetTraining()
        {
            _patternStorage = null;
            panelTrainingImages.Children.Clear();

            if (_images.Count == 0)
            {
                return;
            }

            #region choose images

            int numImages;
            if (!int.TryParse(txtTrainCount.Text, out numImages))
            {
                MessageBox.Show("Couldn't parse count as an integer", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            numImages = Math.Min(_images.Count, numImages);

            var trainImages = UtilityCore.RandomRange(0, _images.Count, numImages).
                              Select(o => new
            {
                File = _images[o],
                Conv = GetTrainingImage(_images[o], _trainKernel),
            }).
                              ToArray();

            #endregion

            //_patternStorage = new RandomPatternStorage();
            _patternStorage = new Hopfield(IMAGESIZE * IMAGESIZE, 0, 1, .9);

            #region show thumbnails

            // Display thumbnails
            //< !--Run them through a KMeans, then sort in 1D-- >
            //< !--Show full resolution over the canvas on mouseover-- >
            //< !--Show full resolution under the canvas on click-- >

            foreach (var trainImage in trainImages)
            {
                double widthHeight    = Math.Sqrt(trainImage.Conv.Length);  // they should be square
                int    widthHeightInt = widthHeight.ToInt_Round();
                if (!Math1D.IsNearValue(widthHeight, widthHeightInt))
                {
                    throw new ApplicationException("Expected square images");
                }

                double[] imageConv = trainImage.Conv;
                imageConv = _patternStorage.Convert_Local_External(imageConv);

                BitmapSource source;
                //if (isColor)
                //{
                //    source = UtilityWPF.GetBitmap_RGB(example, width, height);
                //}
                //else
                //{
                source = UtilityWPF.GetBitmap(imageConv, widthHeightInt, widthHeightInt);
                //}

                Image image = new Image()
                {
                    Source = source,
                    Width  = source.PixelWidth,     // if this isn't set, the image will take up all of the width, and be huge
                    Height = source.PixelHeight,
                    Margin = new Thickness(8),
                };

                panelTrainingImages.Children.Add(image);
            }

            #endregion

            _patternStorage.AddItems(trainImages.Select(o => o.Conv).ToArray());
        }