/// <summary>
        ///     Opens a train set.
        /// </summary>
        public async void OpenTrainSetAsync()
        {
            bool isCurrentTrainSetClosable = false;

            await CloseTrainSetAsync(canClose => isCurrentTrainSetClosable = canClose);

            if (!isCurrentTrainSetClosable)
            {
                return;
            }

            using (Stream stream = this._fileService.Open())
            {
                if (stream == null)
                {
                    return;
                }

                try
                {
                    using (this._busyWatcher.GetTicket())
                    {
                        TrainSet trainSet = await TrainSetSerializer.LoadFromAsync(stream);

                        this._trainSetViewModel.TrainSet = trainSet;
                        CurrentFileName = this._fileService.LastFile;
                        IsModelDirty    = false;
                    }
                }
                catch (Exception exception)
                {
                    ShowErrorDialog(exception, Resources.UnableToOpen);
                }
            }
        }
Example #2
0
        public AnfisService(List <DataItem> dataItems, double trainChunk, double minValue, double maxValue, int foldNum) : this()
        {
            if (dataItems.Count == 0)
            {
                throw new Exception("Data items must not be empty");
            }
            if (trainChunk <= 0 || trainChunk >= 1)
            {
                throw  new Exception("Train chunk value must be between 0 and 1");
            }
            if (foldNum <= 1)
            {
                throw new Exception("Number of folds must be greater than 1");
            }

            TrainSet.AddRange(dataItems.GetRange(0, (int)(trainChunk * dataItems.Count)));
            TestSet.AddRange(dataItems.GetRange(
                                 (int)(trainChunk * dataItems.Count), dataItems.Count - (int)(trainChunk * dataItems.Count)));

            TriangularInfoFolds = new TriangularInfo[foldNum];
            var step = (maxValue - minValue) / (foldNum - 1);

            for (int i = 0; i < foldNum; i++)
            {
                TriangularInfoFolds[i] = new TriangularInfo()
                {
                    A = minValue - step + i * step,
                    M = minValue + i * step
                };
            }
        }
Example #3
0
 public void Train(TrainSet trainSet)
 {
     TrainNetworkS(trainSet, new BackPropagationLearning(m_network1));
     TrainNetworkS(trainSet, new ResilientBackpropagationLearning(m_network2));
     //TrainNetworkE(trainSet, new EvolutionaryLearning(m_network3, 1000));
     TrainNetworkS(trainSet, new DeltaRuleLearning(m_network4));
 }
Example #4
0
        /// <summary>
        /// Trains on a single set.
        /// </summary>
        public double Study(TrainSet currentSet, double learningRate = 1, double moment = 1)
        {
            double actual = this.Handle(currentSet.Input)[0];
            double error  = currentSet.Output - actual; // ideal - actual

            foreach (BackpropNeuron outn in OutputLayer)
            {
                outn.Delta = error * OutputLayer.Activation.Activate(outn.Value, derivative: true);
            }

            // train each hidden layer
            Layer backpropLayer = OutputLayer;

            while ((backpropLayer = backpropLayer.PreviousLayer) != null)
            {
                backpropLayer.Train(learningRate, moment);
            }

            // Change OutputLayer weights.
            foreach (BackpropNeuron outNeuron in OutputLayer)
            {
                for (int s = 0; s < outNeuron.IncomingWeights.Length; s++)
                {
                    double prevNeuronValue = OutputLayer.PreviousLayer[s].Value;
                    outNeuron.IncomingWeights[s] += prevNeuronValue * outNeuron.Delta * learningRate;
                }
            }

            return(error);
        }
