Esempio n. 1
0
        public JobData(SJob data)
        {
            UID           = data.uid;
            status        = data.status;
            packageWeight = data.packageWeight;
            packageXArea  = data.packageXarea;

            costFunction = new CostFunction(data.costFunction);

            if (data.status != JobStatus.Assigning)
            {
                created    = new TimeKeeper.Chronos(data.createdUnity).SetReadOnly();
                assignment = new TimeKeeper.Chronos(data.assignedTime).SetReadOnly();
                deadline   = new TimeKeeper.Chronos(data.deadline).SetReadOnly();
                if (data.status != JobStatus.Delivering)
                {
                    completed = new TimeKeeper.Chronos(data.completedOn).SetReadOnly();
                    pickup    = data.pickup;
                    dropoff   = data.destination;
                }
            }
            else
            {
                created  = TimeKeeper.Chronos.Get();
                deadline = TimeKeeper.Chronos.Get() + costFunction.CompleteIn;
                earnings = costFunction.GetPaid(deadline - 1f, deadline); // aproximate earnings
                Vector3 o = data.pickup;
                o.y = 0;
                Vector3 d = data.destination;
                d.y = 0;

                pickup  = LandingZoneIdentifier.Reposition(o);
                dropoff = LandingZoneIdentifier.Reposition(d);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Create a new minimum graph cut-based seam estimator.
 /// </summary>
 /// <param name="costFunc">The cost function</param>
 /// <param name="terminalCost">The terminal cost</param>
 /// <param name="badRegionPenalty">Bad Region penalty</param>
 public GraphCutSeamFinder(
     CostFunction costFunc  = CostFunction.Color,
     float terminalCost     = 1.0f,
     float badRegionPenalty = 1.0f)
 {
     _ptr = StitchingInvoke.cveGraphCutSeamFinderCreate(costFunc, terminalCost, badRegionPenalty, ref _seamFinderPtr);
 }
Esempio n. 3
0
 //! default constructor
 //public Problem(CostFunction costFunction, Constraint constraint, Vector initialValue = Array())
 public Problem(CostFunction costFunction, Constraint constraint, Vector initialValue)
 {
     costFunction_ = costFunction;
     constraint_   = constraint;
     currentValue_ = initialValue.Clone();
     Utils.QL_REQUIRE(!constraint.empty(), () => "empty constraint given");
 }
Esempio n. 4
0
 public void SetCostFuncion
 (
     CostFunction costFunction
 )
 {
     this.costFunction = costFunction;
 }
Esempio n. 5
0
 public ValueIteration(GridSpace g, double df, double lr, string[] a, CostFunction cf)
 {
     grid            = g;
     discount_factor = df;
     living_reward   = lr;
     actions         = a;
     CF = cf;
 }
Esempio n. 6
0
 public MM_Star
 (
     CostFunction costFunction = CostFunction.SOC
 )
 {
     this.costFunction  = costFunction;
     this.closedList    = new Dictionary <Move, Dictionary <int, Dictionary <int, MAM_AgentState> > >();
     this.expandedNodes = new HashSet <IBinaryHeapItem>();
 }
Esempio n. 7
0
        internal Info Training(NeuralNetwork nn, Data data, CostFunction costFunction = null)
        {
            if (costFunction == null)
            {
                costFunction = new CostFunction();
            }
            Info info = nn.FeedForwardInfo(data.input);

            return(nn.BackPropgate(info, costFunction, data.output));
        }
Esempio n. 8
0
        public Network()//MLP
        {
            int MiniBatchSize = 40;

            CostFunction = CostFunction.Quadratic;
            Add(new InputLayer(new double[MiniBatchSize, 784], MiniBatchSize));
            Add(new HiddenLayer(this[Count - 1].Z, 1000, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.SGD, 0.04, 0.000025, 0.1));
            Add(new HiddenLayer(this[Count - 1].Z, 1000, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.SGD, 0.04, 0.000025, 0.1));
            Add(new OutputLayer(this[Count - 1].Z, 10, MiniBatchSize, ActivationFunction.SoftPlus, WeightInitialization.Random, TrainAlgorithm.SGD, 0.04, 0.000025, 0.1));
            Labels = new double[MiniBatchSize, 10];
        }
    private IMove EvaluateMove()
    {
//        throw new NotImplementedException();
        var multiMove = new MultiMove();

        multiMove.AddMove(new Message($"Fight for {FactoryState.FactoryId}"));
        var moveTroops = new MoveTroops();

        multiMove.AddMove(moveTroops);
        for (int i = 0; i < StepsToPredict; i++)
        {
            //взять  мои фабрики на этом расстоянии
            var myFactoriesForAttack = graph.Factories[FactoryState.FactoryId].GetLinks()
                                       .Where(x => x.Distance <= i && graph.Factories[x.DestinationId].Side == Side.MyOwn)
                                       .Select(x => graph.Factories[x.DestinationId])
                                       .ToList();
            //если могут захватить - ура
            if (myFactoriesForAttack.Any() && myFactoriesForAttack.Sum(x => x.TroopsCanBeUsed) > FactoryState.Enemies[i])
            {
                int troopsUsed, needed;
                needed = troopsUsed = FactoryState.Enemies[i] ?? int.MaxValue;
                foreach (var myFactory in myFactoriesForAttack)
                {
//TODO: sort factories by distance or smth else
                    var sendedTroops = Math.Min(needed + 1, myFactory.TroopsCanBeUsed); // TODO: захват нейтралов совместный. +1 не совсем точно. нужно только для захвата но не деф.
                    myFactory.TroopsCanBeUsed =
                        graph.Factories[myFactory.Id].TroopsCanBeUsed = myFactory.TroopsCanBeUsed - sendedTroops;

                    var shortestPath = GraphLinks.Links[myFactory.Id, FactoryState.FactoryId];
                    moveTroops.AddTroop(new Troop {
                        Dst          = FactoryState.FactoryId,
                        DstInCommand = shortestPath.FirstFactoryId,
                        Side         = Side.MyOwn,
                        Remaining    = shortestPath.Distance + 1,
                        Src          = myFactory.Id,
                        Size         = sendedTroops
                    });

                    needed -= sendedTroops;
                    if (needed <= 0)
                    {
                        break;
                    }
                }

                priorityValue = CostFunction.GetJobEstimatePriority(
                    graph.Factories[FactoryState.FactoryId].Income * (graph.Factories[FactoryState.FactoryId].Side == Side.Neutral ? 1 : 2),
                    troopsUsed, i);

                break;
            }
        }
        return(multiMove);
    }
Esempio n. 10
0
    public int GetPriorityValue()
    {
        if (!moveAvailable)
        {
            return(int.MinValue);
        }
        var result = CostFunction.GetJobEstimatePriority(1, 10, 10, 15);

        Logger.ErrorLog("Cost: " + result + " ACT: " + move.GetConsoleCommand(), LogReportLevel.InnerJobCost);
        return(result);
    }
Esempio n. 11
0
 private string costFunctionToString(CostFunction costFunction)
 {
     if (costFunction == CostFunction.MakeSpan)
     {
         return("MakeSpan");
     }
     else if (costFunction == CostFunction.SOC)
     {
         return("SOC");
     }
     return("NULL");
 }
Esempio n. 12
0
        public static void Run(CostFunction f, int max_iteration = 500)
        {
            ConjugateGradientSearch s = new ConjugateGradientSearch();

            double[] x_0 = f.CreateRandomSolution();

            s.SolutionUpdated += (best_solution, step) =>
            {
                Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost);
            };

            s.Minimize(x_0, f, max_iteration);
        }
Esempio n. 13
0
        private void SetCostFunction(CostFunction costFunc)
        {
            switch (costFunc)
            {
            case CostFunction.MeanSquare:
                CostFunc = new MeanSquare();
                break;

            case CostFunction.CrossEntropy:
                CostFunc = new CrossEntropy();
                break;
            }
        }
Esempio n. 14
0
        public DifferentialEvolution(CostFunction f)
        {
            mSolutionGenerator = (dimension, constraints) =>
            {
                double[] x_0 = f.CreateRandomSolution();
                return(x_0);
            };

            mPopSize   = f.DimensionCount * 10;
            mDimension = f.DimensionCount;

            mUpperBounds = f.UpperBounds;
            mLowerBounds = f.LowerBounds;
        }
        public static void Run(CostFunction f, int max_iterations = 200)
        {
            SLOP s = new SLOP();


            double[] x_0 = f.CreateRandomSolution();

            s.SolutionUpdated += (best_solution, step) =>
            {
                Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost);
            };

            s.Minimize(x_0, f, max_iterations);
        }
