예제 #1
0
        private static void Main(string[] args)
        {
            /*
            if (args.Length != 1)
                exit("Usage: Simplecalc.exe filename");
            */
            using (StreamReader sr = new StreamReader(File.Open("test", FileMode.Open)))
            {
                // Read source
                Lexer lexer = new Lexer(sr);

                // Parse source
                Parser parser = new Parser(lexer);
                Start ast = null;

                try
                {
                    ast = parser.Parse();
                }
                catch (Exception ex)
                {
                    exit(ex.ToString());
                }

                // Print tree
                SimplePrinter printer = new SimplePrinter(true, ConsoleColor.White, ConsoleColor.Gray, ConsoleColor.Red, ConsoleColor.Blue);
                ast.Apply(printer);
            }

            exit("Done");
        }
예제 #2
0
 public SimpleCommunicator(EventHandler onOpen, EventHandler onClose, SimplePrinter printer)
 {
     openSocket += onOpen;
     closeSocket += onClose;
     print = printer;
     simpleExperiment = new SimpleExperiment();
     simpleExperiment.setCommunicator(this);
 }
예제 #3
0
        public static InteractivePrinter CreateSimplePrinter(string title, Func <object, string> formatter)
        {
            if (!AllowInteractivePrinting)
            {
                return(new NullPrinter());
            }
            InteractivePrinter printer = new SimplePrinter()
            {
                Header    = title,
                Formatter = formatter
            };

            printer.Start(title);

            return(printer);
        }
예제 #4
0
        private static void Main(string[] args)
        {
            // Interface Segregation principle suggests that an interface should
            // not contain methods that may not be needed for it's inheritors.

            // In this example we see an interface that breaks this principle IMachine
            // which contains methods Print, Scan and Fax. When we implement this interface
            // from a class MultiPurposeMachine it makes sense, however if we decide to implement
            // the same class from a SimplePrinter it does not make sense because SimplePrinter can not
            // perform Scan and Fax actions.

            // Interface Segregation suggests that we should segregate interfaces, make them smaller
            // and use inheritance if we need to create a composition of different interfaces.

            IMachine multiMachine = new MultiPurposeMachine();

            multiMachine.Print(); // this machine is capable of doing it.
            multiMachine.Fax();   // this machine is capable of doing it.
            multiMachine.Scan();  // this machine is capable of doing it.

            IMachine simplePrinter = new SimplePrinter();

            simplePrinter.Print(); // this machine is capable of doing it.
            simplePrinter
            .Scan();               // this was supposed to be a simple printer with print ability. This method does not make sense.
            simplePrinter
            .Fax();                // this was supposed to be a simple printer with print ability. This method does not make sense.

            // Below classes implement segregated interfaces and now we see they don't have
            // irrelevant methods implemented in them. Even better, we are delegating
            // print and scan to their respective classes so we don't repeat ourselves
            // in the AdvancedPrinter class.

            var advancedPrinter = new AdvancedPrinter(new Printer(), new Scanner());

            advancedPrinter.Print(); // can do
            advancedPrinter.Scan();  // can do
        }
예제 #5
0
        public static InteractivePrinter CreateSimplePrinter(string title, Func<object, string> formatter)
        {
            if (!AllowInteractivePrinting)
                return new NullPrinter();
            InteractivePrinter printer = new SimplePrinter()
            {
                Header = title,
                Formatter = formatter
            };

            printer.Start(title);

            return printer;
        }
