コード例 #1
0
ファイル: Solver.cs プロジェクト: baguio/SSC-AI
        public List<State> Solve (List<Measure> measures, ICostFactory cost_factory, OnProgressDelegate on_progress) {
            List<NodeCollection> node_collections = NodeCollection.CalculateNodes(measures);
            List<List<NodeCollection>> sections = NodeCollection.CalculateSections(node_collections, m_DesiredSectionSize);
            State initial = SolverHelper.GenerateInitialNode(measures[0].beats[0].notes.Count == 10, cost_factory);

            OnProgressArg on_progress_arg = new OnProgressArg();
            on_progress_arg.second_max = node_collections[node_collections.Count - 1].items[0].second;
            on_progress_arg.depth_max = node_collections.Count - 1;
            on_progress_arg.depth_cur = 0;
            foreach (List<NodeCollection> path in sections) {
                State nxt = null;
                while (nxt == null) {
                    Calculate(path, initial, cost_factory, on_progress, on_progress_arg, ref nxt);

                    on_progress(on_progress_arg);
                    if (!on_progress_arg.carry_on) {
                        return SolverHelper.GeneratePlay(nxt == null ? initial : nxt);
                    }
                }
                on_progress_arg.prv_iter_opened_total = on_progress_arg.opened_total;
                on_progress_arg.prv_iter_discarded = on_progress_arg.discarded;

                initial = nxt;
                on_progress_arg.depth_cur += path.Count;
            }
            return SolverHelper.GeneratePlay(initial);
        }
コード例 #2
0
ファイル: Solver.cs プロジェクト: baguio/SSC-AI
        public List<State> Solve (List<Measure> measures, ICostFactory cost_factory, OnProgressDelegate on_progress) {
            List<NodeCollection> node_collections = NodeCollection.CalculateNodes(measures);
            List<List<NodeCollection>> sections = NodeCollection.CalculateSections(node_collections, m_DesiredSectionSize);
            State initial = SolverHelper.GenerateInitialNode(measures[0].beats[0].notes.Count == 10, cost_factory);

            OnProgressArg on_progress_arg = new OnProgressArg();
            on_progress_arg.second_max = node_collections[node_collections.Count - 1].items[0].second;
            on_progress_arg.depth_max = node_collections.Count - 1;
            foreach (List<NodeCollection> path in sections) {
                State nxt = null;
                float storage_ratio = (float)path.Count / 30.0f;
                if (storage_ratio < 1.0f) {
                    storage_ratio = 1.0f;
                }
                int storage_limit = (int)((float)m_ItemLimit * storage_ratio);
                while (nxt == null) {
                    if (storage_limit > 100000) { //Arbitrary limit
                        throw new Exception("Storage limit reached without a solution");
                    }
                    nxt = Calculate(initial, path, storage_limit, cost_factory, on_progress, on_progress_arg);
                    storage_limit += m_ItemLimit;

                    on_progress(on_progress_arg);
                    if (!on_progress_arg.carry_on) {
                        return SolverHelper.GeneratePlay(nxt == null ? initial : nxt);
                    }
                }
                on_progress_arg.prv_iter_opened_total = on_progress_arg.opened_total;
                on_progress_arg.prv_iter_discarded = on_progress_arg.discarded;

                initial = nxt;
            }
            return SolverHelper.GeneratePlay(initial);
        }
コード例 #3
0
        public IEnumerator WaitForAsyncOperation(AsyncOperation asyncOperation, OnProgressDelegate onProgress = null)
        {
            var lastProgress = (onProgress != null) ? asyncOperation.progress : float.MaxValue;

            while (!asyncOperation.isDone)
            {
                yield return(nextFrameResumable);

                if (asyncOperation.progress > lastProgress)
                {
                    lastProgress = asyncOperation.progress;
                    onProgress(lastProgress);
                }
            }
        }
