Пример #1
0
    /// <summary>
    /// Method for creating a new ConfigurablePart on a grid. Should be eventualy removed
    /// </summary>
    /// <param name="grid">The grid to create the ConfigurablePart on</param>
    /// <returns>The resulting ConfigurablePart</returns>
    public ConfigurablePart NewPart(VoxelGrid grid)
    {
        //This method does not need to exist, the constructor is enough
        //Check PPSpace and PPSpaceRequest Json reading methods when implementing new reading method
        //This is probably creating double the amount of spaces
        ConfigurablePart p = new ConfigurablePart();

        p.Type        = PartType.Configurable;
        p.Orientation = (PartOrientation)System.Enum.Parse(typeof(PartOrientation), OrientationName, false);
        p.Grid        = grid;
        p.IsStatic    = false;
        p.Height      = Height;

        p.OCIndexes = OCIndexes;
        var indexes = p.OCIndexes.Split(';');

        p.nVoxels         = indexes.Length;
        p.OccupiedIndexes = new Vector3Int[p.nVoxels];
        p.OccupiedVoxels  = new Voxel[p.nVoxels];

        for (int i = 0; i < p.nVoxels; i++)
        {
            var        index  = indexes[i];
            var        coords = index.Split('_');
            Vector3Int vector = new Vector3Int(int.Parse(coords[0]), int.Parse(coords[1]), int.Parse(coords[2]));
            var        voxel  = grid.Voxels[vector.x, vector.y, vector.z];
            voxel.IsOccupied     = true;
            voxel.Part           = p;
            p.OccupiedIndexes[i] = vector;
            p.OccupiedVoxels[i]  = voxel;
        }
        p.ReferenceIndex = p.OccupiedIndexes[0];
        p.CalculateCenter();
        return(p);
    }
Пример #2
0
    /// <summary>
    /// Populates parts and save the result grid state as an image for a given amount of times
    /// </summary>
    /// <param name="quantity">The quantity of states to save</param>
    protected virtual void PopulateRandomAndSave(int quantity)
    {
        for (int n = 0; n < quantity; n++)
        {
            PopSeed = System.DateTime.Now.Millisecond;
            MainGrid.ClearGrid();
            _existingParts = new List <Part>();

            for (int i = 0; i < _nComponents; i++)
            {
                string           partName = $"CP_{i}";
                bool             success  = false;
                ConfigurablePart p        = new ConfigurablePart();
                int attempt = 0;
                while (!success)
                {
                    p = new ConfigurablePart(MainGrid, !_showVoxels, PopSeed + attempt, partName, out success);
                    attempt++;
                }
                //MainGrid.ExistingParts.Add(p);
                _existingParts.Add(p);
            }
            ImageReadWrite.WriteGrid2Image(MainGrid, n);
        }
    }
Пример #3
0
    /// <summary>
    /// Populates <see cref="BoundaryParts"/> with the <see cref="ConfigurablePart"/>s that
    /// define the boundary of this space
    /// </summary>
    private void SetBoundaryConfigurableParts()
    {
        BoundaryParts = new HashSet <ConfigurablePart>();

        foreach (var voxel in BoundaryVoxels)
        {
            var neighbours = voxel.GetFaceNeighbours();
            foreach (var neighbour in neighbours)
            {
                if (!neighbour.IsOccupied)
                {
                    continue;
                }
                else
                {
                    if (neighbour.Part.Type == PartType.Configurable)
                    {
                        ConfigurablePart part = (ConfigurablePart)neighbour.Part;
                        if (!BoundaryParts.Contains(part))
                        {
                            BoundaryParts.Add(part);
                            //part.AssociateSpace(this);
                        }
                    }
                }
            }
        }
    }
