Ejemplo n.º 1
0
        /// <summary>
        /// Convert metadata object to proto object.
        /// </summary>
        public static DemonstrationMetaProto ToProto(this DemonstrationMetaData dm)
        {
            var demoProto = new DemonstrationMetaProto
            {
                ApiVersion        = DemonstrationMetaData.ApiVersion,
                MeanReward        = dm.meanReward,
                NumberSteps       = dm.numberExperiences,
                NumberEpisodes    = dm.numberEpisodes,
                DemonstrationName = dm.demonstrationName
            };

            return(demoProto);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize metadata values based on proto object.
        /// </summary>
        public static DemonstrationMetaData ToDemonstrationMetaData(this DemonstrationMetaProto demoProto)
        {
            var dm = new DemonstrationMetaData
            {
                numberEpisodes    = demoProto.NumberEpisodes,
                numberExperiences = demoProto.NumberSteps,
                meanReward        = demoProto.MeanReward,
                demonstrationName = demoProto.DemonstrationName
            };

            if (demoProto.ApiVersion != DemonstrationMetaData.ApiVersion)
            {
                throw new Exception("API versions of demonstration are incompatible.");
            }
            return(dm);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Writes the initial data to the stream.
        /// </summary>
        public void Initialize(
            string demonstrationName, BrainParameters brainParameters, string brainName)
        {
            if (m_Writer == null)
            {
                // Already closed
                return;
            }

            m_MetaData = new DemonstrationMetaData {
                demonstrationName = demonstrationName
            };
            var metaProto = m_MetaData.ToProto();

            metaProto.WriteDelimitedTo(m_Writer);

            WriteBrainParameters(brainName, brainParameters);
        }
Ejemplo n.º 4
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var inputType = Path.GetExtension(ctx.assetPath);

            if (inputType == null)
            {
                throw new Exception("Demonstration import error.");
            }

            try
            {
                // Read first two proto objects containing metadata and brain parameters.
                Stream reader = File.OpenRead(ctx.assetPath);

                var metaDataProto = DemonstrationMetaProto.Parser.ParseDelimitedFrom(reader);
                var metaData      = new DemonstrationMetaData(metaDataProto);

                reader.Seek(DemonstrationStore.MetaDataBytes + 1, 0);
                var brainParamsProto = BrainParametersProto.Parser.ParseDelimitedFrom(reader);
                var brainParameters  = new BrainParameters(brainParamsProto);

                reader.Close();

                var demonstration = ScriptableObject.CreateInstance <Demonstration>();
                demonstration.Initialize(brainParameters, metaData);
                userData = demonstration.ToString();

                Texture2D texture = (Texture2D)
                                    AssetDatabase.LoadAssetAtPath(IconPath, typeof(Texture2D));

#if UNITY_2017_3_OR_NEWER
                ctx.AddObjectToAsset(ctx.assetPath, demonstration, texture);
                ctx.SetMainObject(demonstration);
#else
                ctx.SetMainAsset(ctx.assetPath, demonstration);
#endif
            }
            catch
            {
                return;
            }
        }
        /// <summary>
        /// Creates demonstration file.
        /// </summary>
        private void CreateDemonstrationFile(string demonstrationName)
        {
            var literalName = demonstrationName;

            filePath = DemoDirecory + literalName + ExtensionType;
            var uniqueNameCounter = 0;

            while (fileSystem.File.Exists(filePath))
            {
                literalName = demonstrationName + "_" + uniqueNameCounter;
                filePath    = DemoDirecory + literalName + ExtensionType;
                uniqueNameCounter++;
            }

            writer   = fileSystem.File.Create(filePath);
            metaData = new DemonstrationMetaData {
                demonstrationName = demonstrationName
            };
            var metaProto = metaData.ToProto();

            metaProto.WriteDelimitedTo(writer);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates demonstration file.
        /// </summary>
        void CreateDemonstrationFile(string demonstrationName)
        {
            // Creates demonstration file.
            var literalName = demonstrationName;

            m_FilePath = k_DemoDirectory + literalName + k_ExtensionType;
            var uniqueNameCounter = 0;

            while (m_FileSystem.File.Exists(m_FilePath))
            {
                literalName = demonstrationName + "_" + uniqueNameCounter;
                m_FilePath  = k_DemoDirectory + literalName + k_ExtensionType;
                uniqueNameCounter++;
            }

            m_Writer   = m_FileSystem.File.Create(m_FilePath);
            m_MetaData = new DemonstrationMetaData {
                demonstrationName = demonstrationName
            };
            var metaProto = m_MetaData.ToProto();

            metaProto.WriteDelimitedTo(m_Writer);
        }
Ejemplo n.º 7
0
 public void Initialize(BrainParameters brainParameters,
                        DemonstrationMetaData demonstrationMetaData)
 {
     this.brainParameters = brainParameters;
     metaData             = demonstrationMetaData;
 }