protected override void Setup(Mapper.Context context)
            {
                this.context = context;
                Configuration conf = context.GetConfiguration();

                depth  = conf.GetInt(Pentomino.Depth, PentDepth);
                width  = conf.GetInt(Pentomino.Width, PentWidth);
                height = conf.GetInt(Pentomino.Height, PentHeight);
                pent   = (Pentomino)ReflectionUtils.NewInstance(conf.GetClass(Pentomino.Class, typeof(
                                                                                  OneSidedPentomino)), conf);
                pent.Initialize(width, height);
                pent.SetPrinter(new DistributedPentomino.PentMap.SolutionCatcher(this));
            }
Exemple #2
0
        /// <summary>Solve the 6x10 pentomino puzzle.</summary>
        public static void Main(string[] args)
        {
            int       width  = 6;
            int       height = 10;
            Pentomino model  = new Pentomino(width, height);
            IList     splits = model.GetSplits(2);

            for (IEnumerator splitItr = splits.GetEnumerator(); splitItr.HasNext();)
            {
                int[] choices = (int[])splitItr.Next();
                System.Console.Out.Write("split:");
                for (int i = 0; i < choices.Length; ++i)
                {
                    System.Console.Out.Write(" " + choices[i]);
                }
                System.Console.Out.WriteLine();
                System.Console.Out.WriteLine(model.Solve(choices) + " solutions found.");
            }
        }
                public virtual void Solution(IList <IList <Pentomino.ColumnName> > answer)
                {
                    string board = Pentomino.StringifySolution(this._enclosing.width, this._enclosing
                                                               .height, answer);

                    try
                    {
                        this._enclosing.context.Write(this._enclosing.prefixString, new Text("\n" + board
                                                                                             ));
                        this._enclosing.context.GetCounter(this._enclosing.pent.GetCategory(answer)).Increment
                            (1);
                    }
                    catch (IOException e)
                    {
                        System.Console.Error.WriteLine(StringUtils.StringifyException(e));
                    }
                    catch (Exception ie)
                    {
                        System.Console.Error.WriteLine(StringUtils.StringifyException(ie));
                    }
                }
        /// <summary>
        /// Create the input file with all of the possible combinations of the
        /// given depth.
        /// </summary>
        /// <param name="fs">the filesystem to write into</param>
        /// <param name="dir">the directory to write the input file into</param>
        /// <param name="pent">the puzzle</param>
        /// <param name="depth">the depth to explore when generating prefixes</param>
        /// <exception cref="System.IO.IOException"/>
        private static long CreateInputDirectory(FileSystem fs, Path dir, Pentomino pent,
                                                 int depth)
        {
            fs.Mkdirs(dir);
            IList <int[]> splits = pent.GetSplits(depth);
            Path          input  = new Path(dir, "part1");
            PrintWriter   file   = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream
                                                                              (fs.Create(input), 64 * 1024), Charsets.Utf8));

            foreach (int[] prefix in splits)
            {
                for (int i = 0; i < prefix.Length; ++i)
                {
                    if (i != 0)
                    {
                        file.Write(',');
                    }
                    file.Write(prefix[i]);
                }
                file.Write('\n');
            }
            file.Close();
            return(fs.GetFileStatus(input).GetLen());
        }
        /// <exception cref="System.Exception"/>
        public virtual int Run(string[] args)
        {
            Configuration conf = GetConf();

            if (args.Length == 0)
            {
                System.Console.Out.WriteLine("Usage: pentomino <output> [-depth #] [-height #] [-width #]"
                                             );
                ToolRunner.PrintGenericCommandUsage(System.Console.Out);
                return(2);
            }
            // check for passed parameters, otherwise use defaults
            int width  = conf.GetInt(Pentomino.Width, PentWidth);
            int height = conf.GetInt(Pentomino.Height, PentHeight);
            int depth  = conf.GetInt(Pentomino.Depth, PentDepth);

            for (int i = 0; i < args.Length; i++)
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(args[i], "-depth"))
                {
                    depth = System.Convert.ToInt32(args[++i].Trim());
                }
                else
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(args[i], "-height"))
                    {
                        height = System.Convert.ToInt32(args[++i].Trim());
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(args[i], "-width"))
                        {
                            width = System.Convert.ToInt32(args[++i].Trim());
                        }
                    }
                }
            }
            // now set the values within conf for M/R tasks to read, this
            // will ensure values are set preventing MAPREDUCE-4678
            conf.SetInt(Pentomino.Width, width);
            conf.SetInt(Pentomino.Height, height);
            conf.SetInt(Pentomino.Depth, depth);
            Type pentClass = conf.GetClass <Pentomino>(Pentomino.Class, typeof(OneSidedPentomino
                                                                               ));
            int        numMaps = conf.GetInt(MRJobConfig.NumMaps, DefaultMaps);
            Path       output  = new Path(args[0]);
            Path       input   = new Path(output + "_input");
            FileSystem fileSys = FileSystem.Get(conf);

            try
            {
                Job job = Job.GetInstance(conf);
                FileInputFormat.SetInputPaths(job, input);
                FileOutputFormat.SetOutputPath(job, output);
                job.SetJarByClass(typeof(DistributedPentomino.PentMap));
                job.SetJobName("dancingElephant");
                Pentomino pent = ReflectionUtils.NewInstance(pentClass, conf);
                pent.Initialize(width, height);
                long inputSize = CreateInputDirectory(fileSys, input, pent, depth);
                // for forcing the number of maps
                FileInputFormat.SetMaxInputSplitSize(job, (inputSize / numMaps));
                // the keys are the prefix strings
                job.SetOutputKeyClass(typeof(Text));
                // the values are puzzle solutions
                job.SetOutputValueClass(typeof(Text));
                job.SetMapperClass(typeof(DistributedPentomino.PentMap));
                job.SetReducerClass(typeof(Reducer));
                job.SetNumReduceTasks(1);
                return(job.WaitForCompletion(true) ? 0 : 1);
            }
            finally
            {
                fileSys.Delete(input, true);
            }
        }