Esempio n. 16
0
 internal Info BackPropgate(Info info, CostFunction costFunction, double[] target)
 {
     for (int i = info.errors.Length - 1; i > 0; i--)
     {
         info.errors[i] = i == info.errors.Length - 1 ?
                          layers[i - 1].BackPropogate(costFunction.Gradient(info.activations[i], target), info.thresholds[i]) :
                          layers[i - 1].BackPropogate(layers[i].weights, info.errors[i + 1], info.thresholds[i]);
     }
     foreach (double cost in costFunction.Cost(info.activations[info.activations.Length - 1], target))
     {
         info.cost += cost;
     }
     return(info);
 }
Esempio n. 17
0
        public ParticleSwarm(PSOParameters param, CostFunction fun)
        {
            if (fun == null)
            {
                throw new Exception("Cost Function cannot be null.");
            }
            m_Parameters   = param;
            m_CostFunction = fun;

            //initialize starting position and velocitiy
            m_MinLoc = m_Parameters.m_Min;        // -5.0; // for each weight
            m_MaxLoc = m_Parameters.m_Max;
            m_MinVel = -0.1 * m_Parameters.m_Max; // velocities
            m_MaxVel = 0.1 * m_Parameters.m_Max;
        }
Esempio n. 18
0
 public Network(string[] InputFileNames, double[,] Outputs)//ConvNet with 4 channel png-images
 {
     CostFunction = CostFunction.CrossEntropy;
     //Outputs[MiniBatchSize, NumberOfOutputNeurons]
     if (InputFileNames.Length == Outputs.GetLength(0))
     {
         int MiniBatchSize = InputFileNames.Length;
         Labels = Outputs;
         Add(new InputLayer(ImageLoader.ImageHandler.ImageToZ_3D(InputFileNames), MiniBatchSize));
         Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 5, 2, 10, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.SGD, 0.001 * MiniBatchSize, 0.01 / MiniBatchSize, 1));
         Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 3, 1, 10, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.SGD, 0.001 * MiniBatchSize, 0.01 / MiniBatchSize, 1));
         Add(new HiddenLayer(this[Count - 1].Z, 100, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.SGD, 0.001 * MiniBatchSize, 0.1 / MiniBatchSize, 1));
         Add(new OutputLayer(this[Count - 1].Z, Labels.GetLength(1), MiniBatchSize, ActivationFunction.SoftMax, WeightInitialization.Random, TrainAlgorithm.SGD, 0.001 * MiniBatchSize, 0.01 / MiniBatchSize, 1));
     }
 }
 public virtual ContinuousSolution Minimize(CostFunction f, int max_iterations)
 {
     return(Minimize((x, lower_bounds, upper_bounds, constraints) =>
     {
         return f.Evaluate(x);
     },
                     (x, gradX, lower_bounds, upper_bounds, constraints) =>
     {
         f.CalcGradient(x, gradX);
     },
                     (improvement, iterations) =>
     {
         return iterations > max_iterations;
     }));
 }
        public EvolutionStrategy(CostFunction f, int mu = 30, int lambda = 20)
        {
            mSolutionGenerator = (dimension, constraints) =>
            {
                double[] x_0 = f.CreateRandomSolution();
                return(x_0);
            };

            mPopSize_mu           = mu;
            mOffspringSize_lambda = lambda;
            mDimension            = f.DimensionCount;

            mUpperBounds = f.UpperBounds;
            mLowerBounds = f.LowerBounds;
        }
