Exemple #1
0
 public override void Solve(int l, QMatrix Q, double[] p, sbyte[] y,
                            double[] alpha, double Cp, double Cn, double eps,
                            SolutionInfo si, int shrinking)
 {
     this.si = si;
     base.Solve(l, Q, p, y, alpha, Cp, Cn, eps, si, shrinking);
 }
Exemple #2
0
 public void DataCollection(QMatrix qMatrix)
 {
     if (_collectedMatrix == null)
     {
         _collectedMatrix = qMatrix;
     }
 }
Exemple #3
0
 private void Start()
 {
     _gameController = GameController.GetController();
     _brain          = _gameController.AIBrain;
     CurrentMatrix   = _brain.FindQMatrixForFood(_gameController.Goal);
     RewardMatrix    = InitRewardMatrix(_gameController.Goal);
 }
Exemple #4
0
        static void Main(string[] args)
        {
            double[,] matrix = new double[3, 3] {
                { 1, -1, 1 }, { 2, 0, 1 }, { 1, -1, 1 }
            };
            QMatrix matrix1 = new QMatrix(matrix);

            Console.WriteLine(matrix1.PrintMatrix());

            matrix = new double[3, 3] {
                { 1, -1, 1 }, { 2, 0, 1 }, { 1, -1, 1 }
            };
            QMatrix matrix2 = new QMatrix(matrix);

            if (matrix1 == matrix2)
            {
                Console.WriteLine("Матрицы равны");
            }
            else
            {
                Console.WriteLine("Матрицы не равны");
            }
            Console.WriteLine("\nПеремножение матриц");
            Console.WriteLine((matrix1 * matrix2).PrintMatrix());
            Console.WriteLine("Сложение матриц");
            Console.WriteLine((matrix1 + matrix2).PrintMatrix());
            Console.WriteLine("Вычетание матриц");
            Console.WriteLine((matrix1 - matrix2).PrintMatrix());

            Console.ReadLine();
        }
        public static string GenerateProperQMatrix(Grid grid, QMatrix qMatrix)
        {
            var dic = new Dictionary <Point, double>();

            foreach (var targetCells in qMatrix.Values)
            {
                foreach (var targetCell in targetCells.Keys)
                {
                    if (!dic.ContainsKey(targetCell))
                    {
                        dic.Add(targetCell, double.MinValue);
                    }
                    dic[targetCell] = Math.Max(dic[targetCell], targetCells[targetCell]);
                }
            }

            var matrixBuilder = new StringBuilder();

            foreach (var row in grid)
            {
                var rowBuilder = new StringBuilder();
                foreach (var cell in row)
                {
                    rowBuilder.AppendFormat("{0}\t", dic[cell]);
                }
                matrixBuilder.AppendLine(rowBuilder.ToString());
            }
            return(matrixBuilder.ToString());
        }
        private void GetSponsors()
        {
            sponsors = QProtocolRequests.GetSponsorCodes(RequestStatuses.Closed);
            QMatrix.GetSponsorNames(sponsors);
            ArrayList sortedsponsors = new ArrayList(sponsors);

            sortedsponsors.Sort(HistoryController.sortalpha());
            this.sortedsponsors = sortedsponsors;
        }
Exemple #7
0
    private bool BarrelHit(QPoint pos)
    {
        QMatrix matrix = new QMatrix();

        matrix.Translate(0, Height());
        matrix.Rotate(-currentAngle);
        matrix = matrix.Inverted();
        return(barrelRect.Contains(matrix.Map(pos)));
    }
 public void Restart()
 {
     restart     = true;
     newEpisode  = true;
     paused      = true;
     qMat        = new QMatrix();
     currentCell = 36;
     labryntDisplay.ClearTrail();
     labryntDisplay.MovePlayer(currentCell);
 }
 //Initialise the parameters for a new run of the Q-learning algorithm.
 private void InitialiseAlgorithm()
 {
     qMat           = new QMatrix();
     alpha          = alphaSelector.GetSliderValue();
     gamma          = gammaSelector.GetSliderValue();
     epsilon        = 0.99999999;
     epsilonDecay1  = System.Convert.ToDouble(epsd1Selector.captionText.text);
     epsilonDecay2  = System.Convert.ToDouble(epsd2Selector.captionText.text);
     episodeCounter = 0;
 }
        private static string GetHeaderRow(QMatrix qMatrix)
        {
            var rowBuilder = new StringBuilder();

            rowBuilder.Append("\t");
            foreach (var cell in qMatrix.Keys)
            {
                rowBuilder.AppendFormat("{0}\t", cell);
            }
            return(rowBuilder.ToString());
        }