Пример #4
0
    public ConfigurablePart NewPart(VoxelGrid grid)
    {
        ConfigurablePart p = new ConfigurablePart();

        p.Type        = PartType.Configurable;
        p.Orientation = (PartOrientation)System.Enum.Parse(typeof(PartOrientation), OrientationName, false);
        p._grid       = grid;
        p.IsStatic    = false;
        p.Height      = Height;

        p.OCIndexes = OCIndexes;
        var indexes = p.OCIndexes.Split(';');

        p.nVoxels         = indexes.Length;
        p.OccupiedIndexes = new Vector3Int[p.nVoxels];
        p.OccupiedVoxels  = new Voxel[p.nVoxels];

        for (int i = 0; i < p.nVoxels; i++)
        {
            var        index  = indexes[i];
            var        coords = index.Split('_');
            Vector3Int vector = new Vector3Int(int.Parse(coords[0]), int.Parse(coords[1]), int.Parse(coords[2]));
            var        voxel  = grid.Voxels[vector.x, vector.y, vector.z];
            voxel.IsOccupied     = true;
            voxel.Part           = p;
            p.OccupiedIndexes[i] = vector;
            p.OccupiedVoxels[i]  = voxel;
        }
        p.ReferenceIndex = p.OccupiedIndexes[0];
        p.CalculateCenter();
        return(p);
    }
 void PopulateRandomConfigurable_NAI(int amt, int seed)
 {
     for (int i = 0; i < amt; i++)
     {
         ConfigurablePart p = new ConfigurablePart(_grid, !_showVoxels, seed);
         _existingParts.Add(p);
     }
 }
Пример #6
0
 //
 //METHODS AND FUNCTIONS
 //
 void PopulateRandomConfigurable(int amt)
 {
     for (int i = 0; i < amt; i++)
     {
         ConfigurablePart p = new ConfigurablePart(_grid, _existingParts);
         _existingParts.Add(p);
     }
 }
Пример #7
0
 /// <summary>
 /// Initializes blank, not yet applied to the grid, <see cref="ConfigurablePart"/>
 /// </summary>
 protected void CreateBlankConfigurables()
 {
     for (int i = 0; i < _nComponents; i++)
     {
         ConfigurablePart p = new ConfigurablePart(MainGrid, !_showVoxels, $"CP_{i}");
         MainGrid.ExistingParts.Add(p);
         _existingParts.Add(p);
         _agents.Add(p.CPAgent);
     }
 }
Пример #8
0
 void PopulateRandomConfigurableAndSave(int amt, int variations, string prefix)
 {
     for (int n = 0; n < variations; n++)
     {
         _grid.ClearGrid();
         _existingParts = new List <Part>();
         for (int i = 0; i < amt; i++)
         {
             ConfigurablePart p = new ConfigurablePart(_grid, false, n);
             _existingParts.Add(p);
         }
         ImageReadWrite.WriteGrid2Image(_grid, n, prefix);
     }
 }
Пример #9
0
 /// <summary>
 /// Populates a given number of configurable parts on the grid
 /// </summary>
 /// <param name="amt">The amount of parts to create</param>
 protected virtual void PopulateRandomConfigurables(int amt)
 {
     for (int i = 0; i < amt; i++)
     {
         string           partName = $"CP_{i}";
         bool             success  = false;
         ConfigurablePart p        = new ConfigurablePart();
         int attempt = 0;
         while (!success)
         {
             p = new ConfigurablePart(MainGrid, !_showVoxels, PopSeed + attempt, partName, out success);
             attempt++;
         }
         MainGrid.ExistingParts.Add(p);
         _existingParts.Add(p);
     }
 }
    /// <summary>
    /// Clears the grid and populate a given amount of new configurable parts on the grid
    /// </summary>
    /// <param name="amt">The amount of parts to populate</param>
    /// <returns>The Texture that represents the grid state</returns>
    Texture2D PopulateRandomConfigurableGetImage(int amt)
    {
        _grid.ClearGrid();
        var configurables = _existingParts.OfType <ConfigurablePart>();

        foreach (var c in configurables)
        {
            c.DestroyGO();
        }
        _existingParts = new List <Part>();
        for (int i = 0; i < amt; i++)
        {
            ConfigurablePart p = new ConfigurablePart(_grid, !_showVoxels, _popSeed);
            _existingParts.Add(p);
        }
        //Write image to temp_sr folder
        return(ImageReadWrite.TextureFromGridOriginal(_grid));
    }
    Texture2D PopulateRandomConfigurableAndAnalyse(int amt, string prefix)
    {
        _grid.ClearGrid();
        var configurables = _existingParts.OfType <ConfigurablePart>();

        foreach (var c in configurables)
        {
            c.DestroyGO();
        }
        _existingParts = new List <Part>();
        for (int i = 0; i < amt; i++)
        {
            ConfigurablePart p = new ConfigurablePart(_grid, !_showVoxels, _compSeed);
            _existingParts.Add(p);
        }
        //Write image to temp_sr folder
        return(ImageReadWrite.ReadWriteAI(_grid, prefix));
    }
