Beispiel #1
0
        private void InitVars()
        {
            this.m_vars = new MyVariable[this.MAX_VARIABLES];

            for (int i = 0; i < m_labels.Length; i++)
            {
                m_vars[i] = new MyVariable(m_labels[i], 0);
            }
        }
 /// <summary>
 /// Size is given by the no of variables , but only in case that the variable is contained in the ds.
 /// </summary>
 /// <returns></returns>
 private int ReadSize(IDecisionSpace ds, MyVariable v, int varInd, String predictorName)
 {
     if (ds.IsVariableIncluded(varInd))
     {
         if (variableWarned.ContainsKey(predictorName + v.GetLabel()))
         {
             variableWarned.Remove(predictorName + v.GetLabel());
         }
         return(v.Values.Count);
     }
     if (!variableWarned.ContainsKey(predictorName + v.GetLabel()))
     {
         variableWarned.Add(predictorName + v.GetLabel(), v);
         MyLog.DEBUG.WriteLine("Observer Warning: variable " + v.GetLabel() + " not contained in the DS: " + predictorName);
     }
     return(1);
 }
            /// <summary>
            /// Method creates 2D array of max action utilities and max action labels over across selected dimensions.
            /// The values in the memory are automatically scaled into the interval 0,1. Realtime values are multiplied by motivations.
            /// </summary>
            /// <param name="values">array passed by reference for storing utilities of best action</param>
            /// <param name="labelIndexes">array of the same size for best action indexes</param>
            /// <param name="XVarIndex">global index of state variable in the VariableManager</param>
            /// <param name="YVarIndex">the same: y axis</param>
            /// <param name="showRealtimeUtilities">show current utilities (scaled by the current motivation)</param>
            public void ReadTwoDimensions(ref float[,] values, ref int[,] labelIndexes,
                                          int XVarIndex, int YVarIndex, bool showRealtimeUtilities)
            {
                if (XVarIndex >= Owner.Rds.VarManager.MAX_VARIABLES)
                {
                    XVarIndex = Owner.Rds.VarManager.MAX_VARIABLES - 1;
                }
                if (YVarIndex >= Owner.Rds.VarManager.MAX_VARIABLES)
                {
                    YVarIndex = Owner.Rds.VarManager.MAX_VARIABLES - 1;
                }
                if (YVarIndex < 0)
                {
                    YVarIndex = 0;
                }
                if (XVarIndex < 0)
                {
                    XVarIndex = 0;
                }
                MyQSAMemory mem = Owner.Memory;

                int[] sizes   = mem.GetStateSizes();            // size of the matrix
                int[] indexes = Owner.Rds.VarManager.GetCurrentState();

                int[] actionGlobalIndexes = mem.GetGlobalActionIndexes();

                MyVariable varX = Owner.Rds.VarManager.GetVarNo(XVarIndex);
                MyVariable varY = Owner.Rds.VarManager.GetVarNo(YVarIndex);

                float[] varXvals = varX.Values.ToArray();
                float[] varYvals = varY.Values.ToArray();

                Array.Sort(varXvals);
                Array.Sort(varYvals);

                int sx = 0;
                int sy = 0;

                sx = varX.Values.Count;
                sy = varY.Values.Count;

                if (values == null || labelIndexes == null ||
                    values.GetLength(0) != sx || values.GetLength(1) != sy ||
                    labelIndexes.GetLength(0) != sx || labelIndexes.GetLength(1) != sy)
                {
                    values       = new float[sx, sy];
                    labelIndexes = new int[sx, sy];
                }

                for (int i = 0; i < sx; i++)
                {
                    indexes[XVarIndex] = (int)varXvals[i];

                    for (int j = 0; j < sy; j++)
                    {
                        indexes[YVarIndex] = (int)varYvals[j];

                        float[] utilities      = mem.ReadData(indexes);
                        float   memoryMaxValue = Owner.LearningAlgorithm.GetMaxVal();

                        if (memoryMaxValue != 0)
                        {
                            for (int k = 0; k < utilities.Length; k++)
                            {
                                utilities[k] = utilities[k] / memoryMaxValue;
                            }
                        }

                        float maxValue = 0.0f;
                        int   maxIndex = 0;

                        if (utilities.Length != actionGlobalIndexes.Length)
                        {
                            MyLog.DEBUG.WriteLine("ERROR: unexpected length of utilities array, will place default values");
                            utilities = new float[actionGlobalIndexes.Length];
                        }
                        else if (actionGlobalIndexes.Length == 0)
                        {
                            MyLog.DEBUG.WriteLine("WARNING: this DS contains no actions. Will use the action 0");
                            utilities           = new float[1];
                            actionGlobalIndexes = new int[] { 0 };
                        }
                        else
                        {
                            maxValue = utilities.Max();
                            maxIndex = utilities.ToList().IndexOf(maxValue);
                        }
                        if (showRealtimeUtilities)
                        {
                            Owner.MotivationInput.SafeCopyToHost();
                            float motivation = Owner.MotivationInput.Host[0];

                            values[i, j] = maxValue * motivation;
                        }
                        else
                        {
                            values[i, j] = maxValue;
                        }
                        labelIndexes[i, j] = actionGlobalIndexes[maxIndex];
                    }
                }
            }
            /// <summary>
            /// For a given predictor, the method creates 2D array of max action utilities and max action labels over selected dimensions.
            /// The values in the memory are automatically scaled into the interval 0,1. Realtime values are multililed by motivations (therfore are bigger).
            /// </summary>
            /// <param name="values">array passed by reference for storing utilities of best action</param>
            /// <param name="labelIndexes">array of the same size for best action indexes</param>
            /// <param name="predictor">an asbtract action</param>
            /// <param name="XVarIndex">global index of state variable in the VariableManager</param>
            /// <param name="YVarIndex">the same: y axis</param>
            /// <param name="showRealtimeUtilities">show current utilities (scaled by motivations from the source and the hierarchy?)</param>
            public void ReadTwoDimensions(ref float[,] values, ref int[,] labelIndexes,
                                          MyStochasticReturnPredictor predictor, int XVarIndex, int YVarIndex, bool showRealtimeUtilities)
            {
                MyRootDecisionSpace rds = predictor.Rds;

                if (XVarIndex >= rds.VarManager.MAX_VARIABLES)
                {
                    XVarIndex = rds.VarManager.MAX_VARIABLES - 1;
                }
                if (YVarIndex >= rds.VarManager.MAX_VARIABLES)
                {
                    YVarIndex = rds.VarManager.MAX_VARIABLES - 1;
                }
                if (YVarIndex < 0)
                {
                    YVarIndex = 0;
                }
                if (XVarIndex < 0)
                {
                    XVarIndex = 0;
                }
                MyQSAMemory mem = predictor.Mem;

                int[] sizes   = mem.GetStateSizes();                      // size of the matrix
                int[] indexes = predictor.Ds.GetCurrentState();           // initial indexes

                int[] actionGlobalIndexes = mem.GetGlobalActionIndexes(); // global indexes of actions in the memory

                int promotedIndex = predictor.GetPromotedVariableIndex();

                MyVariable varX = rds.VarManager.GetVarNo(XVarIndex);
                MyVariable varY = rds.VarManager.GetVarNo(YVarIndex);

                float[] varXvals = varX.Values.ToArray();
                float[] varYvals = varY.Values.ToArray();

                Array.Sort(varXvals);
                Array.Sort(varYvals);

                int sx = 0;
                int sy = 0;

                if (XVarIndex == promotedIndex)
                {
                    sx = 1;
                    indexes[XVarIndex] = 0;

                    varXvals = new float[] { 0 };

                    sy = this.ReadSize(predictor.Ds, varY, YVarIndex, predictor.GetLabel());
                }
                else if (YVarIndex == promotedIndex)
                {
                    sy = 1;
                    indexes[YVarIndex] = 0;

                    varYvals = new float[] { 0 };

                    sx = this.ReadSize(predictor.Ds, varX, XVarIndex, predictor.GetLabel());
                }
                else
                {
                    sx = this.ReadSize(predictor.Ds, varX, XVarIndex, predictor.GetLabel());
                    sy = this.ReadSize(predictor.Ds, varY, YVarIndex, predictor.GetLabel());
                }

                if (values == null || labelIndexes == null ||
                    values.GetLength(0) != sx || values.GetLength(1) != sy ||
                    labelIndexes.GetLength(0) != sx || labelIndexes.GetLength(1) != sy)
                {
                    values       = new float[sx, sy];
                    labelIndexes = new int[sx, sy];
                }

                for (int i = 0; i < sx; i++)
                {
                    indexes[XVarIndex] = (int)varXvals[i];

                    for (int j = 0; j < sy; j++)
                    {
                        indexes[YVarIndex] = (int)varYvals[j];

                        float[] utilities = mem.ReadData(indexes);
                        if (predictor.GetMaxMemoryValue() != 0)
                        {
                            for (int k = 0; k < utilities.Length; k++)
                            {
                                utilities[k] = utilities[k] / predictor.GetMaxMemoryValue();
                            }
                        }

                        float maxValue = 0.0f;
                        int   maxIndex = 0;

                        if (utilities.Length != actionGlobalIndexes.Length)
                        {
                            MyLog.DEBUG.WriteLine("ERROR: unexpected length of utilities array, will place default values");
                            utilities = new float[actionGlobalIndexes.Length];
                        }
                        else if (actionGlobalIndexes.Length == 0)
                        {
                            MyLog.DEBUG.WriteLine("WARNING: this DS contains no actions. Will use the action 0");
                            utilities           = new float[1];
                            actionGlobalIndexes = new int[] { 0 };
                        }
                        else
                        {
                            maxValue = utilities.Max();
                            maxIndex = utilities.ToList().IndexOf(maxValue);
                        }
                        if (showRealtimeUtilities)
                        {
                            values[i, j] = maxValue * predictor.GetMyTotalMotivation();
                        }
                        else
                        {
                            values[i, j] = maxValue;
                        }
                        labelIndexes[i, j] = actionGlobalIndexes[maxIndex];
                    }
                }
            }
        private void InitVars()
        {
            this.m_vars = new MyVariable[this.MAX_VARIABLES];

            for (int i = 0; i < m_labels.Length; i++)
            {
                m_vars[i] = new MyVariable(m_labels[i], 0);
            }
        }