예제 #6
0
        Dictionary <long, KeyValuePair <double[], List <double> > > serialCallCommunicatorWithIDs(List <long> genomeIDs)
        {
            waitingOnEvaluation = true;

            callCommunicatorWithIDs(genomeIDs, (jsonString) =>
            {
                lastReturnedObject = jsonString;

                //get our genome behaviors!
                //genomeBehaviors = new Dictionary<long, KeyValuePair<double[], List<double>>>();//new Dictionary<long, List<double>>();

                //fitnessDictionary = new Dictionary<long, double>();
                genomeBehaviors = new Dictionary <long, KeyValuePair <double[], List <double> > >();//new Dictionary<long, List<double>>();


                if (jsonString.Args.Length > 0)
                {
                    //we try parsing our object into a dictionary like object
                    try
                    {
                        var parsedJson = JObject.Parse((string)jsonString.Args[0]);
                        int objCount   = 3;

                        //for each genome, we need to build our double list
                        foreach (var gID in genomeIDs)
                        {
                            if (genomeBehaviors.ContainsKey(gID))
                            {
                                continue;
                            }

                            List <double> doubleBehavior = new List <double>();

                            double[] accumObjectives = new double[objCount];
                            for (int i = 0; i < objCount; i++)
                            {
                                accumObjectives[i] = 0.0;
                            }

                            //LINQ way to do it
                            //    parsedJson[gID.ToString()].SelectMany(
                            //    xyBehavior => new List<double>(){ xyBehavior["x"].Value<double>(), xyBehavior["y"].Value<double>()}
                            //).ToList<double>();

                            var genomeEntry = parsedJson[gID.ToString()];

                            double val;
                            if (!double.TryParse(genomeEntry["fitness"].ToString(), out val))
                            {
                                val = EvolutionAlgorithm.MIN_GENOME_FITNESS;
                            }

                            fitnessDictionary.Add(gID, Math.Max(EvolutionAlgorithm.MIN_GENOME_FITNESS, val));

                            int ix = 0;

                            foreach (var objective in genomeEntry["objectives"])
                            {
                                if (double.TryParse(objective.ToString(), out val))
                                {
                                    accumObjectives[ix] = val;
                                }
                                ix++;
                            }



                            foreach (var behavior in genomeEntry["behavior"])
                            {
                                if (double.TryParse(behavior.ToString(), out val))
                                {
                                    doubleBehavior.Add(val);
                                }
                                else
                                {
                                    if (double.TryParse(behavior["x"].ToString(), out val))
                                    {
                                        doubleBehavior.Add(val);
                                    }

                                    if (double.TryParse(behavior["y"].ToString(), out val))
                                    {
                                        doubleBehavior.Add(val);
                                    }
                                }
                            }

                            if (genomeEntry["secondBehavior"] != null)
                            {
                                List <double> secondBehavior = new List <double>();

                                foreach (var behavior in genomeEntry["secondBehavior"])
                                {
                                    if (double.TryParse(behavior.ToString(), out val))
                                    {
                                        secondBehavior.Add(val);
                                    }
                                    else
                                    {
                                        if (double.TryParse(behavior["x"].ToString(), out val))
                                        {
                                            secondBehavior.Add(val);
                                        }

                                        if (double.TryParse(behavior["y"].ToString(), out val))
                                        {
                                            secondBehavior.Add(val);
                                        }
                                    }
                                }

                                genomeSecondaryBehaviors.Add(gID, secondBehavior);
                            }



                            //now we have our double behavior, add to our behavior dictionary
                            //we assume no duplicated for simplicity
                            genomeBehaviors.Add(gID, new KeyValuePair <double[], List <double> >(accumObjectives, doubleBehavior));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine(e.StackTrace);
                        throw e;
                    }
                }

                //when finished, stop the eval procedure, error or not (which we don't yet check for -- or timeout!)
                waitingOnEvaluation = false;
            });

            int      timeout  = 400000;
            bool     timedOut = false;
            DateTime now      = DateTime.Now;

            //then we wait for a return
            //this is very very very dangerous without error checking
            while (!timedOut && waitingOnEvaluation)
            {
                if ((DateTime.Now - now).TotalMilliseconds > timeout)
                {
                    timedOut = true;
                    simpleCom.printString("TIMED OUT EVALUATION, RETRYING");
                }
            }

            //skip over the rest, just send an empty object
            if (timedOut)
            {
                return(new Dictionary <long, KeyValuePair <double[], List <double> > >());
            }

            try
            {
                //we have some returned evaluation, go ahead and print that poop.
                simpleCom.printString("Finished serial evaluation of: " + SimplePrinter.listToString <long>(genomeIDs));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(genomeBehaviors);
        }