Esempio n. 21
0
        public EvolutionaryProgramming(CostFunction f, int popSize = 100, int boutSize = 5)
        {
            mSolutionGenerator = (dimension, constraints) =>
            {
                double[] x_0 = f.CreateRandomSolution();
                return(x_0);
            };

            mPopSize   = popSize;
            mDimension = f.DimensionCount;
            mBoutSize  = boutSize;

            mUpperBounds = f.UpperBounds;
            mLowerBounds = f.LowerBounds;
        }
Esempio n. 22
0
        public double Backward()
        {
            Data.Data["da" + (Layers.Count - 1).ToString()] = CostFunction.Backward(m_actual, m_expected, Data, Layers.Count);

            MatrixData data = Data;

            for (int i = Layers.Count - 1; i > 0; i--)
            {
                Layers[i].Backward(ref data);
            }
            Data = data;

            // CostFunction.ResetCost();

            return(CostFunction.BatchCost);
        }
Esempio n. 23
0
        public JobData(Hub pickup, Vector3 dropoff)
        {
            UID = ++_count;

            Hub           = pickup.UID;
            Status        = JobStatus.Assigning;
            Pickup        = pickup.Position;
            Dropoff       = LandingZoneIdentifier.Reposition(dropoff);
            PackageWeight = Random.Range(0.1f, 2.5f);

            Cost             = new CostFunction(WeightToRev(Pricing.US, PackageWeight));
            ExpectedDuration = (LateralManhattan() + LateralEuclidean()) / (2 * DroneMovementJob.HorizontalSpeed) + (Pickup.y - dropoff.y) / DroneMovementJob.VerticalSpeed;
            StDevDuration    = LateralManhattan() / DroneMovementJob.HorizontalSpeed - ExpectedDuration + (Pickup.y - Dropoff.y) / DroneMovementJob.VerticalSpeed;

            Deadline = Cost.Start + Cost.Guarantee;
        }
