Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            var modelName = "FR001";
            //var modelName = "FA001";

            var srcModel = new TModel($"Data/Problems/{modelName}_src.mdl");
            var tgtModel = new TModel($"Data/Problems/{modelName}_tgt.mdl");

            //var srcModel = TModel.MakeEmpty(tgtModel.Name, tgtModel.R);
            //var tgtModel = TModel.MakeEmpty(srcModel.Name, srcModel.R);

            void TestStrategy(IStrategy strategy, bool saveTrace = false)
            {
                Console.WriteLine($"=== {strategy.Name}");

                try
                {
                    var trace = strategy.MakeTrace(srcModel, tgtModel);
                    if (saveTrace)
                    {
                        File.WriteAllBytes("trace.nbt", TraceSerializer.Serialize(trace));
                    }
                    if (trace == null)
                    {
                        Console.WriteLine("  empty trace");
                        return;
                    }
                    var state  = new TState(srcModel);
                    var reader = new TCommandsReader(trace);
                    while (!reader.AtEnd())
                    {
                        state.Step(reader);
                    }

                    Console.WriteLine(state.HasValidFinalState(tgtModel));
                    Console.WriteLine(state.Energy);
                }
                catch (Exception e)
                {
                    Console.Write($"BROKENL {e}");
                }
            }

            //TestStrategy(new DumpCubeStrategy());
            //TestStrategy(new DumpCubeStrategy(), true);
            TestStrategy(new TTraceReaderStrategy($"data/DefaultTraces"));
            TestStrategy(new BfsStrategy(), true);
        }
Exemplo n.º 2
0
        private int ProcessTask(Task task)
        {
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);

            var traceFile = $"{BestStrategiesDirectory}/{task.Name}.nbt";

            if (!AllowedPrefixes.Any(x => task.Name.StartsWith(x)))
            {
                writer.WriteLine($"Model {task.Name} is not allowed, ignoring");
                //File.Copy($"{DefaultTracesDirectory}/{Path.GetFileName(traceFile)}", traceFile, true);
            }
            else
            {
                IStrategy[] allowedStrategies = Strategies;

                writer.WriteLine($"{task.Name}");
                var(best, _) = RunStrategy(task, BaselineStrategy, writer);
                if (best != null)
                {
                    StrategyStats[BaselineStrategy.Name] += best.Value;
                }

                foreach (var strategy in allowedStrategies)
                {
                    var(energy, commands) = RunStrategy(task, strategy, writer);

                    if (energy != null)
                    {
                        lock (Lock)
                        {
                            StrategyStats[strategy.Name] += energy.Value;
                        }
                    }

                    if ((energy != null) && ((best == null) || (energy < best)))
                    {
                        writer.WriteLine("  NEW BEST!!!");
                        best = energy;

                        File.Delete(traceFile);
                        File.Delete($"{traceFile}.tmp");

                        using (var f = File.OpenWrite($"{traceFile}.tmp"))
                        {
                            f.Write(TraceSerializer.Serialize(commands));
                        }

                        File.Move($"{traceFile}.tmp", traceFile);

                        using (var f = File.OpenWrite($"{traceFile}.winner.txt"))
                        {
                            f.Write(Encoding.UTF8.GetBytes($"Strategy: {strategy.Name}"));
                        }
                    }
                }
            }

            writer.Flush();

            stream.Seek(0, SeekOrigin.Begin);
            StreamReader reader = new StreamReader(stream);

            lock (Lock) {
                Console.Write(reader.ReadToEnd());
            }

            return(0);
        }