Example #5
0
        private void TrainNetworkS(TrainSet trainSet, ISupervisedLearning teacher)
        {
            double[] sides     = new double[4];
            double[] direction = new double[2];
            double[] input     = new double[5];
            double[] output    = new double[4];

            double error = 10;
            int    epoch = 10000;

            while (epoch-- > 0)
            {
                for (int s = 0; s < trainSet.Situation.Count; s++)
                {
                    sides     = SimplifyEnvironment(trainSet.Situation[s].Environment);
                    direction = VectorFromDirection(trainSet.Decision[s].Direction);

                    // INPUT
                    input[0] = trainSet.Situation[s].ColonyPopulation;
                    input[1] = sides[0]; // UP
                    input[2] = sides[1]; // RIGHT
                    input[3] = sides[2]; // DOWN
                    input[4] = sides[3]; // LEFT

                    // OUTPUT
                    output[0] = trainSet.Decision[s].IsLeaving ? 1 : -1;
                    output[1] = trainSet.Decision[s].PopulationToMove;
                    output[2] = direction[0]; // X
                    output[3] = direction[1]; // Y

                    error = teacher.Run(input, output);
                }
                Debug.Print(error.ToString());
            }
        }
        public async Task <ApiResponse <IdNameModel> > Create(TrainSetCreateModel model)
        {
            var user = await CurrentUser();

            if (user == null)
            {
                return(Failed("No user"));
            }

            var entity = new TrainSet
            {
                Name            = model.Name,
                SourceId        = model.SourceId,
                UserId          = user.Id,
                Type            = model.Type,
                MaxCount        = model.MaxCount,
                MinCount        = model.MinCount,
                InputWordsCount = 3,
            };

            entity = await _trainSets.Add(entity);

            if (entity == null)
            {
                return(Failed("Server error"));
            }

            return(Ok(new IdNameModel
            {
                Id = entity.Id,
                Name = entity.Name
            }));
        }
Example #7
0
 public ConsistFile(string fileName)
 {
     using (STFReader stf = new STFReader(fileName, false))
         stf.ParseFile(new STFReader.TokenProcessor[] {
             new STFReader.TokenProcessor("train", () => { Train = new TrainSet(stf); }),
         });
     Name = Train.Name;
 }
Example #8
0
            public override bool Test(Sim a, TrainSet target, bool isAutonomous, ref GreyedOutTooltipCallback greyedOutTooltipCallback)
            {
                if (!OccultTypeHelper.HasType(a, OccultTypes.Fairy))
                {
                    return(false);
                }

                return(base.Test(a, target, isAutonomous, ref greyedOutTooltipCallback));
            }
        /// <summary>
        ///     Moves the selected waggon up.
        /// </summary>
        public void MoveWaggonUp()
        {
            object movedWaggon = this._waggonsView.CurrentItem;
            int    index       = this._waggonsView.CurrentPosition;

            Contract.Assume(index > 0 && index < this._waggonsView.Count);

            TrainSet.MoveWaggonUpAt(index);

            this._waggonsView.Refresh();
            this._waggonsView.MoveCurrentTo(movedWaggon);
        }
        public TrainSetViewModel([ImportMany] IEnumerable <ITrainSetScreen> trainSetScreens)
        {
            this._trainSet = new TrainSet();

            Items.AddRange(trainSetScreens.OrderBy(screen => screen.Order));
            ActivateItem(Items.FirstOrDefault());

            foreach (var reportingScreen in Items.OfType <IReportModelChanges>())
            {
                reportingScreen.ModelChanged += OnReportingScreenModelChanged;
            }
        }
Example #11
0
        /// <summary>
        ///     Moves the selected train down.
        /// </summary>
        public void MoveTrainDown()
        {
            object movedTrain = this._trainsView.CurrentItem;
            int    index      = this._trainsView.CurrentPosition;

            Contract.Assume(index >= 0 && index < this._trainsView.Count - 1);

            TrainSet.MoveTrainDownAt(index);

            this._trainsView.Refresh();
            this._trainsView.MoveCurrentTo(movedTrain);
        }