Esempio n. 24
0
        public static void Example1()
        {
            //Defining cost function
            CostFunction cost = new CostFunction();

            //Initializing WEO
            WeoParameters parameters = new WeoParameters()
            {
                CostFucntion = cost,
                Nwm          = 50,
                TMax         = 100,
                MepMin       = 0.03, //Best value from the paper
                MepMax       = 0.6,  //Best value from the paper
                ThetaMin     = 20,   //Best value from the paper which is equivalent to DepMin = 0.6
                ThetaMax     = 50,   //Best value from the paper which is equivalent to DepMax = 1
                Space        = new double[][]
                {
                    new double[] { -100, 100 },
                    new double[] { -100, 100 }
                }
            };
            WEO weo = new WEO(parameters);

            //Running WEO
            weo.Run();

            //Results
            double[] bestSol  = weo.BestSolution;
            double   bestCost = weo.BestCost;

            //Printing best solution
            StringWriter stringWr = new StringWriter();

            stringWr.Write(bestSol[0]);
            for (int i = 1; i < bestSol.Length; i++)
            {
                stringWr.Write(", {0}", bestSol[i]);
            }
            Console.WriteLine("Best solution: {0}", stringWr.ToString());

            //Printing best cost
            Console.WriteLine(bestCost);

            Console.ReadKey();
        }
Esempio n. 25
0
        /// <summary>
        ///   Runs the optimization routine for the set number of iterations.
        /// </summary>
        public void Run()
        {
            CostFunction.Initialize();

            for (var x = 0; x < Properties.MaxIterations; x++)
            {
                if (OpimizationMethod.Update(Properties))
                {
                    Step();
                }
                else
                {
                    break;
                }
            }

            Completed = true;
        }