Exemple #11
0
    //Constructor.
    public QLearningAI(double alpha, double gamma, double epsilon,
                       double epsilonDecay1, double epsilonDecay2)
    {
        this.alpha         = alpha;
        this.gamma         = gamma;
        this.epsilon       = epsilon;
        this.epsilonDecay1 = epsilonDecay1;
        this.epsilonDecay2 = epsilonDecay2;

        qMat = new QMatrix();
    }
        public void ChangeContactButtonClicked()
        {
            IList items        = QMatrix.GetSponsorContacts_NameAndCodeOnly(this.request.Contact.SponsorCode);
            Item  selectedItem = TemplatesController.ShowListBoxOptionsForm(items, view.ParentControl);

            if (selectedItem.Value != "")
            {
                CheckAndAddToAuditItems("ContactCode", this.request.Contact.ContactCode, selectedItem.Value);
                this.request.SetContact(selectedItem.Value);
                UpdateViewWithSponsorContact();
            }
        }
Exemple #13
0
 static void Main(string[] args)
 {
     QMatrix<int> M = new QMatrix<int>(3);
     Console.WriteLine(M);
     Console.WriteLine("rows: " + M.RowNumber + "\ncols: " + M.ColNumber + "\ninf: " + M.infinity + "\ninfSet: " + M.infinitySetted);
     QMatrix<int> N = new QMatrix<int>(3);
     M[0, 0] = 1;
     M[2, 2] = 3;
     Console.WriteLine("M and N equals: " + (M == N) + "\nM and N not equals: " + (M != N));
     Console.WriteLine(5 * M);
     Console.WriteLine(M * 5);
     N[2, 2] = 10;
     N[1, 1] = -4;
     Console.WriteLine(M + N);
     Console.WriteLine(M - N);
 }
        public static string GenerateQMatrix(QMatrix qMatrix)
        {
            var matrixBuilder = new StringBuilder();

            matrixBuilder.AppendLine(GetHeaderRow(qMatrix));
            foreach (var cell in qMatrix.Keys)
            {
                var rowBuilder = new StringBuilder();
                rowBuilder.AppendFormat("{0}\t", cell);
                foreach (var otherCell in qMatrix[cell].Keys)
                {
                    rowBuilder.AppendFormat("{0}\t", qMatrix[cell][otherCell]);
                }
                matrixBuilder.AppendLine(rowBuilder.ToString());
            }
            return(matrixBuilder.ToString());
        }
Exemple #15
0
        private void DoSearch()
        {
            this.sponsorContacts.Clear();
            sponsorContacts = QMatrix.GetSponsors(this.view.SearchSponsorName);
            ArrayList sorted = new ArrayList(sponsorContacts);

            sorted.Sort(ProtocolRequestAddController.sortalpha());
            this.sortedSponsorContacts = sorted;

            if (sponsorContacts.Count == 0)
            {
                MessageBox.Show("No record found.");
            }
            else
            {
                this.view.ClearView();
                IList items         = CreateContactList();
                int   selectedIndex = ShowPopup(items);
                ContactListSelectedIndex(selectedIndex);
            }
        }
Exemple #16
0
        public void SaveQMatrix()
        {
            if (_collectedMatrix == null)
            {
                return;
            }

            var food = new Vector2Int(_collectedMatrix.X, _collectedMatrix.Y);
            var data = new QMatrix(food);

            data.Generations = _collectedMatrix.Generations++;

            for (var i = 0; i < _collectedMatrix.QualityMatrix.GetLength(0); i++)
            {
                for (var j = 0; j < _collectedMatrix.QualityMatrix.GetLength(1); j++)
                {
                    var up    = _collectedMatrix.QualityMatrix[i, j].GetDirectionValue(Direction.Up);
                    var right = _collectedMatrix.QualityMatrix[i, j].GetDirectionValue(Direction.Right);
                    var down  = _collectedMatrix.QualityMatrix[i, j].GetDirectionValue(Direction.Down);
                    var left  = _collectedMatrix.QualityMatrix[i, j].GetDirectionValue(Direction.Left);

                    data.QualityMatrix[i, j].SetDirectionValue(Direction.Up, up);
                    data.QualityMatrix[i, j].SetDirectionValue(Direction.Right, right);
                    data.QualityMatrix[i, j].SetDirectionValue(Direction.Down, down);
                    data.QualityMatrix[i, j].SetDirectionValue(Direction.Left, left);
                }
            }

            foreach (var matrix in _data)
            {
                if (matrix.GetCoordinateEquals(data.X, data.Y))
                {
                    matrix.QualityMatrix = data.QualityMatrix;
                    return;
                }
            }
            _collectedMatrix = null;
            _data.Add(data);
        }
        public void ContactButtonClicked()
        {
            IList items = QMatrix.GetSponsorContacts_NameAndCodeOnly(this.request.Contact.SponsorCode);

            if (items != null)
            {
                Item selectedContact = TemplatesController.ShowListBoxOptionsForm(items, view.ParentControl);
                Debug.WriteLine(selectedContact.Text);
                Debug.WriteLine(selectedContact.Value);
                if (selectedContact != null)
                {
                    this.request.Contact.ContactName = selectedContact.Text;
                    this.request.Contact.ContactCode = selectedContact.Value;
                    this.view.ContactName            = selectedContact.Text;
                    this.request.Contact.Email       = QMatrix.GetSponsorByContactCode(selectedContact.Value).Email;
                    this.view.Email = this.request.Contact.Email;
                }
            }
            else
            {
                MessageBox.Show("There are no contacts for this sponsor!");
            }
        }