コード例 #4
0
ファイル: Solver.cs プロジェクト: baguio/SSC-AI
        private void Calculate (List<NodeCollection> node_collections, State cur, ICostFactory cost_factory, OnProgressDelegate on_progress, OnProgressArg on_progress_arg, ref State best) {
            on_progress_arg.progress_cur = (int)(
                (
                    (float)(cur.distance_from_start + 1) /
                    (float)(on_progress_arg.depth_max)
                ) * 100.0f
            );
            on_progress(on_progress_arg);

            if (cur.distance_from_start == node_collections.Count - 1) {
                //This is a completed play
                if (best == null) {
                    best = cur;
                } else if (cur.cost.GetTotalCost() < best.cost.GetTotalCost()) {
                    best = cur;
                }
                return;
            }
            if (!on_progress_arg.carry_on) {
                return;
            }
            int index = cur.distance_from_start + 1;
            NodeCollection collection = node_collections[index];
            Beat beat = collection.beat;

            List<State> nxt_list = new List<State>();
            foreach (Node node in collection.items) {
                List<Analyzer.State.Limb[]> neighbours = new List<Analyzer.State.Limb[]>();
                ArcCalculator.Calculate(cur, node, neighbours);
                foreach (Analyzer.State.Limb[] n in neighbours) {
                    State nxt = ArcCalculator.TransitionTo(cur, n, collection.beat.second, index, beat, cost_factory);
                    if (nxt == null) {
                        continue;
                    }
                    if (best == null || nxt.cost.GetTotalCost() < best.cost.GetTotalCost()) {
                        nxt_list.Add(nxt);
                    }
                }
            }
            nxt_list.Sort((State a, State b) => {
                return a.cost.GetTotalCost().CompareTo(b.cost.GetTotalCost());
            });
            foreach (State n in nxt_list) {
                Calculate(node_collections, n, cost_factory, on_progress, on_progress_arg, ref best);
            }
        }