Esempio n. 26
0
        public Network(string InputFileName, int _MiniBatchSize, string[] _ListOfFilenames, bool MNIST)//ConvNet with binarized, labelled image-dataset
        {
            L2_reg  = 0.01;
            IsMNIST = MNIST;
            if (!MNIST)
            {
                NoOfImages      = 10000;//"images per fileName-location" (data_batch_1,data_batch_2,data_batch_3,data_batch_4,data_batch_5,test_batch contain each 10000 images)
                MiniBatchSize   = _MiniBatchSize;
                ListOfFilenames = _ListOfFilenames;
                CostFunction    = CostFunction.CrossEntropy;
                Bl     = new ImageLoader.BinLoader(InputFileName, MiniBatchSize);
                Labels = Bl.GetLabels(0);
                Add(new InputLayer(Bl.GetZ_3D(0), MiniBatchSize));
                //long trained network
                //Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 5, 2, 10, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.Adam, 0.00001 * MiniBatchSize, 0.01 / MiniBatchSize, 1));
                //Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 5, 3, 16, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.Adam, 0.00001 * MiniBatchSize, 0.01 / MiniBatchSize, 1));
                //Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 3, 1, 16, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.Adam, 0.00001 * MiniBatchSize, 0.01 / MiniBatchSize, 1));
                //Add(new HiddenLayer(this[Count - 1].Z, 120, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.Adam, 0.00001 * MiniBatchSize, 0.1 / MiniBatchSize, 1));
                //Add(new HiddenLayer(this[Count - 1].Z, 84, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.Adam, 0.00001 * MiniBatchSize, 0.1 / MiniBatchSize, 1));
                //Add(new OutputLayer(this[Count - 1].Z, Labels.GetLength(1), MiniBatchSize, ActivationFunction.SoftMax, WeightInitialization.Random, TrainAlgorithm.Adam, 0.00001 * MiniBatchSize, 0.01 / MiniBatchSize, 1));

                Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 3, 1, 10, MiniBatchSize, ActivationFunction.lReLu, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.SGD, 0.00001 * MiniBatchSize, L2_reg, 1));
                Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 5, 2, 24, MiniBatchSize, ActivationFunction.lReLu, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.SGD, 0.00001 * MiniBatchSize, L2_reg, 1));
                Add(new HiddenLayer(this[Count - 1].Z, 120, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.SGD, 0.00001 * MiniBatchSize, L2_reg, 1));
                Add(new HiddenLayer(this[Count - 1].Z, 84, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.SGD, 0.00001 * MiniBatchSize, L2_reg, 1));
                Add(new OutputLayer(this[Count - 1].Z, Labels.GetLength(1), MiniBatchSize, ActivationFunction.SoftMax, WeightInitialization.Random, TrainAlgorithm.SGD, 0.00001 * MiniBatchSize, L2_reg, 1));
            }
            else//if MNIST-Data
            {
                ListOfFilenames = _ListOfFilenames;
                NoOfImages      = 60000;//"images per fileName-location"(t10k-images.idx3-ubyte contains 60000 images)
                MiniBatchSize   = _MiniBatchSize;
                CostFunction    = CostFunction.CrossEntropy;
                MNIST_Load      = new ImageLoader.MNIST_Loader(ListOfFilenames, MiniBatchSize, true);
                Labels          = MNIST_Load.GetLabels(0);
                Add(new InputLayer(MNIST_Load.GetZ_3D(0), MiniBatchSize));
                Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 3, 1, 6, MiniBatchSize, ActivationFunction.lReLu, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.SGD, 0.0001 * MiniBatchSize, L2_reg, 1));
                Add(new ConvolutionalLayer(this[Count - 1].Z_3D, 3, 2, 16, MiniBatchSize, ActivationFunction.lReLu, WeightInitialization.NormalizedGaussianRandom, TrainAlgorithm.SGD, 0.0001 * MiniBatchSize, L2_reg, 1));
                Add(new HiddenLayer(this[Count - 1].Z, 120, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.SGD, 0.0001 * MiniBatchSize, L2_reg, 1));
                Add(new HiddenLayer(this[Count - 1].Z, 84, MiniBatchSize, ActivationFunction.Sigmoid, WeightInitialization.Random, TrainAlgorithm.SGD, 0.0001 * MiniBatchSize, L2_reg, 1));
                Add(new OutputLayer(this[Count - 1].Z, Labels.GetLength(1), MiniBatchSize, ActivationFunction.SoftMax, WeightInitialization.Random, TrainAlgorithm.SGD, 0.0001 * MiniBatchSize, L2_reg, 1));
            }
        }