Exemple #18
0
 public Bot(Grid grid)
 {
     Policy    = new Policy();
     this.grid = grid;
     qMatrix   = new QMatrix(grid);
 }
 public void SetContact(string contactCode)
 {
     this.Contact.ContactCode = contactCode;
     this.Contact             = QMatrix.GetSponsorByContactCodeFixed(contactCode);
 }
Exemple #20
0
        private QMatrix InitRewardMatrix(Vector2Int goal)
        {
            QMatrix reward = new QMatrix(goal);

            return(reward);
        }
Exemple #21
0
        public virtual void Solve(int l, QMatrix Q, double[] p_, sbyte[] y_,
                                  double[] alpha_, double Cp, double Cn, double eps, SolutionInfo si, int shrinking)
        {
            this.l        = l;
            this.Q        = Q;
            QD            = Q.get_QD();
            p             = (double[])p_.Clone();
            y             = (sbyte[])y_.Clone();
            alpha         = (double[])alpha_.Clone();
            this.Cp       = Cp;
            this.Cn       = Cn;
            this.eps      = eps;
            this.unshrink = false;

            // initialize alpha_status
            {
                alpha_status = new sbyte[l];
                for (int i = 0; i < l; i++)
                {
                    update_alpha_status(i);
                }
            }

            // initialize active set (for shrinking)
            {
                active_set = new int[l];
                for (int i = 0; i < l; i++)
                {
                    active_set[i] = i;
                }
                active_size = l;
            }

            // initialize gradient
            {
                G     = new double[l];
                G_bar = new double[l];
                int i;
                for (i = 0; i < l; i++)
                {
                    G[i]     = p[i];
                    G_bar[i] = 0;
                }
                for (i = 0; i < l; i++)
                {
                    if (!is_lower_bound(i))
                    {
                        float[] Q_i     = Q.get_Q(i, l);
                        double  alpha_i = alpha[i];
                        int     j;
                        for (j = 0; j < l; j++)
                        {
                            G[j] += alpha_i * Q_i[j];
                        }
                        if (is_upper_bound(i))
                        {
                            for (j = 0; j < l; j++)
                            {
                                G_bar[j] += get_C(i) * Q_i[j];
                            }
                        }
                    }
                }
            }

            // optimization step

            int iter     = 0;
            int max_iter = System.Math.Max(10000000, l > int.MaxValue / 100 ? int.MaxValue : 100 * l);
            int counter  = System.Math.Min(l, 1000) + 1;

            int[] working_set = new int[2];

            while (iter < max_iter)
            {
                // show progress and do shrinking

                if (--counter == 0)
                {
                    counter = System.Math.Min(l, 1000);
                    if (shrinking != 0)
                    {
                        do_shrinking();
                    }
                    SupportVectorMachine.info(".");
                }

                if (select_working_set(working_set) != 0)
                {
                    // reconstruct the whole gradient
                    reconstruct_gradient();
                    // reset active set size and check
                    active_size = l;
                    SupportVectorMachine.info("*");
                    if (select_working_set(working_set) != 0)
                    {
                        break;
                    }
                    else
                    {
                        counter = 1;    // do shrinking next iteration
                    }
                }

                int i = working_set[0];
                int j = working_set[1];

                ++iter;

                // update alpha[i] and alpha[j], handle bounds carefully

                float[] Q_i = Q.get_Q(i, active_size);
                float[] Q_j = Q.get_Q(j, active_size);

                double C_i = get_C(i);
                double C_j = get_C(j);

                double old_alpha_i = alpha[i];
                double old_alpha_j = alpha[j];

                if (y[i] != y[j])
                {
                    double quad_coef = QD[i] + QD[j] + 2 * Q_i[j];
                    if (quad_coef <= 0)
                    {
                        quad_coef = 1e-12;
                    }
                    double delta = (-G[i] - G[j]) / quad_coef;
                    double diff  = alpha[i] - alpha[j];
                    alpha[i] += delta;
                    alpha[j] += delta;

                    if (diff > 0)
                    {
                        if (alpha[j] < 0)
                        {
                            alpha[j] = 0;
                            alpha[i] = diff;
                        }
                    }
                    else
                    {
                        if (alpha[i] < 0)
                        {
                            alpha[i] = 0;
                            alpha[j] = -diff;
                        }
                    }
                    if (diff > C_i - C_j)
                    {
                        if (alpha[i] > C_i)
                        {
                            alpha[i] = C_i;
                            alpha[j] = C_i - diff;
                        }
                    }
                    else
                    {
                        if (alpha[j] > C_j)
                        {
                            alpha[j] = C_j;
                            alpha[i] = C_j + diff;
                        }
                    }
                }
                else
                {
                    double quad_coef = QD[i] + QD[j] - 2 * Q_i[j];
                    if (quad_coef <= 0)
                    {
                        quad_coef = 1e-12;
                    }
                    double delta = (G[i] - G[j]) / quad_coef;
                    double sum   = alpha[i] + alpha[j];
                    alpha[i] -= delta;
                    alpha[j] += delta;

                    if (sum > C_i)
                    {
                        if (alpha[i] > C_i)
                        {
                            alpha[i] = C_i;
                            alpha[j] = sum - C_i;
                        }
                    }
                    else
                    {
                        if (alpha[j] < 0)
                        {
                            alpha[j] = 0;
                            alpha[i] = sum;
                        }
                    }
                    if (sum > C_j)
                    {
                        if (alpha[j] > C_j)
                        {
                            alpha[j] = C_j;
                            alpha[i] = sum - C_j;
                        }
                    }
                    else
                    {
                        if (alpha[i] < 0)
                        {
                            alpha[i] = 0;
                            alpha[j] = sum;
                        }
                    }
                }

                // update G

                double delta_alpha_i = alpha[i] - old_alpha_i;
                double delta_alpha_j = alpha[j] - old_alpha_j;

                for (int k = 0; k < active_size; k++)
                {
                    G[k] += Q_i[k] * delta_alpha_i + Q_j[k] * delta_alpha_j;
                }

                // update alpha_status and G_bar

                {
                    bool ui = is_upper_bound(i);
                    bool uj = is_upper_bound(j);
                    update_alpha_status(i);
                    update_alpha_status(j);
                    int k;
                    if (ui != is_upper_bound(i))
                    {
                        Q_i = Q.get_Q(i, l);
                        if (ui)
                        {
                            for (k = 0; k < l; k++)
                            {
                                G_bar[k] -= C_i * Q_i[k];
                            }
                        }
                        else
                        {
                            for (k = 0; k < l; k++)
                            {
                                G_bar[k] += C_i * Q_i[k];
                            }
                        }
                    }

                    if (uj != is_upper_bound(j))
                    {
                        Q_j = Q.get_Q(j, l);
                        if (uj)
                        {
                            for (k = 0; k < l; k++)
                            {
                                G_bar[k] -= C_j * Q_j[k];
                            }
                        }
                        else
                        {
                            for (k = 0; k < l; k++)
                            {
                                G_bar[k] += C_j * Q_j[k];
                            }
                        }
                    }
                }
            }

            if (iter >= max_iter)
            {
                if (active_size < l)
                {
                    // reconstruct the whole gradient to calculate objective value
                    reconstruct_gradient();
                    active_size = l;
                    SupportVectorMachine.info("*");
                }
                SupportVectorMachine.info("\nWARNING: reaching max number of iterations");
            }

            // calculate rho

            si.rho = calculate_rho();

            // calculate objective value
            {
                double v = 0;
                int    i;
                for (i = 0; i < l; i++)
                {
                    v += alpha[i] * (G[i] + p[i]);
                }

                si.obj = v / 2;
            }

            // put back the solution
            {
                for (int i = 0; i < l; i++)
                {
                    alpha_[active_set[i]] = alpha[i];
                }
            }

            si.upper_bound_p = Cp;
            si.upper_bound_n = Cn;

            SupportVectorMachine.info("\noptimization finished, #iter = " + iter + "\n");
        }