Пример #12
0
    public static List <ConfigurablePart> ReadConfigurablesAsList(VoxelGrid grid, string file)
    {
        List <ConfigurablePart> outList = new List <ConfigurablePart>();
        string          jsonString      = Resources.Load <TextAsset>(file).text;
        CPartCollection partList        = JsonUtility.FromJson <CPartCollection>(jsonString);

        foreach (var part in partList.Parts)
        {
            ConfigurablePart p = new ConfigurablePart();
            p.OCIndexes       = part.OCIndexes;
            p.OrientationName = part.OrientationName;
            p.OccupiedIndexes = part.OccupiedIndexes;
            p.Height          = part.Height;

            outList.Add(p.NewPart(grid));
        }

        return(outList);
    }
Пример #13
0
 void PopulateRandomConfigurableAndSave(int amt, int variations)
 {
     for (int n = 0; n < variations; n++)
     {
         _populated = true;
         _grid.ClearGrid();
         _existingParts = new List <Part>();
         if (_bigGrid)
         {
             ReadStructure("StructureParts_BigSlab");
         }
         for (int i = 0; i < amt; i++)
         {
             ConfigurablePart p = new ConfigurablePart(_grid, false, n);
             _existingParts.Add(p);
         }
         ImageReadWrite.WriteGrid2Image(_grid, n);
     }
 }
Пример #14
0
    /// <summary>
    /// Check if there are enough requests to initialize reconfiguration. If so, pauses simulation
    /// and tells the agents to store their current positions
    /// </summary>
    protected override void CheckForReconfiguration()
    {
        //If the space count (of non-spare spaces) is 0, restart the position of every other part
        if (_spaces.Where(s => !s.IsSpare).Count() == 1)
        {
            for (int i = 0; i < _agents.Count; i++)
            {
                if (i % 2 == 0)
                {
                    ConfigurablePart part = _agents[i].GetPart();
                    int  attempt          = 0;
                    bool success          = false;
                    foreach (var index in part.OccupiedIndexes)
                    {
                        var voxel = MainGrid.Voxels[index.x, index.y, index.z];
                        voxel.IsOccupied = false;
                        voxel.Part       = null;
                    }
                    while (!success)
                    {
                        part.JumpToNewPosition(PopSeed + attempt, out success);
                        attempt++;
                    }
                }
            }

            FinilizeReconfiguration();
            _activeRequest = null;
            foreach (var agent in _agents)
            {
                agent.FreezeAgent();
                agent.ClearRequest();
            }
            ResetSpacesEvaluation();
        }

        //If all ok, continue checking for reconfiguration
        else if (_spaces.Count(s => s.Reconfigure) > 0)
        {
            _timePause = true;
            //Get only the first one and only create one type of request.
            var toReconfigure = _spaces.Where(s => s.Reconfigure).ToList();
            var target        = toReconfigure[0];
            if (target.Reconfigure_Area)
            {
                //SetAreaModel();
                int targetArea = 1;
                if (target._areaDecrease > target._areaIncrease)
                {
                    targetArea = -1;
                }

                ReconfigurationRequest rr = new ReconfigurationRequest(target, targetArea, 0);
                _activeRequest = rr;
                //_reconfigurationRequests.Add(rr);
            }
            else if (target.Reconfigure_Connectivity)
            {
                //SetConnectivityModel();
                int targetConnec = 1;
                if (target._connectivityDecrease > target._connectivityIncrease)
                {
                    targetConnec = -1;
                }

                ReconfigurationRequest rr = new ReconfigurationRequest(target, 0, targetConnec);
                _activeRequest = rr;
                //_reconfigurationRequests.Add(rr);
            }

            var spaces = _spaces.Where(s => s.Reconfigure).ToList();
            for (int i = 1; i < _spaces.Count(s => s.Reconfigure); i++)
            {
                PPSpace space = spaces[i];
                space.ResetAreaEvaluation();
                space.ResetConnectivityEvaluation();
                space.TimesSurvived = 0;
                space.TimesUsed     = 0;
            }
        }
    }
Пример #15
0
 /// <summary>
 /// Sets the <see cref="ConfigurablePart"/> that should be associated with this agent
 /// and its <see cref="PP_Environment"/>
 /// </summary>
 /// <param name="part">The <see cref="ConfigurablePart"/></param>
 public void SetPart(ConfigurablePart part)
 {
     _part        = part;
     _environment = _part._environment;
 }
Пример #16
0
 public void EnsurePartInGrid(ConfigurablePart part)
 {
     part.OccupyVoxels();
 }