Esempio n. 27
0
        public Matrix Forward(Matrix input, Matrix expected, ref double error)
        {
            // Initiate the Input Matrix Data in MatrixData class
            Data.Data["a0"] = input;
            MatrixData data = Data;

            for (int i = 1; i < Layers.Count; i++)
            {
                Layers[i].Forward(ref data);
            }

            Data = data;

            m_actual   = Data.Data["a" + (Layers.Count - 1).ToString()];
            m_expected = expected;

            error = CostFunction.Forward(Data.Data["a" + (Layers.Count - 1).ToString()], expected, Data, Layers.Count);

            return(Data.Data["a" + (Layers.Count - 1).ToString()]);
        }
Esempio n. 28
0
        public void Minimize(double x_0, CostFunction f, int max_iterations)
        {
            double x  = x_0;
            double fx = f.Evaluate(x);

            double newtonX = 0;

            double denominator = 0;

            ContinuousSolution best_solution = new ContinuousSolution(new double[] { x }, fx);

            double?improvement = null;

            for (int k = 0; k < max_iterations; ++k)
            {
                f.CalcGradient(x, out denominator);
                if (Math.Abs(denominator) < ZERO)
                {
                    break;
                }

                newtonX = x - fx / denominator;

                if (Math.Abs(newtonX - x) < SIGMA)
                {
                    break;
                }
                x = newtonX;

                fx = f.Evaluate(x);

                if (best_solution.TryUpdateSolution(new double[] { x }, fx, out improvement))
                {
                    OnSolutionUpdated(best_solution, k);
                }

                OnStepped(new ContinuousSolution(new double[] { x }, fx), k);
            }
        }
