Daily Archives

Articles indexed Thursday March 18 2010

Page 3/133 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Help with a logic problem

    - by Stradigos
    I'm having a great deal of difficulty trying to figure out the logic behind this problem. I have developed everything else, but I really could use some help, any sort of help, on the part I'm stuck on. Back story: *A group of actors waits in a circle. They "count off" by various amounts. The last few to audition are thought to have the best chance of getting the parts and becoming stars. Instead of actors having names, they are identified by numbers. The "Audition Order" in the table tells, reading left-to-right, the "names" of the actors who will be auditioned in the order they will perform.* Sample output: etc, all the way up to 10. What I have so far: using System; using System.Collections; using System.Text; namespace The_Last_Survivor { class Program { static void Main(string[] args) { //Declare Variables int NumOfActors = 0; System.DateTime dt = System.DateTime.Now; int interval = 3; ArrayList Ring = new ArrayList(10); //Header Console.Out.WriteLine("Actors\tNumber\tOrder"); //Add Actors for (int x = 1; x < 11; x++) { NumOfActors++; Ring.Insert((x - 1), new Actor(x)); foreach (Actor i in Ring) { Console.Out.WriteLine("{0}\t{1}\t{2}", NumOfActors, i, i.Order(interval, x)); } Console.Out.WriteLine("\n"); } Console.In.Read(); } public class Actor { //Variables protected int Number; //Constructor public Actor(int num) { Number = num; } //Order in circle public string Order(int inter, int num) { //Variable string result = ""; ArrayList myArray = new ArrayList(num); //Filling Array for (int i = 0; i < num; i++) myArray.Add(i + 1); //Formula foreach (int element in myArray) { if (element == inter) { result += String.Format(" {0}", element); myArray.RemoveAt(element); } } return result; } //String override public override string ToString() { return String.Format("{0}", Number); } } } } The part I'm stuck on is getting some math going that does this: Can anyone offer some guidance and/or sample code?

    Read the article

  • Need help with fixing Genetic Algorithm that's not evolving correctly

    - by EnderMB
    I am working on a maze solving application that uses a Genetic Algorithm to evolve a set of genes (within Individuals) to evolve a Population of Individuals that power an Agent through a maze. The majority of the code used appears to be working fine but when the code runs it's not selecting the best Individual's to be in the new Population correctly. When I run the application it outputs the following: Total Fitness: 380.0 - Best Fitness: 11.0 Total Fitness: 406.0 - Best Fitness: 15.0 Total Fitness: 344.0 - Best Fitness: 12.0 Total Fitness: 373.0 - Best Fitness: 11.0 Total Fitness: 415.0 - Best Fitness: 12.0 Total Fitness: 359.0 - Best Fitness: 11.0 Total Fitness: 436.0 - Best Fitness: 13.0 Total Fitness: 390.0 - Best Fitness: 12.0 Total Fitness: 379.0 - Best Fitness: 15.0 Total Fitness: 370.0 - Best Fitness: 11.0 Total Fitness: 361.0 - Best Fitness: 11.0 Total Fitness: 413.0 - Best Fitness: 16.0 As you can clearly see the fitnesses are not improving and neither are the best fitnesses. The main code responsible for this problem is here, and I believe the problem to be within the main method, most likely where the selection methods are called: package GeneticAlgorithm; import GeneticAlgorithm.Individual.Action; import Robot.Robot.Direction; import Maze.Maze; import Robot.Robot; import java.util.ArrayList; import java.util.Random; public class RunGA { protected static ArrayList tmp1, tmp2 = new ArrayList(); // Implementation of Elitism protected static int ELITISM_K = 5; // Population size protected static int POPULATION_SIZE = 50 + ELITISM_K; // Max number of Iterations protected static int MAX_ITERATIONS = 200; // Probability of Mutation protected static double MUTATION_PROB = 0.05; // Probability of Crossover protected static double CROSSOVER_PROB = 0.7; // Instantiate Random object private static Random rand = new Random(); // Instantiate Population of Individuals private Individual[] startPopulation; // Total Fitness of Population private double totalFitness; Robot robot = new Robot(); Maze maze; public void setElitism(int result) { ELITISM_K = result; } public void setPopSize(int result) { POPULATION_SIZE = result + ELITISM_K; } public void setMaxIt(int result) { MAX_ITERATIONS = result; } public void setMutProb(double result) { MUTATION_PROB = result; } public void setCrossoverProb(double result) { CROSSOVER_PROB = result; } /** * Constructor for Population */ public RunGA(Maze maze) { // Create a population of population plus elitism startPopulation = new Individual[POPULATION_SIZE]; // For every individual in population fill with x genes from 0 to 1 for (int i = 0; i < POPULATION_SIZE; i++) { startPopulation[i] = new Individual(); startPopulation[i].randGenes(); } // Evaluate the current population's fitness this.evaluate(maze, startPopulation); } /** * Set Population * @param newPop */ public void setPopulation(Individual[] newPop) { System.arraycopy(newPop, 0, this.startPopulation, 0, POPULATION_SIZE); } /** * Get Population * @return */ public Individual[] getPopulation() { return this.startPopulation; } /** * Evaluate fitness * @return */ public double evaluate(Maze maze, Individual[] newPop) { this.totalFitness = 0.0; ArrayList<Double> fitnesses = new ArrayList<Double>(); for (int i = 0; i < POPULATION_SIZE; i++) { maze = new Maze(8, 8); maze.fillMaze(); fitnesses.add(startPopulation[i].evaluate(maze, newPop)); //this.totalFitness += startPopulation[i].evaluate(maze, newPop); } //totalFitness = (Math.round(totalFitness / POPULATION_SIZE)); StringBuilder sb = new StringBuilder(); for(Double tmp : fitnesses) { sb.append(tmp + ", "); totalFitness += tmp; } // Progress of each Individual //System.out.println(sb.toString()); return this.totalFitness; } /** * Roulette Wheel Selection * @return */ public Individual rouletteWheelSelection() { // Calculate sum of all chromosome fitnesses in population - sum S. double randNum = rand.nextDouble() * this.totalFitness; int i; for (i = 0; i < POPULATION_SIZE && randNum > 0; ++i) { randNum -= startPopulation[i].getFitnessValue(); } return startPopulation[i-1]; } /** * Tournament Selection * @return */ public Individual tournamentSelection() { double randNum = rand.nextDouble() * this.totalFitness; // Get random number of population (add 1 to stop nullpointerexception) int k = rand.nextInt(POPULATION_SIZE) + 1; int i; for (i = 1; i < POPULATION_SIZE && i < k && randNum > 0; ++i) { randNum -= startPopulation[i].getFitnessValue(); } return startPopulation[i-1]; } /** * Finds the best individual * @return */ public Individual findBestIndividual() { int idxMax = 0; double currentMax = 0.0; double currentMin = 1.0; double currentVal; for (int idx = 0; idx < POPULATION_SIZE; ++idx) { currentVal = startPopulation[idx].getFitnessValue(); if (currentMax < currentMin) { currentMax = currentMin = currentVal; idxMax = idx; } if (currentVal > currentMax) { currentMax = currentVal; idxMax = idx; } } // Double check to see if this has the right one //System.out.println(startPopulation[idxMax].getFitnessValue()); // Maximisation return startPopulation[idxMax]; } /** * One Point Crossover * @param firstPerson * @param secondPerson * @return */ public static Individual[] onePointCrossover(Individual firstPerson, Individual secondPerson) { Individual[] newPerson = new Individual[2]; newPerson[0] = new Individual(); newPerson[1] = new Individual(); int size = Individual.SIZE; int randPoint = rand.nextInt(size); int i; for (i = 0; i < randPoint; ++i) { newPerson[0].setGene(i, firstPerson.getGene(i)); newPerson[1].setGene(i, secondPerson.getGene(i)); } for (; i < Individual.SIZE; ++i) { newPerson[0].setGene(i, secondPerson.getGene(i)); newPerson[1].setGene(i, firstPerson.getGene(i)); } return newPerson; } /** * Uniform Crossover * @param firstPerson * @param secondPerson * @return */ public static Individual[] uniformCrossover(Individual firstPerson, Individual secondPerson) { Individual[] newPerson = new Individual[2]; newPerson[0] = new Individual(); newPerson[1] = new Individual(); for(int i = 0; i < Individual.SIZE; ++i) { double r = rand.nextDouble(); if (r > 0.5) { newPerson[0].setGene(i, firstPerson.getGene(i)); newPerson[1].setGene(i, secondPerson.getGene(i)); } else { newPerson[0].setGene(i, secondPerson.getGene(i)); newPerson[1].setGene(i, firstPerson.getGene(i)); } } return newPerson; } public double getTotalFitness() { return totalFitness; } public static void main(String[] args) { // Initialise Environment Maze maze = new Maze(8, 8); maze.fillMaze(); // Instantiate Population //Population pop = new Population(); RunGA pop = new RunGA(maze); // Instantiate Individuals for Population Individual[] newPop = new Individual[POPULATION_SIZE]; // Instantiate two individuals to use for selection Individual[] people = new Individual[2]; Action action = null; Direction direction = null; String result = ""; /*result += "Total Fitness: " + pop.getTotalFitness() + " - Best Fitness: " + pop.findBestIndividual().getFitnessValue();*/ // Print Current Population System.out.println("Total Fitness: " + pop.getTotalFitness() + " - Best Fitness: " + pop.findBestIndividual().getFitnessValue()); // Instantiate counter for selection int count; for (int i = 0; i < MAX_ITERATIONS; i++) { count = 0; // Elitism for (int j = 0; j < ELITISM_K; ++j) { // This one has the best fitness newPop[count] = pop.findBestIndividual(); count++; } // Build New Population (Population size = Steps (28)) while (count < POPULATION_SIZE) { // Roulette Wheel Selection people[0] = pop.rouletteWheelSelection(); people[1] = pop.rouletteWheelSelection(); // Tournament Selection //people[0] = pop.tournamentSelection(); //people[1] = pop.tournamentSelection(); // Crossover if (rand.nextDouble() < CROSSOVER_PROB) { // One Point Crossover //people = onePointCrossover(people[0], people[1]); // Uniform Crossover people = uniformCrossover(people[0], people[1]); } // Mutation if (rand.nextDouble() < MUTATION_PROB) { people[0].mutate(); } if (rand.nextDouble() < MUTATION_PROB) { people[1].mutate(); } // Add to New Population newPop[count] = people[0]; newPop[count+1] = people[1]; count += 2; } // Make new population the current population pop.setPopulation(newPop); // Re-evaluate the current population //pop.evaluate(); pop.evaluate(maze, newPop); // Print results to screen System.out.println("Total Fitness: " + pop.totalFitness + " - Best Fitness: " + pop.findBestIndividual().getFitnessValue()); //result += "\nTotal Fitness: " + pop.totalFitness + " - Best Fitness: " + pop.findBestIndividual().getFitnessValue(); } // Best Individual Individual bestIndiv = pop.findBestIndividual(); //return result; } } I have uploaded the full project to RapidShare if you require the extra files, although if needed I can add the code to them here. This problem has been depressing me for days now and if you guys can help me I will forever be in your debt.

    Read the article

  • loading child swf as3

    - by RichW
    Hi, I've been given an fla to make some changes too. Basically its a fairly long timeline animation with sound. So far I've successfully added a few button functions for sound etc.. but one has got me stumped. One of the buttons needs to load a child swf. I'm using the code below but I'm recieving an error - 'Error #1009: Cannot access a property or method of a null object reference'. I believe this may be refferring to an object that isn't set yet but I have no idea which one it is: Code: var mcExt:MovieClip = new MovieClip(); var ldr:Loader = new Loader(); ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded); ldr.load(new URLRequest("Downloads.swf")); function swfLoaded(e:Event):void { mcExt = MovieClip(ldr.contentLoaderInfo.content); ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfLoaded); mcExt.x = 50; mcExt.y = 50; addChild(mcExt); } Any help on what is going wrong would be greatly appreciated! Thanks

    Read the article

  • Csharp component which generates fragmens with highlights for diffs for 2 strings

    - by MicMit
    I need C# equivalent ( ideally open source ) which is similar to Delphi DLL. I am currently using the wrapper ( C# syntax is provided , but it is a call from a different language ) zdiff( string ref str1, string ref str2, int range , int trim ) it calls inside str1 = GetHiDiff(@str1,1,trim) str2 = GetHiDiff(@str1,2,trim) where function GetHiDiff(s:pchar; sIndex:integer; wtrim:integer): pchar; stdcall; What it does it returns a left fragment html of str1 and a right html fragment of str2 with diffs highlighted as strings are passed by reference. Range parameter determines the size of html fragment. Not sure what trim 0 does.

    Read the article

  • How to make an image randomizer from gallery directory?

    - by zebraman
    Hello, I would like to make a image randomizer/slider that randomly displays an image from a designated gallery and has controls to view the next/previous image. I have seen this done where images must be coded into arrays, but I would like to be able to just drop images into a /img/gallery directory and have the randomizer/slider pull random images from that directory. Could anyone offer some guidance on how to do this, possibly using jQuery, or refer me to a tutorial that does something like this?

    Read the article

  • Loading Separate Pages With Animation in JQTouch

    - by Donovan Keith
    I'm trying to convert a database-driven multiple-choice-style study website (written in JSP) into an iPhone app using JQTouch. If I load all of the Q&A's into their own divs in the same file I can easily link and animate between them using links to hashtags, like: a class="button" href="#question22" Unfortunately this isn't practical. The logic of the website as it currently works requires calls to a number of dynamically generated pages; I can't include every question in its own div in the same flat file. How would I go about dynamically (pre)loading the next question (a JSP page like AskQuestion.jsp?questionId=Kzhctand ) then serving that up within the app after the user presses a button? Thanks for any help you might offer. -Donovan

    Read the article

  • Nhibernate Criteria Ignore Child Collection

    - by CocoB
    I have a simple one to many association in my model. The parent class has a collection of children. In the mapping files, the association is a one to many, eager-loaded, using fetchmode.join. This works fine, but how can I write a criteria query but NOT trigger the loading of the child collection? In other words, I want to query the parent and not have it generate the join in the resulting sql. I tried setting the fetch mode to lazy, but in that case Nhibernate generates two separate queries. I don't want the table for child queried at all.

    Read the article

  • Google Contacts and Mac OS X Address Book

    - by dyve
    The sync that Google offers for Google Contacts < Mac OS X Address Book is seriously flawed. It doesn't sync automatically, it doesn't sync all contacts, it doesn't sync all fields. See here for a list of warnings and issues. Is there a better way to sync Google Contacts to Mac Address Book bidirectionally? Preferably free, and preferably without adding extra software. I have tried to do it through Plaxo (which has an excellent Mac Address Book sync, albeit through an extra software install), but Plaxo doesn't handle Google Sync well (no updates). UPDATE: For the new Mac OS X Snow Leopard this shouldn't be an issue. This question is looking for a Leopard answer.

    Read the article

  • Unable to understand a line in Google CodePreview's README

    - by Masi
    The README is in Google's codepreview which uses Google-appengine. To run the app locally (e.g. for testing), download the Google App Engine SDK from http://code.google.com/appengine/downloads.html. You can then run the server using make serve I run make serve in my terminal after moving Google-appengine.app to my Application -folder in OS X Leopard. I get make: *** No rule to make target `serve'. Stop. How can you run the make serve to run the server for Google AppEngine?

    Read the article

  • Sync iPod Touch's calendar to Google Calendar

    - by Masi
    How can you sync your calendar of iPod Touch to Google Calendar? I have rarely internet connection at my iPod Touch. It has my calendar as "offline". I cannot share it with my friends. I would like to use "Google Calendar" in a offline mode such that I can more easily sync my calendar, every time my iPod is online. So problems are to sync iPod Touch's calendar to Google Calendar to be able to use Google calendar offline in iPod touch to put iPod sync Gcal every time it observes a network

    Read the article

  • Extended Blogs – Now cross posting to Sqlblog.com

    - by extended_events
    Thanks to some help from SQL MVP Adam Mechanic, the Extended Events blog is now being cross posted on SQLblog.com. Except for the first two posts (welcome message & Reading event data 101), these blogs will be identical so read whichever site you prefer, either the SQLblog version, or the original MSDN blog. - Mike Share this post: email it! | bookmark it! | digg it! | reddit! | kick it! | live it!

    Read the article

  • virtaul function

    - by hitech
    class a { virtual void foo(void) ; }; class b : public a { public: virtual void foo(void) { cout<< "class b"; } }; int main ( ) { class a *b_ptr = new b ; b_ptr-foo(); } please guide me why the b_ptr-foo() will not call the foo() function of the class b?

    Read the article

  • jqueryUI: Drag element from dialog and drop onto main page?

    - by Seth Petry-Johnson
    I am trying to create a drag and drop system consisting of a workspace and a "palette". The workspace currently consists of re-orderable list items, and I want the palette to be a floating window from which I can drag items and add them to a specific position on the workspace. I am currently using the jqueryUI "sortable" plugin for the workspace and the jqueryUI "dialog" plugin for the palette. However, I cannot drag something out of the dialog and on to the main page. When I try, the item being dragged disappears as it crosses the boundary of the dialog (which makes sense). What can I change so that items will remain visible as I drag them out of the palette and allow me to drop them onto the main workspace? Alternatively, are there any jquery plugins that offer this sort of drag-n-drop palette as a primary feature?

    Read the article

  • T SQL Count question

    - by johniom
    I'm making a report at work which looks at 5 columns which all contain v1aa, v1ab and v1ac. What I want to do is count up all the v1aa in the 5 columns and show the results (and the same for v1ab and v1ac) An example of how I like it to be displayed as is as follows :- Amber = 3 (v1aa code) Blue = 2 (v1ab code) Red = 1 (v1ac code) Could anyone give me any tips how to get me started?

    Read the article

  • Adding metadata attributes to MySQL table

    - by Jack
    I would like to add custom attributes to a MySQL table which I can read via php. These attributes are not to interfere with the table itself - they are primarily accessed by php code during code generation time and these attributes HAVE to reside in the DB itself. Something similar in concept to .NET reflection. Does MySQL support anything like this? Thanks.

    Read the article

  • slicing behaviour question of a list of lists

    - by Lex
    I got a function like def f(): ... ... return [list1, list2] this returns a list of lists [[list1.item1,list1.item2,...],[list2.item1,list2.item2,...]] now when I do the following: for i in range(0,2):print f()[i][0:10] it works and print the lists sliced but if i do print f()[0:2][0:10] then it prints the lists ignoring the [0:10] slicing. Is there any way to make the second form work or do I have to loop every time to get the desired result?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >