コード例 #1
0
    /***
     * Fetch a pack
     * Remove old shapes
     * Get the new shapes
     */
    public override void AgentReset()
    {
        step    = 0;
        stepNum = 0;
        pack    = packEvol.ReadPack(packFileName, packID);
        GameObject _brain = GameObject.Find("Academy/Brain");
        Brain      brain  = _brain.transform.GetComponent <Brain>();

        // Destroying the already present shapes
        if (shapes != null)
        {
            for (int i = 0; i < shapes.Length; i++)
            {
                UnityEngine.Object.DestroyImmediate(shapes[i]);
            }
            shapes = null;
        }

        if (rotBeforeMov)
        {
            validPos = new int[1, 1, MOV_RES, MOV_RES, MOV_RES];
        }
        else
        {
            validPos = new int[packEvol.r1Value.Count, packEvol.r2Value.Count, MOV_RES, MOV_RES, MOV_RES];
        }

        if (!loadPrecompute)
        {
            if (getGT)
            {
                gtStepAction = gtPack.GetGtStepAction(ref pack, rotBeforeMov: rotBeforeMov);
            }

            if (getSavedAct)
            {
                gtStepAction = GetStepAction();
            }

            // Loading the new shapes
            string[] _null = null;
            packEvol.GetShapes(ref _null, ref pack.sources, ref shapes, loadFromResources: false, keepRenderer: editorMode);
            for (int i = 0; i < shapes.Length; i++)
            {
                shapes[i].transform.localScale = pack.scales[i];
            }

            // Move shapes in visible pisitions in demoMode
            // Also removing box colliders for faster processing
            if (demoMode)
            {
                float theta = (2.0f * Mathf.PI) / shapes.Length;
                for (int i = 0; i < shapes.Length; i++)
                {
                    BoxCollider[] boxes = shapes[i].GetComponents <BoxCollider>();
                    foreach (BoxCollider box in boxes)
                    {
                        Destroy(box);
                    }

                    shapes[i].transform.position = new Vector3(2.0f * Mathf.Sin(i * theta),
                                                               0f,
                                                               2.0f * Mathf.Cos(i * theta));
                }
            }
            else
            {
                // intial estimate added so as to ensure faster computation
                vox = new List <float> (Mathf.CeilToInt(pack.efficiency * VOX_RES * VOX_RES * VOX_RES));
                for (int i = 0; i < shapes.Length; i++)
                {
                    shapes[i].transform.position = Vector3.zero;
                    boxVoxel(ref vox, i);
                    shapes[i].transform.position = 100 * Vector3.one;
                }
                vox.TrimExcess();

                // setting up the number of observarions
                brain.brainParameters.vectorObservationSize =
                    ((VOX_RES * VOX_RES * VOX_RES) > (vox.Count + 1)) ? (VOX_RES * VOX_RES * VOX_RES) + 2 : vox.Count + 3;
            }
        }
        else
        {
            shapes = new GameObject[pack.sources.Length];
            precompute.loadPrecompute(packFileName, packID, ref gtStepAction, rotBeforeMov);
            brain.brainParameters.vectorObservationSize = (VOX_RES * VOX_RES * VOX_RES) + 2;
            if (getSavedAct)
            {
                gtStepAction = GetStepAction();
            }
        }
    }
コード例 #2
0
    // packFileName is the file name relative to the steamingAssets folder
    // outNameUnity is the file name for the precomputed unity file relative to the steamingAssets folder
    // outNamePython is the file name for the precomputed python file relative to the steamingAssets folder
    public void PrecomptePack(string packFileName, int packID, string outNameUnity, string outNamePython)
    {
        // Step 0: not recomputing if files already exist
        if (System.IO.File.Exists(Application.streamingAssetsPath + "/" + outNameUnity) &&
            System.IO.File.Exists(Application.streamingAssetsPath + "/" + outNamePython))
        {
            return;
        }

        // Step 1: Read the pack
        Pack pack = packEvol.ReadPack(packFileName, packID);


        // Step 2a: Get gt action
        List <StepAction> gtRotBeforeMov    = gtPack.GetGtStepAction(ref pack, rotBeforeMov: true);
        List <StepAction> gtNotRotBeforeMov = gtPack.GetGtStepAction(ref pack, rotBeforeMov: false);

        // Step 2b: Get gt2 action
        List <StepAction2> gtRotBeforeMov2    = gtPack.GetGtStepAction2(ref pack, gtRotBeforeMov, rotBeforeMov: true);
        List <StepAction2> gtNotRotBeforeMov2 = gtPack.GetGtStepAction2(ref pack, gtNotRotBeforeMov, rotBeforeMov: false);

        // Step 3: Get shapes
        GameObject[] shapes = null;
        string[]     _null  = null;
        packEvol.GetShapes(ref _null, ref pack.sources, ref shapes, loadFromResources: false, keepRenderer: false);
        for (int i = 0; i < shapes.Length; i++)
        {
            shapes[i].transform.localScale = pack.scales[i];
        }

        // Step 4: Get shape voxels
        List <float> vox = new List <float> (Mathf.CeilToInt(pack.efficiency
                                                             * packingAgent.VOX_RES
                                                             * packingAgent.VOX_RES
                                                             * packingAgent.VOX_RES));

        for (int i = 0; i < shapes.Length; i++)
        {
            shapes[i].transform.position = Vector3.zero;
            packingAgent.boxVoxel(ref vox, i);
            shapes[i].transform.position = 100 * Vector3.one;
        }
        vox.TrimExcess();

        // Destroy the shapes
        for (int i = 0; i < shapes.Length; i++)
        {
            Object.DestroyImmediate(shapes[i]);
        }

        // Step 5: Calculate and Write PrecomputeUnity
        string           path            = Application.streamingAssetsPath + "/" + outNameUnity;
        PrecomputeUnity2 precomputeUnity = new PrecomputeUnity2(gtRotBeforeMov, gtNotRotBeforeMov,
                                                                gtRotBeforeMov2, gtNotRotBeforeMov2);

        File.WriteAllText(path, JsonUtility.ToJson(precomputeUnity));

        // Step 6: Calculate and Write PrecomputePython
        path = Application.streamingAssetsPath + "/" + outNamePython;
        PrecomputePython precomputePython = new PrecomputePython(vox);

        File.WriteAllText(path, JsonUtility.ToJson(precomputePython));
    }