Example #12
0
        private void editDeletebutton_Click(object sender, EventArgs e)
        {
            if (playerListBox.SelectedItem == null)
            {
                MessageBox.Show("No item selected.", "Warning",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                DialogResult = DialogResult.None;
                return;
            }

            var player = m_players?.
                         Where(p => p.ToString() == playerListBox.SelectedItem.ToString()).FirstOrDefault();
            var nation = m_nations?.
                         Where(n => n.ToString() == playerListBox.SelectedItem.ToString()).FirstOrDefault();
            var trainSet = m_trainSets?.
                           Where(t => t.ToString() == playerListBox.SelectedItem.ToString()).FirstOrDefault();

            if (m_trainSets != null)
            {
                var setNames = playerListBox.SelectedItems;
                var jointSet = new TrainSet();
                foreach (var name in setNames)
                {
                    var toAdd = m_trainSets?.
                                Where(t => t.ToString() == name.ToString()).FirstOrDefault();
                    if (toAdd != null)
                    {
                        jointSet.Merge(toAdd);
                    }
                }
                trainSet = jointSet;
            }

            if (m_action == GameAction.EditPlayer)
            {
                EditPlayer(player);
            }
            else if (m_action == GameAction.DeletePlayer)
            {
                DeletePlayer(player);
            }
            else if (m_action == GameAction.TrainNationSelectNation)
            {
                SelectTrainSet(nation);
            }
            else
            {
                TrainAndSaveNation(trainSet);
            }
        }
Example #13
0
 internal ActivityObject(STFReader stf)
 {
     stf.MustMatchBlockStart();
     stf.ParseBlock(new STFReader.TokenProcessor[] {
         new STFReader.TokenProcessor("objecttype", () => { stf.MustMatchBlockStart(); stf.MustMatch("WagonsList"); stf.MustMatchBlockEnd(); }),
         new STFReader.TokenProcessor("train_config", () => { TrainSet = new TrainSet(stf); }),
         new STFReader.TokenProcessor("direction", () => { Direction = stf.ReadIntBlock(null); }),
         new STFReader.TokenProcessor("id", () => { ID = stf.ReadIntBlock(null); }),
         new STFReader.TokenProcessor("tile", () => {
             stf.MustMatchBlockStart();
             location = new WorldLocation(stf.ReadInt(null), stf.ReadInt(null),
                                          stf.ReadFloat(STFReader.Units.None, null), 0f, stf.ReadFloat(STFReader.Units.None, null));
             stf.MustMatchBlockEnd();
         }),
     });
 }
        protected async Task ScheduleProcess(TrainSet data)
        {
            switch (data.Type)
            {
            case TrainSetSourceType.Twitter:
                data = await ScheduleProcessTwitter(data);

                break;

            default:
                data = await ScheduleProcessNotImplemented(data);

                break;
            }

            await _trainSets.Update(data);
        }
Example #15
0
 public TrainSet extractBatch(int size)
 {
     if (size > 0 && size <= this.size())
     {
         TrainSet set = new TrainSet(INPUT_SIZE, OUTPUT_SIZE);
         int[]    ids = NetworkTools.randomValues(0, this.size() - 1, size);
         foreach (int i in ids)
         {
             set.addData(this.getInput(i), this.getOutput(i));
         }
         return(set);
     }
     else
     {
         return(this);
     }
 }
        private void BtnEdit_OnClick(object sender, RoutedEventArgs e)
        {
            int oldTrainId = (int)this.CmbTrainId.SelectedItem;
            int seats;
            if (String.IsNullOrEmpty(this.TxtDescription.Text) || String.IsNullOrEmpty(this.TxtSeats.Text) || !int.TryParse(this.TxtSeats.Text,out seats))
            {
                MessageBox.Show("Invalid input data!");
                return;
            }

            TrainSet updatedTrain = new TrainSet() {Seats = int.Parse(this.TxtSeats.Text),Description = this.TxtDescription.Text, IsFree = (bool)this.ChkIsFree.IsChecked};
            if (DataAccess.DataAccess.EditTrain(oldTrainId, updatedTrain))
            {
                parent.RefreshTrainGrind();
                MessageBox.Show("Successfully updated the train!");
            }
            else
            {
                MessageBox.Show("Something went wrong while updating the train");
            }
        }
Example #17
0
        public void Train(List <TrainSet> trainSetList, int maxEpoch
                          , double learningRate = 1
                          , double moment       = 1)
        {
            double[] idealOutputs = trainSetList.Select(t => t.Output).ToArray();

            for (int ep = 0; ep < maxEpoch; ep++)
            {
                for (int ts = 0; ts < trainSetList.Count; ts++)
                {
                    TrainSet currentSet = trainSetList[ts];

                    double error = Study(currentSet, learningRate, moment);

                    if (ts == 0 && ep % 1000 == 0)
                    {
                        Console.WriteLine($"Ep #{ep} | {Math.Abs(error)}");
                    }
                }
            }
        }
 private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
 {
     int seats;
     if (!int.TryParse(this.TxtSeats.Text,out seats) || String.IsNullOrEmpty(this.TxtDescription.Text))
     {
         MessageBox.Show("Invalid input!");
     }
     else
     {
         TrainSet train = new TrainSet() { Seats = seats, Description = this.TxtDescription.Text, IsFree = true};
         if (DataAccess.DataAccess.AddTrain(train))
         {
             this.parent.RefreshTrainGrind();
             MessageBox.Show("Successfully inserted a new train");
             this.Close();
         }
         else
         {
             MessageBox.Show("Something went wrong while creating the train!");
         }
     }
 }
        /// <summary>
        ///     Saves a trainSet asynchronously to a stream.
        /// </summary>
        /// <param name="trainSet">The trainSet to save.</param>
        /// <param name="stream">The stream to save to.</param>
        /// <exception cref="ArgumentNullException">When <paramref name="trainSet" /> or <paramref name="stream" />is null.</exception>
        public static async Task SaveToAsync(TrainSet trainSet, Stream stream)
        {
            Contract.Requires<ArgumentNullException>(trainSet != null);
            Contract.Requires<ArgumentNullException>(stream != null);

            var serializer = new DataContractSerializer(typeof(TrainSet));
            var xmlWriterSettings = new XmlWriterSettings
                                    {
                                        CheckCharacters = true,
                                        Encoding = new UTF8Encoding(),
                                        Indent = true
                                    };

            await Task.Factory.StartNew(
                () =>
                {
                    using (
                        XmlDictionaryWriter writer =
                            XmlDictionaryWriter.CreateDictionaryWriter(XmlWriter.Create(stream, xmlWriterSettings)))
                    {
                        serializer.WriteObject(writer, trainSet);
                    }
                });
        }
Example #20
0
        /// <summary>
        ///     Saves a trainSet asynchronously to a stream.
        /// </summary>
        /// <param name="trainSet">The trainSet to save.</param>
        /// <param name="stream">The stream to save to.</param>
        /// <exception cref="ArgumentNullException">When <paramref name="trainSet" /> or <paramref name="stream" />is null.</exception>
        public static async Task SaveToAsync(TrainSet trainSet, Stream stream)
        {
            Contract.Requires <ArgumentNullException>(trainSet != null);
            Contract.Requires <ArgumentNullException>(stream != null);

            var serializer        = new DataContractSerializer(typeof(TrainSet));
            var xmlWriterSettings = new XmlWriterSettings
            {
                CheckCharacters = true,
                Encoding        = new UTF8Encoding(),
                Indent          = true
            };

            await Task.Factory.StartNew(
                () =>
            {
                using (
                    XmlDictionaryWriter writer =
                        XmlDictionaryWriter.CreateDictionaryWriter(XmlWriter.Create(stream, xmlWriterSettings)))
                {
                    serializer.WriteObject(writer, trainSet);
                }
            });
        }
        protected async Task <TrainSet> ScheduleProcessTwitter(TrainSet data)
        {
            var collection = await _twitterCollectionsStore.Get(data.SourceId);

            if (collection == null)
            {
                data.SetFailed("Can not find source data");
                return(data);
            }

            var sources = await _twitterSourcesStore.GetBy(x => x.CollectionId == collection.Id);

            if (sources == null || !sources.Any())
            {
                data.SetFailed("Can not find any twitter sources");
                return(data);
            }

            var user = await _userStore.FindByIdAsync(collection.UserId);

            if (user == null)
            {
                data.SetFailed("Can not find user data");
                return(data);
            }

            var userTwitter = await _userSocialsStore.GetTwitter(user.Id);

            if (userTwitter == null)
            {
                data.SetFailed("No twitter access token");
                return(data);
            }

            try
            {
                OAuthTwitter(userTwitter);
            }
            catch
            {
                data.SetFailed("Error with twitter connections");
                return(data);
            }

            // upload twitter data

            int min = data.MinCount;

            min = Math.Max(1, min);

            int max = data.MaxCount;

            max = Math.Max(100, max);
            max = Math.Min(10000, max);

            if (min > max)
            {
                var t = min;
                max = min;
                min = t;
            }

            int perSource = (int)Math.Ceiling((double)max / sources.Count);
            var entity    = new TrainSetModel {
            };

            var rawData = new StringBuilder();
            int total   = 0;

            var regex = new Regex("http[s]?://[A-Za-z0-9._-]*");

            foreach (var screen in sources)
            {
                long?lastId      = null;
                int  count       = 0;
                var  twetterUser = await UserAsync.GetUserFromId(screen.TwitterId);

                while (perSource > count)
                {
                    var @params = new UserTimelineParameters
                    {
                        MaximumNumberOfTweetsToRetrieve = 50,
                    };

                    if (lastId.HasValue)
                    {
                        @params.SinceId = lastId.Value;
                    }

                    var tweets = await TimelineAsync.GetUserTimeline(twetterUser, @params);

                    if (tweets == null || !tweets.Any())
                    {
                        break;
                    }

                    count += tweets.Count();
                    foreach (var t in tweets)
                    {
                        rawData.Append(regex.Replace(t.FullText, string.Empty));
                    }
                }

                total += count;
            }

            if (total < min)
            {
                data.SetFailed($"Not enough data avaliable. Avaliable : {total}. Minimum: {min}");
                return(data);
            }

            WordBag wb = WordBag.CreateToWords(rawData.ToString(), data.InputWordsCount + 1);

            _vReader.UploadBinary();

            List <Tuple <string[], string[]> > stringList = new List <Tuple <string[], string[]> >();
            List <Tuple <double[], double[]> > doubleList = new List <Tuple <double[], double[]> >();

            foreach (var s in wb.Read())
            {
                var vectorList = new List <double[]>();
                var wordList   = new List <string>();

                foreach (var ss in s)
                {
                    var word = _vReader.Vocab.GetRepresentationOrNullFor(ss);
                    if (word == null)
                    {
                        break;
                    }

                    vectorList.Add(ss.Select(x => (double)x).ToArray());
                    wordList.Add(ss);
                }

                if (vectorList.Count < s.Length)
                {
                    continue;
                }

                var tmpVector = new List <double>();
                foreach (var i in vectorList.Take(data.InputWordsCount))
                {
                    tmpVector.AddRange(i);
                }

                doubleList.Add(new Tuple <double[], double[]>(tmpVector.ToArray(), vectorList.Last().ToArray()));
                stringList.Add(new Tuple <string[], string[]>(wordList.Take(wordList.Count - 1).ToArray(), new string[1] {
                    wordList.Last()
                }));
            }

            entity.Data         = doubleList.ToArray();
            entity.StringSource = stringList.ToArray();

            string dataString = JsonConvert.SerializeObject(entity);

            await _storageBlobClient.SetContainer(CONTAINER_NAME, true);

            var storageKey = await _storageBlobClient.WriteText(dataString);

            if (string.IsNullOrWhiteSpace(storageKey))
            {
                data.SetFailed("Can not upload train set to storage");
                return(data);
            }

            data.StorageKey = storageKey;
            data.SetReady();

            data.ExamplesCount = entity.Data.Count();

            return(data);
        }
Example #22
0
 public void Train(TrainSet trainSet)
 {
     // Random will stay forever random
 }
Example #23
0
 private void TrainAndSaveNation(TrainSet trainSet)
 {
     m_nation.Government.Train(trainSet);
     DataParser.Serialize(m_nations);
 }
Example #24
0
        static void Test()
        {
            Random ran = new Random();

            double[] input = { 0.0, 0.0 };
            double[] output;
            double[] aux_i = new double[2];
            double[] aux_o = new double[1];
            int      N = 1000;
            int      i1, i2;

            TrainPair[] tps = new TrainPair[N];

            for (int i = 0; i < N; i++)
            {
                i1 = 0;
                i2 = 0;
                if (ran.NextDouble() > 0.5)
                {
                    i1 = 1;
                }
                if (ran.NextDouble() > 0.5)
                {
                    i2 = 1;
                }

                aux_i[0] = i1;
                aux_i[1] = i2;
                aux_o[0] = (double)(i1 ^ i2);

                //Console.WriteLine("{0} ^ {1} = {2}", aux_i[0], aux_i[1], aux_o[0]);

                tps[i] = new TrainPair(aux_i, aux_o);
            }


            TrainSet ts = new TrainSet(tps);

            Red r = new Red(2, 2, 1);

            //printAr(output);
            r.train(ts, 1);
            output = r.compute(input);
            Console.WriteLine("0 ^ 0 = " + Math.Round(output[0]) + " ({0})", output[0]);

            input[0] = 1.0;
            input[1] = 0.0;
            output   = r.compute(input);
            Console.WriteLine("0 ^ 1 = " + Math.Round(output[0]) + " ({0})", output[0]);

            input[0] = 0.0;
            input[1] = 1.0;
            output   = r.compute(input);
            Console.WriteLine("1 ^ 0 = " + Math.Round(output[0]) + " ({0})", output[0]);

            input[0] = 1.0;
            input[1] = 1.0;
            output   = r.compute(input);
            Console.WriteLine("1 ^ 1 = " + Math.Round(output[0]) + " ({0})", output[0]);

            Console.WriteLine("*******************");
        }
Example #25
0
 protected double Train(TrainSet trainSet, double learningRate = 1, double moment = 1) => throw new NotImplementedException();
Example #26
0
 public void Train(TrainSet trainSet)
 {
     // AgentBot no need to train
 }
Example #27
0
        public void Train(TrainSet trainSet)
        {
            var dim       = trainSet.Situation.First().Environment.Size * 2 + 1;
            var inputSize = dim * dim * 2 + 3;

            if (m_network == null)
            {
                m_inputSize = inputSize;
                var layerSizes = new int[HiddenLayerSizes.Length + 2];
                layerSizes[0] = m_inputSize;
                layerSizes[layerSizes.Length - 1] = OutputSize;
                Array.Copy(HiddenLayerSizes, 0, layerSizes, 1, HiddenLayerSizes.Length);
                m_network = new NeuralNetwork(layerSizes, LearningRate);
            }
            if (inputSize != m_inputSize)
            {
                throw new Exception("Training set input variables doesn't match neural networks input structure.");
            }

            /*
             * var directionCount = (
             *  from object direction in Enum.GetValues(typeof(Direction))
             *  select trainSet.Decision.Count(d => d.Direction == (Direction)direction)).ToList();
             * var minCount = directionCount.Min();
             * directionCount = directionCount.Select(cnt => cnt - minCount).ToList();
             * while (directionCount.Sum() != 0)
             * {
             *  var ran = new Random();
             *  for (var i = 0; i < trainSet.Situation.Count; i++)
             *  {
             *      if (trainSet.Decision[i] != null &&
             *          directionCount[(int)trainSet.Decision[i].Direction] > 0
             *          && ran.NextDouble() > 0.5)
             *      {
             *          directionCount[(int)trainSet.Decision[i].Direction]--;
             *          trainSet.Decision[i] = null;
             *          trainSet.Situation[i] = null;
             *      }
             *  }
             * }
             */
            var setSize = trainSet.Decision.Count;

            while (trainSet.Decision.Count(d => d?.Direction == Direction.None) > setSize / 6)
            {
                var ran = new Random();
                for (var i = 0; i < trainSet.Decision.Count; i++)
                {
                    if (trainSet.Decision[i]?.Direction == Direction.None && ran.NextDouble() > 0.6)
                    {
                        trainSet.Decision[i]  = null;
                        trainSet.Situation[i] = null;
                        setSize--;
                    }
                }
            }
            trainSet.Decision  = trainSet.Decision.Where(d => d != null).ToList();
            trainSet.Situation = trainSet.Situation.Where(s => s != null).ToList();
            var inputs  = new double[trainSet.Situation.Count][];
            var outputs = new double[trainSet.Situation.Count][];

            for (var i = 0; i < trainSet.Decision.Count; i++)
            {
                inputs[i]  = SituationToInputs(trainSet.Situation[i]);
                outputs[i] = DecisionToOutputs(trainSet.Decision[i]);
            }
            m_network.Train(inputs, outputs, TestSetPercentage, MaxEpochs, GoalError);
        }
Example #28
0
 public override string GetInteractionName(Sim actor, TrainSet target, InteractionObjectPair iop)
 {
     return(base.GetInteractionName(actor, target, new InteractionObjectPair(sOldSingleton, target)));
 }
 protected async Task <TrainSet> ScheduleProcessNotImplemented(TrainSet data)
 {
     data.SetFailed("Not implimented yet");
     return(data);
 }
Example #30
0
 public void Train(TrainSet trainSet)
 {
     throw new NotImplementedException();
 }