コード例 #5
0
ファイル: frmAdv2Viewer.cs プロジェクト: alexcherney/tangra3
        internal bool SaveAsAviFile(string fileName, int firstFrame, int lastFrame, AdvToAviConverter converter, bool tryCodec, bool isCalibrationStream, double msPerFrame, double addedGamma, OnProgressDelegate progressCallback)
        {
            IAviSaver saver = AdvToAviConverterFactory.CreateConverter(converter);

            saver.CloseAviFile();
            if (!saver.StartNewAviFile(fileName, (int)m_AdvFile.Width, (int)m_AdvFile.Height, 8, 25, tryCodec))
            {
                progressCallback(100, 0);
                return(false);
            }
            try
            {
                int           aviFrameNo               = 0;
                AdvIndexEntry firstFrameIdx            = isCalibrationStream ? m_AdvFile.CalibrationIndex[firstFrame] : m_AdvFile.MainIndex[firstFrame];
                double        ticksToMSFactor          = 1000.0 / (isCalibrationStream ? m_AdvFile.CalibrationSteamInfo.ClockFrequency : m_AdvFile.MainSteamInfo.ClockFrequency);
                double        startingTimeMilliseconds = firstFrameIdx.ElapsedTicks * ticksToMSFactor;

                if (m_AdvFile.MainIndex[lastFrame].ElapsedTicks != 0)
                {
                    // Sampling can be done as we have sufficient timing information
                }
                else
                {
                    InvokeMessageBox(
                        "There is insufficient timing information in this file to convert it to AVI. This could be caused by an old file format or trying to make an AVI from a single frame.",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);

                    return(false);
                }

                if (InvokeMessageBox(
                        "Please note that the AVI export is doing resampling of the original video which will typically cause frames to duplicated and/or dropped.\r\n\r\nThis export function is meant to be used for video streaming (i.e. sharing the video for viewing on the Internet) and should not be used to convert the video to another format for measuring in another software. If you want to measure the video in another software either measure it directly as ADV/AAV file (if supported) or export it to a FITS file sequence from the main file menu and measure the FITS images.\r\n\r\nDo you wish to continue?",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return(false);
                }

                progressCallback(5, 0);

                for (int i = firstFrame; i <= lastFrame; i++)
                {
                    AdvIndexEntry frame = isCalibrationStream ? m_AdvFile.CalibrationIndex[i] : m_AdvFile.MainIndex[i];

                    AdvFrameInfo frameInfo = null;
                    uint[]       pixels    = null;

                    if (isCalibrationStream)
                    {
                        pixels = m_AdvFile.GetCalibrationFramePixels((uint)i, out frameInfo);
                    }
                    else
                    {
                        pixels = m_AdvFile.GetMainFramePixels((uint)i, out frameInfo);
                    }

                    using (Pixelmap pixmap = CreatePixelmap(pixels))
                    {
                        int lastRepeatedAviFrameNo = 0;

                        if (frame.ElapsedTicks != 0)
                        {
                            lastRepeatedAviFrameNo = (int)Math.Round((frame.ElapsedTicks * ticksToMSFactor - startingTimeMilliseconds) / msPerFrame);
                        }

                        while (aviFrameNo < lastRepeatedAviFrameNo)
                        {
                            if (!saver.AddAviVideoFrame(pixmap, addedGamma, m_AdvFile.MaxPixelValue))
                            {
                                progressCallback(100, 0);
                                return(false);
                            }
                            aviFrameNo++;
                        }
                    }

                    int percDone = (int)Math.Min(90, 90 * (i - firstFrame) * 1.0 / (lastFrame - firstFrame + 1));
                    progressCallback(5 + percDone, 0);
                }
            }
            finally
            {
                saver.CloseAviFile();
                progressCallback(100, 0);
            }

            return(false);
        }
コード例 #6
0
ファイル: frmAdv2Viewer.cs プロジェクト: alexcherney/tangra3
        internal void ExportStatusSectionToCSV(string fileName, int firstFrame, int lastFrame, bool isCalibrationFrame, OnProgressDelegate progressCallback)
        {
            progressCallback(5, 0);

            bool headerAppended = false;

            using (FileStream fsOutput = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write))
                using (TextWriter writer = new StreamWriter(fsOutput))
                {
                    for (int i = firstFrame; i <= lastFrame; i++)
                    {
                        AdvFrameInfo frameInfo;
                        if (isCalibrationFrame)
                        {
                            m_AdvFile.GetCalibrationFramePixels((uint)i, out frameInfo);
                        }
                        else
                        {
                            m_AdvFile.GetMainFramePixels((uint)i, out frameInfo);
                        }

                        string headerRow;
                        string nextRow = StatusDataToCsvRow(frameInfo, i, out headerRow);
                        if (!headerAppended)
                        {
                            writer.WriteLine(headerRow);
                            headerAppended = true;
                        }
                        writer.WriteLine(nextRow);

                        int percDone = (int)Math.Min(90, 90 * (i - firstFrame) * 1.0 / (lastFrame - firstFrame + 1));
                        progressCallback(5 + percDone, 0);
                    }

                    progressCallback(95, 0);

                    writer.Flush();
                }
        }
コード例 #7
0
 public IEnumerator WaitForAsyncOperation(AsyncOperation asyncOperation, OnProgressDelegate onProgress = null)
 {
     return(context.WaitForAsyncOperation(asyncOperation, onProgress));
 }
コード例 #8
0
 public abstract bool Copy(string dest, string source, bool replace = true, bool evenReadOnly  = false, bool attributes = false, OnProgressDelegate progress = null);
