Ejemplo n.º 1
0
    public bool setMap()
    {
        // Create the map based on the file
        this.mapModel = MapGenetic.read(mapPath.text);

        // Setting map to view
        if (this.GetComponent <MapGeneticView>() != null)
        {
            this.GetComponent <MapGeneticView>().MapModel      = this.mapModel;
            this.GetComponent <MapGeneticView>().MapController = this;
        }

        this.mapModel.Graph.floydWarshall();            // Calculate all paths
        // Setting graph to genetic controller
        cities = new List <SalesmanCity>();
        for (int i = 0; i < mapModel.Targets.Count; i++)
        {
            // For each target, create a city indicating the index of the vertex
            cities.Add(new SalesmanCity(i, mapModel.getCharacterGraphPosition(mapModel.Targets[i])));
        }
        geneticController = GetComponent <GeneticSceneController>();
        geneticController.setGraph(this.mapModel.Graph, cities);
        //this.selectShowSolutionMode( showBestSolution );

        return(this.mapModel != null);
    }
 public void setType(MapGenetic.MapTileType type)
 {
     this.type = type;
     if (this.type != MapGenetic.MapTileType.NotDefined)
     {
         tileImage.sprite = tileSprites[MapGenetic.typeToTypeIndex(this.type)];
     }
 }
Ejemplo n.º 3
0
    public static MapGenetic readFromFile(string fileName)
    {
        string path = fileName;

        // UNCOMMENT FOR FIXED MAP FILES : WEB BUILD
        //return Map.read( Resources.Load<TextAsset>( fileName ).text );

        // Starting Reader
        System.IO.StreamReader reader;
        try{
            reader = new System.IO.StreamReader(path);
        }
        catch (System.Exception ex) {
            Debug.LogError("Error while opening the Map file");
            Debug.LogError(ex.Message);
            return(null);
        }

        // Reading Information
        return(MapGenetic.read(reader.ReadToEnd()));
    }
Ejemplo n.º 4
0
 public GeneticCharacter(Vector2 pos, MapGenetic map)
 {
     this.position = pos;
     this.map      = map;
 }
 public GeneticTarget(Vector2 position, MapGenetic map) : base(position, map)
 {
 }
Ejemplo n.º 6
0
    public static MapGenetic read(string content)
    {
        /*
         * File format :
         * Height Width 0|1(neighborhood4)
         * Height *
         *  [Width * MapTileType (index)]
         */

        // Reading Information
        try{
            string[]        linesWithComments = content.Split('\n');
            List <string[]> lines             = new List <string[]>();
            // filtering comments and empty lines
            for (int i = 0; i < linesWithComments.Length; i++)
            {
                string[] lineSplitSpace = linesWithComments[i].Trim().Split(' ');
                if (lineSplitSpace.Length > 0 && lineSplitSpace[0].Length > 0 && lineSplitSpace[0][0] != '#')
                {
                    lines.Add(lineSplitSpace);
                }
            }

            int lineIt = 0;

            // initial info
            string[] infoline = lines[lineIt];
            lineIt++;
            int  height        = int.Parse(infoline [0]);
            int  width         = int.Parse(infoline [1]);
            bool neighborhood4 = int.Parse(infoline [2]) == 1;
            //bool bidirectional = int.Parse(infoline [3]) == 1;

            MapGenetic m = new MapGenetic(height, width, neighborhood4, true);

            // Vertices Costs
            for (int i = 0; i < 4; i++)
            {
                // line for the cost
                string[] costLine = lines[lineIt];
                lineIt++;

                m.setMapTileTypeCost(MapGenetic.typeIndexToType(int.Parse(costLine[0])), float.Parse(costLine[1]));
            }

            // Vertices
            for (int y = 0; y < height; y++)
            {
                string[] mapLine = lines[lineIt];
                lineIt++;
                for (int x = 0; x < width; x++)
                {
                    m.addTile(x, height - 1 - y, MapGenetic.typeIndexToType(int.Parse(mapLine[x])));
                }
            }

            // Targets
            int nTargets = int.Parse(lines[lineIt][0]);
            lineIt++;
            for (int i = 0; i < nTargets; i++)
            {
                GeneticTarget t = new GeneticTarget(new Vector2(int.Parse(lines[lineIt][0]), int.Parse(lines[lineIt][1])), m);
                if (m.checkCharacterPosition(t))
                {
                    m.addTarget(t);
                }
                else
                {
                    Debug.LogError("Map : the position of the enemy " + (i + 1) + " is incorrect.");
                    throw new UnityEngine.UnityException("Map : the position of the enemy " + (i + 1) + " is incorrect.");
                }
                lineIt++;
            }

            return(m);
        }catch (System.Exception ex) {
            Debug.LogError("Error while opening the Map file : wrong format");
            Debug.LogError(ex.Message);
            return(null);
        }
    }
    float worstFitness;                 // used to calculate the time for moving the players (based on the worst fitness)

    public GeneticPlayer(Vector2 position, MapGenetic map, ChromosomeSalesman solution, bool bestSolution, float worstFitness) : base(position, map)
    {
        this.solution     = solution;
        this.bestSolution = bestSolution;
        this.worstFitness = worstFitness;
    }