Exemplo n.º 1
0
        /// <summary>
        /// really slow but not expecting big numbers needed
        /// </summary>
        private void MakeBabiesBaby()
        {
            if (!CanMakeBabies())
              {
            System.Windows.MessageBox.Show("Need to have both teams A and B opened and genomes selected from both to make babies.  This is without replacement, so total must be at least 51.");
            return;
              }

              List<GenomeItemViewModel> selectedInA = GetSelectedItems(TeamA).ToList();
              List<GenomeItemViewModel> selectedInB = GetSelectedItems(TeamB).ToList();
              Debug.Assert(selectedInB.Count > 0 && selectedInA.Count > 0);

              var parentTotal = selectedInA.Count() + selectedInB.Count(); //total of genomes from parents
              if (parentTotal < 50)
               {
            System.Windows.MessageBox.Show(@"Need to select at least 51 genomes.  This is without replacement, so total must be at least 51.  I might be making another action with replacement in the future.  If you need this, give me an email at [email protected]");
            return;
              }

              var babyTotal = 50;

              float percentAsFloat = (float)BabyPercentageTeamB;
              int numFromB = (int)(percentAsFloat / 100 * (float)babyTotal);
              int numFromA = babyTotal - numFromB;
              int numToMake = int.Parse(NumberOfBabiesToMake);
              Random r = new Random(DateTime.Now.Millisecond + DateTime.Now.Minute);
              int randomBatchIdInt = r.Next(0, 10000);
              string batchId = randomBatchIdInt.ToString("D4");//e.g. 94 = 0094
              for (int i = 0; i < numToMake; i++)
              {
            var workingA = new List<GenomeItemViewModel>(selectedInA);
            var workingB = new List<GenomeItemViewModel>(selectedInB);

            List<Genome> newBabyGenomes = new List<Genome>();
            for (int j = 0; j < numFromA; j++)
            {
              var genomeToCopy = workingA[r.Next(0, workingA.Count)];
              workingA.Remove(genomeToCopy);
              newBabyGenomes.Add(genomeToCopy.Genome);
            }

            for (int j = 0; j < numFromB; j++)
            {
              var genomeToCopy = workingB[r.Next(0, workingB.Count)];
              workingB.Remove(genomeToCopy);
              newBabyGenomes.Add(genomeToCopy.Genome);
            }

            Team baby = new Team();
            baby.Genomes.AddRange(newBabyGenomes);

            //index in this for loop will aid in making a unique id for the baby
            //batchId identifies this particular batch in addition to the prefix
            string uniqueBabyName = BabiesPrefix + batchId + i.ToString("D3");

            if (!Directory.Exists(BabiesDirectory))
              Directory.CreateDirectory(BabiesDirectory);
            string babyFullFileNamePath = BabiesDirectory + "\\" + uniqueBabyName;
            baby.FileNameFullPath = babyFullFileNamePath;
            baby.Text = uniqueBabyName;

            Team.WriteToFile(baby, babyFullFileNamePath);
              }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the TeamA/B ViewModel with the team.
        /// If setTeamA, then setting TeamA, else setting TeamB.
        /// </summary>
        /// <param name="setTeamA">decides which team viewmodel to set, A or B</param>
        /// <param name="team"></param>
        private void SetTeam(bool setTeamA, Team team)
        {
            TeamViewModel teamViewModel = null;
              if (setTeamA)
            teamViewModel = TeamA;
              else
            teamViewModel = TeamB;

              if (teamViewModel == null)
            teamViewModel = Services.Container.GetExportedValue<TeamViewModel>();

              Debug.Assert(teamViewModel != null);

              teamViewModel.Team = team;
              teamViewModel.SaveName = Path.GetFileNameWithoutExtension(team.FileNameFullPath);
              teamViewModel.WorkingDir = Path.GetDirectoryName(team.FileNameFullPath);
        }
Exemplo n.º 3
0
        private void HybridizeSelectedGenomes()
        {
            var allSelectedGenomes = new List<Genome>();
              var teams = new TeamViewModel[] { TeamA, TeamB };
              foreach (var teamVM in teams)
              {
            var selectedGenomes = from gItemVM in teamVM.Items
                              where ((GenomeItemViewModel)gItemVM).IsChecked
                              select ((GenomeItemViewModel)gItemVM).Genome;
            allSelectedGenomes.AddRange(selectedGenomes);
              }

              if (allSelectedGenomes.Count == 0)
              {
            //Variant = null;
            return;
              }

              var newTeam = new Team();
              for (int i = 0; i < allSelectedGenomes.Count; i++)
              {
            var genome = allSelectedGenomes[i];
            genome.Id = i + 1;
              }
              newTeam.Genomes.AddRange(allSelectedGenomes);
              Variant.Team = newTeam;
              if (string.IsNullOrEmpty(Variant.WorkingDir))
            Variant.WorkingDir = TeamA.WorkingDir;
              var nameContribTeamA = TeamA.TeamFileName;
              var nameContribTeamB = TeamB.TeamFileName;
              if (!string.IsNullOrEmpty(TeamA.SaveName))
            nameContribTeamA = TeamA.SaveName;
              if (!string.IsNullOrEmpty(TeamB.SaveName))
            nameContribTeamB = TeamB.SaveName;

              Variant.SaveName = nameContribTeamA + "_" + nameContribTeamB + "_mix_" +
                         new Random().Next(0, 1000).ToString() +
                         "." + Properties.Resources.DefaultFileExtension;
        }
Exemplo n.º 4
0
        private void ComposeTeams()
        {
            var teamItems = Composer.Items.ToList<IScreen>();
              var totalWeight = 0;
              var numOfTeamsToMake = int.Parse(Composer.NumOfTeamsToMake);
              var sampledGenomes = new List<Genome>();
              var random = new Random(DateTime.Now.Millisecond + DateTime.Now.Minute + DateTime.Now.Second);
              var batchNum = random.Next(0, 1000).ToString();

              //hack: hard coding in output directory (via static resources object property, but user can't change)
              if (!Directory.Exists(Properties.Resources.InitialComposeTeamsDirectory))
            Directory.CreateDirectory(Properties.Resources.InitialComposeTeamsDirectory);

              //CALCULATE TOTAL WEIGHT FOR NORMALIZING
              for (int i = 0; i < numOfTeamsToMake; i++)
              {
            foreach (var item in teamItems)
            {
              var teamItemVM = (TeamItemViewModel)item;
              int weight = 0;
              //if our weight is empty, not an int, or less than 1, then we don't need to consider this team's genomes.
              if (string.IsNullOrEmpty(teamItemVM.TeamWeight) ||
              !int.TryParse(teamItemVM.TeamWeight, out weight) ||
              weight < 1)
            continue;
              totalWeight += weight;
            }

            //SAMPLE GENOMES FROM TEAM SOURCES
            foreach (var teamItem in teamItems)
            {
              var teamItemVM = (TeamItemViewModel)teamItem;
              int weight = 0;
              //if our weight is empty, not an int, or less than 1, then we don't need to consider this team's genomes.
              if (string.IsNullOrEmpty(teamItemVM.TeamWeight) ||
              !int.TryParse(teamItemVM.TeamWeight, out weight) ||
              weight < 1)
            continue;

              int normalizedWeight = (int)((float)weight / (float)totalWeight * (float)50);
              int numGenomesFromThisTeam = normalizedWeight;

              var team = teamItemVM.Team;
              if (team == null)
            continue;

              //select from this team numGenomesFromThisTeam times, with replacement
              for (int j = 0; j < numGenomesFromThisTeam; j++)
              {
            //Thread.Sleep(10);
            //var r = new Random(DateTime.Now.Millisecond + DateTime.Now.Minute + DateTime.Now.Second);
            var genome = team.Genomes[random.Next(0, team.Genomes.Count)]; //random is exclusive upper bound.
            sampledGenomes.Add(genome);
              }
            }

            //CREATE NEW TEAM FROM THE GENOMES WE'VE SAMPLED (with replacement)
            Team newTeam = new Team();
            newTeam.Genomes.AddRange(sampledGenomes);
            sampledGenomes.Clear();//better to do this only if have to but we don't really care do we at this point?
            totalWeight = 0;

            //GENERATE TEAM NAME
            newTeam.Text= batchNum + "_" + i.ToString("D3") + "_tm." + Properties.Resources.DefaultFileExtension;
            string fullPath = Properties.Resources.InitialComposeTeamsDirectory + "\\" + newTeam.Text;

            //SAVE THE TEAM
            Team.WriteToFile(newTeam, fullPath);
              }
        }
Exemplo n.º 5
0
        public void Save(Team team)
        {
            //HACK: SaveCurrentTeamAsVariant() this is a horrendous kluge
              string text = team.Text;
              string fullPath = team.FileNameFullPath;

              Team.CreateVariant(ref text, ref fullPath);

              team.Text = text;
              team.FileNameFullPath = fullPath;

              Team.WriteToFile(team, fullPath);
        }
Exemplo n.º 6
0
        /// <summary>
        /// This loads the stream (represents the file, but not necessarily of type FileStream) into a Team
        /// object and returns that object.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static Team LoadFromFile(Stream stream)
        {
            Team retTeam = new Team();

              var reader = new StreamReader(stream);
              stream.Position = 0;
              List<string> lines = new List<string>();
              string allText = reader.ReadToEnd();
              stream.Position = 0;
              //string[] separators = new string[] { "\\r\\n" };
              string[] allLines = Team.ParseAllText(allText);
              var currentGenome = new Genome();
              int elementIndex = 0;

              for (int i = 0; i < allLines.Length; i++)
              {
            var line = reader.ReadLine();
            var lineStart = line.Substring(0, 4);
            if (lineStart == "geno")
            {
              if (line.Substring(0, 7) == "genomes")
              {
            //we have a beginning of a genome line
            currentGenome = new Genome();
            elementIndex = 0;
            string idAsString = line.Substring(_LengthGenomeStartTag + 1, line.Length - _LengthGenomeStartTag - 1);
            currentGenome.Id = int.Parse(idAsString);
              }
              else
              {
            //we have an endgenome tag, so add the genome to the team
            retTeam.Genomes.Add(currentGenome);
            currentGenome = null;
              }
            }
            else if (lineStart == "trai")
            {
              //this line is a trait
              elementIndex++;
              var newTrait = new Trait();
              newTrait.FullLineText = line;
              currentGenome.AddTrait(newTrait, elementIndex);
            }
            else if (lineStart == "gene")
            {
              //this line is a gene element
              elementIndex++;
              var newElement = new Gene();
              newElement.FullLineText = line;
              currentGenome.AddGene(newElement, elementIndex);
            }
            else if (lineStart == "node")
            {
              //this line is a node element
              elementIndex++;
              var newElement = new Node();
              newElement.FullLineText = line;
              currentGenome.AddNode(newElement, elementIndex);
            }
            else
              throw new Exception();
              }
              return retTeam;
        }
Exemplo n.º 7
0
        public static void WriteToFile(Team team, string fileNameFullPath)
        {
            if (fileNameFullPath.Contains(@"%"))
            fileNameFullPath = Environment.ExpandEnvironmentVariables(fileNameFullPath);

              var dir = Path.GetDirectoryName(fileNameFullPath);
              if (!Directory.Exists(dir))
            Directory.CreateDirectory(dir);

              //build the team into text
              //string teamText = BuildTextFromTeam(team);
              string teamText = team.BuildText();

              //write the text to
              using (StreamWriter fileWriter = new StreamWriter(fileNameFullPath, false))
              {
            fileWriter.Write(teamText);
              }
        }