コード例 #9
0
        //todo: add or remove argument: attributes
        //todo: add or remove argument: progress
        public override bool Copy(string dest, string source, bool replace = true, bool evenReadOnly = false, bool attributes = false, OnProgressDelegate progress = null)
        {
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (FileExist(dest))
            {
                var fileInfo = new FileInfo(dest);

                if (replace)
                {
                    if (fileInfo.IsReadOnly)
                    {
                        if (evenReadOnly)
                        {
                            Delete(dest, false, true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        Delete(dest, false, true);
                    }
                }
                else
                {
                    return(false);
                }
            }
            File.Copy(source, dest);
            return(true);
        }
コード例 #10
0
ファイル: frmAdv2Viewer.cs プロジェクト: hpavlov/tangra3
        internal void ExportStatusSectionToCSV(string fileName, int firstFrame, int lastFrame, bool isCalibrationFrame, OnProgressDelegate progressCallback)
        {
            progressCallback(5, 0);

            bool headerAppended = false;

            using (FileStream fsOutput = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write))
            using (TextWriter writer = new StreamWriter(fsOutput))
            {
                for (int i = firstFrame; i <= lastFrame; i++)
                {
                    AdvFrameInfo frameInfo;
                    if (isCalibrationFrame)
                        m_AdvFile.GetCalibrationFramePixels((uint)i, out frameInfo);
                    else
                        m_AdvFile.GetMainFramePixels((uint)i, out frameInfo);

                    string headerRow;
                    string nextRow = StatusDataToCsvRow(frameInfo, i, out headerRow);
                    if (!headerAppended)
                    {
                        writer.WriteLine(headerRow);
                        headerAppended = true;
                    }
                    writer.WriteLine(nextRow);

                    int percDone = (int)Math.Min(90, 90 * (i - firstFrame) * 1.0 / (lastFrame - firstFrame + 1));
                    progressCallback(5 + percDone, 0);
                }

                progressCallback(95, 0);

                writer.Flush();
            }
        }
コード例 #11
0
ファイル: frmAdv2Viewer.cs プロジェクト: hpavlov/tangra3
        internal bool SaveAsAviFile(string fileName, int firstFrame, int lastFrame, AdvToAviConverter converter, bool tryCodec, bool isCalibrationStream, double msPerFrame, double addedGamma, OnProgressDelegate progressCallback)
        {
            IAviSaver saver = AdvToAviConverterFactory.CreateConverter(converter);

            saver.CloseAviFile();
            if (!saver.StartNewAviFile(fileName, (int)m_AdvFile.Width, (int)m_AdvFile.Height, 8, 25, tryCodec))
            {
                progressCallback(100, 0);
                return false;
            }
            try
            {
                int aviFrameNo = 0;
                AdvIndexEntry firstFrameIdx = isCalibrationStream ? m_AdvFile.CalibrationIndex[firstFrame] : m_AdvFile.MainIndex[firstFrame];
                double ticksToMSFactor = 1000.0 / (isCalibrationStream ? m_AdvFile.CalibrationSteamInfo.ClockFrequency : m_AdvFile.MainSteamInfo.ClockFrequency);
                double startingTimeMilliseconds = firstFrameIdx.ElapsedTicks * ticksToMSFactor;

                if (m_AdvFile.MainIndex[lastFrame].ElapsedTicks != 0)
                {
                    // Sampling can be done as we have sufficient timing information
                }
                else
                {
                    MessageBox.Show(
                        "There is insufficient timing information in this file to convert it to AVI. This could be caused by an old file format or trying to make an AVI from a single frame.",
                        "Tangra 3",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);

                    return false;
                }

                if (MessageBox.Show(
                    this,
                    "Please note that the AVI export is doing resampling of the original video which will typically cause frames to duplicated and/or dropped.\r\n\r\nThis export function is meant to be used for video streaming (i.e. sharing the video for viewing on the Internet) and should not be used to convert the video to another format for measuring in another software. If you want to measure the video in another software either measure it directly as ADV/AAV file (if supported) or export it to a FITS file sequence from the main file menu and measure the FITS images.\r\n\r\nDo you wish to continue?",
                    "Tangra",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Warning) != DialogResult.Yes)
                    return false;

                progressCallback(5, 0);

                for (int i = firstFrame; i <= lastFrame; i++)
                {
                    AdvIndexEntry frame = isCalibrationStream ? m_AdvFile.CalibrationIndex[i] : m_AdvFile.MainIndex[i];

                    AdvFrameInfo frameInfo = null;
                    uint[] pixels = null;

                    if (isCalibrationStream)
                        pixels = m_AdvFile.GetCalibrationFramePixels((uint)i, out frameInfo);
                    else
                        pixels = m_AdvFile.GetMainFramePixels((uint)i, out frameInfo);

                    using (Pixelmap pixmap = CreatePixelmap(pixels))
                    {
                        int lastRepeatedAviFrameNo = 0;

                        if (frame.ElapsedTicks != 0)
                            lastRepeatedAviFrameNo = (int)Math.Round((frame.ElapsedTicks * ticksToMSFactor - startingTimeMilliseconds) / msPerFrame);

                        while (aviFrameNo < lastRepeatedAviFrameNo)
                        {
                            if (!saver.AddAviVideoFrame(pixmap, addedGamma, m_AdvFile.MaxPixelValue))
                            {
                                progressCallback(100, 0);
                                return false;
                            }
                            aviFrameNo++;
                        }
                    }

                    int percDone = (int)Math.Min(90, 90 * (i - firstFrame) * 1.0 / (lastFrame - firstFrame + 1));
                    progressCallback(5 + percDone, 0);
                }
            }
            finally
            {
                saver.CloseAviFile();
                progressCallback(100, 0);
            }

            return false;
        }
コード例 #12
0
ファイル: Solver.cs プロジェクト: baguio/SSC-AI
        public static State Calculate (
            State initial, List<NodeCollection> path,
            int storage_limit, ICostFactory cost_factory,
            OnProgressDelegate on_progress, OnProgressArg arg
        ) {
            int path_offset = initial.distance_from_start + 1;

            OpenedStateCollection opened = new OpenedStateCollection(storage_limit, 1000/*magic number*/, new StateComparer(0.2f/*magic number*/));
            opened.Add(initial);
            State prv_opened = null;
            State result = null;

            List<State> tmp = new List<State>();
            while (opened.Commit()) {
                State cur = opened.First();
                int nxt_distance_from_start = cur.distance_from_start + 1;
                int path_index = nxt_distance_from_start - path_offset;
                if (path_index == path.Count) {
                    if (result == null || result.cost.GetTotalCost() > cur.cost.GetTotalCost()) {
                        result = cur;
                    }
                    continue;
                }
                prv_opened = cur;
                Beat beat = path[path_index].beat;
                List<Node> neighbours = path[path_index].items;

                if (on_progress != null) {
                    arg.progress_cur = (int)((float)nxt_distance_from_start / (float)arg.depth_max * 100.0f);
                    arg.depth_cur = nxt_distance_from_start;
                    arg.second_cur = cur.second;

                    ++arg.closed;

                    arg.opened_cur = opened.Count();
                    arg.opened_total = arg.prv_iter_opened_total + opened.GetTotalAdded();
                    arg.discarded = arg.prv_iter_discarded + opened.GetTotalDiscarded();

                    on_progress(arg);
                    if (!arg.carry_on) {
                        return result;
                    }
                }
                if (neighbours.Count == 0) {
                    throw new ExecutionEngineException();
                } else {
                    //tmp.Clear();
                    foreach (Node n in neighbours) {
                        if (!ArcCalculator.IsArcValid(cur, n)) {
                            continue;
                        }
                        List<Analyzer.State.Limb[]> possibilities = new List<Analyzer.State.Limb[]>();
                        ArcCalculator.Calculate(cur, n, possibilities);
                        foreach (Analyzer.State.Limb[] p in possibilities) {
                            State nxt = ArcCalculator.TransitionTo(cur, p, n.second, n.distance_from_start, beat, cost_factory);
                            if (nxt != null) {
                                //tmp.Add(nxt);
                                opened.Add(nxt);
                            }
                        }
                    }
                }
            }
            return result;
        }