Esempio n. 29
0
        Vector <double> Minimize(CostFunction costFunction, Vector <double> theta, Options options)
        {
            Vector <double>     input              = theta;
            int                 currentEpoch       = 0;
            int                 red                = 1;
            int                 lineSearchFailures = 0;
            ICostGradientResult evaluate           = costFunction(options.Weights.Item1, options.Weights.Item2, options.Training.Set, options.Training.Desired, options.Lambda);
            double              cost               = evaluate.Cost;
            Vector <double>     gradient           = evaluate.Gradient;

            LearningEvent(currentEpoch, options, input, cost);

            currentEpoch = currentEpoch + (options.Epochs < 0 ? 1 : 0);
            Vector <double> inverseGradient = gradient.Multiply(-1d);

            double slope = inverseGradient.Multiply(-1d).DotProduct(inverseGradient);
            double step  = red / (1.0 - slope);

            while (EpochsNotFinished(options, currentEpoch))
            {
                currentEpoch = currentEpoch + (options.Epochs > 0 ? 1 : 0);
                Vector <double> inputSet = Vector <double> .Build.DenseOfVector(input);

                double          progressCost     = cost;
                Vector <double> progressGradient = Vector <double> .Build.DenseOfVector(gradient);

                input           = input.Add(inverseGradient.Multiply(step));
                options.Weights = input.ReshapeMatrices(options.Weights.Item1.RowCount, options.Weights.Item1.ColumnCount, options.Weights.Item2.RowCount, options.Weights.Item2.ColumnCount);
                evaluate        = costFunction(options.Weights.Item1, options.Weights.Item2, options.Training.Set, options.Training.Desired, options.Lambda);
                double          epochCost     = evaluate.Cost;
                Vector <double> innerGradient = evaluate.Gradient;
                LearningEvent(currentEpoch, options, input, epochCost);

                currentEpoch = currentEpoch + (options.Epochs < 0 ? 1 : 0);
                double epochSlope      = innerGradient.DotProduct(inverseGradient);
                double innerEpochCost  = cost;
                double innerEpochSlope = slope;
                double stepBack        = -step;
                int    M       = GetM(options.Epochs, currentEpoch);
                int    success = 0;
                double limit   = -1;

                while (true)
                {
                    while (((epochCost > cost + step * RHO * slope) | (epochSlope > -SIG * slope)) && (M > 0))
                    {
                        limit = step;
                        double innerStep = GetInnerStep(cost, epochCost, epochSlope, innerEpochCost, innerEpochSlope, stepBack);
                        step            = step + innerStep;
                        input           = input.Add(inverseGradient.Multiply(innerStep));
                        options.Weights = input.ReshapeMatrices(options.Weights.Item1.RowCount, options.Weights.Item1.ColumnCount, options.Weights.Item2.RowCount, options.Weights.Item2.ColumnCount);
                        evaluate        = costFunction(options.Weights.Item1, options.Weights.Item2, options.Training.Set, options.Training.Desired, options.Lambda);
                        epochCost       = evaluate.Cost;
                        innerGradient   = evaluate.Gradient;
                        LearningEvent(currentEpoch, options, input, epochCost);
                        M            = M - 1;
                        currentEpoch = currentEpoch + (options.Epochs < 0 ? 1 : 0);
                        epochSlope   = innerGradient.DotProduct(inverseGradient);
                        stepBack     = stepBack - innerStep;
                    }
                    if (epochCost > cost + step * RHO * slope || epochSlope > -SIG * slope)
                    {
                        break;
                    }
                    else if (IsSuccessfullEpoch(slope, epochSlope))
                    {
                        success = 1;
                        break;
                    }
                    else if (M == 0)
                    {
                        break;
                    }

                    double epochStep = GetStep(step, epochSlope, innerEpochSlope, stepBack, limit, epochCost, innerEpochCost);
                    innerEpochCost  = epochCost;
                    innerEpochSlope = epochSlope;
                    stepBack        = -epochStep;
                    step            = step + epochStep;
                    input           = input.Add(inverseGradient.Multiply(epochStep));
                    options.Weights = input.ReshapeMatrices(options.Weights.Item1.RowCount, options.Weights.Item1.ColumnCount, options.Weights.Item2.RowCount, options.Weights.Item2.ColumnCount);
                    evaluate        = costFunction(options.Weights.Item1, options.Weights.Item2, options.Training.Set, options.Training.Desired, options.Lambda);
                    epochCost       = evaluate.Cost;
                    innerGradient   = evaluate.Gradient;
                    LearningEvent(currentEpoch, options, input, epochCost);
                    M            = M - 1;
                    currentEpoch = CountEpochs(options, currentEpoch);
                    epochSlope   = innerGradient.DotProduct(inverseGradient);
                }

                if (success == 1)
                {
                    cost = epochCost;
                    double numerator = (innerGradient.DotProduct(innerGradient) - gradient.DotProduct(innerGradient)) / gradient.DotProduct(gradient);
                    inverseGradient = inverseGradient.Multiply(numerator).Subtract(innerGradient);
                    Vector <double> temporaryGradient = temporaryGradient = gradient;
                    gradient      = innerGradient;
                    innerGradient = temporaryGradient;
                    epochSlope    = gradient.DotProduct(inverseGradient);
                    if (epochSlope > 0)
                    {
                        inverseGradient = gradient.Multiply(-1d);
                        epochSlope      = inverseGradient.Multiply(-1d).DotProduct(inverseGradient);
                    }
                    step  = step * Math.Min(RATIO, slope / (epochSlope - RealMin));
                    slope = epochSlope;
                    lineSearchFailures = 0;
                }
                else
                {
                    input    = inputSet;
                    cost     = progressCost;
                    gradient = progressGradient;
                    if (LineSearchFailedOrRunOutOfEpochs(options, currentEpoch, lineSearchFailures))
                    {
                        break;
                    }
                    Vector <double> temporaryGradient = gradient;
                    gradient           = innerGradient;
                    innerGradient      = temporaryGradient;
                    inverseGradient    = gradient.Multiply(-1d);
                    slope              = inverseGradient.Multiply(-1d).DotProduct(inverseGradient);
                    step               = 1d / (1d - slope);
                    lineSearchFailures = 1;
                }
            }
            return(input);
        }
Esempio n. 30
0
 public Vector <double> MinimizeFunction(CostFunction costFunction, Vector <double> theta, Options options)
 {
     return(Minimize(costFunction, theta, options));
 }