Esempio n. 1
0
        private void EnergyGrad(double[] x, ref double func, double[] grad, object obj)
        {
            Tuple <Story, PositionTable <double>, int, int> tuple = obj as Tuple <Story, PositionTable <double>, int, int>;
            Story story = tuple.Item1;
            PositionTable <double> position = tuple.Item2;
            int frame = tuple.Item3;
            int i     = tuple.Item4;

            func    = 0;
            grad[0] = 0;

            for (int j = 0; j < story.Characters.Count; ++j)
            {
                if (i != j && story.SessionTable[j, frame] != -1)
                {
                    if (story.SessionTable[i, frame] == story.SessionTable[j, frame])
                    {
                        var t = InnerEnergy(x[0], position[j, frame], 1.0, 1.0 * (Ultities.GetGroupCount(story, i, frame) - 1));
                        func    += InnerDistanceConstraintWeight * t.Item1;
                        grad[0] += InnerDistanceConstraintWeight * t.Item2;
                    }
                    else
                    {
                        var t = OuterEnergy(x[0], position[j, frame], 5.0);
                        func    += OutterDistanceConstraintWeight * t.Item1;
                        grad[0] += OutterDistanceConstraintWeight * t.Item2;
                    }
                }
            }

            if (LineStraightFunction == 1 && frame > 0 && frame < story.FrameCount - 1 && story.SessionTable[i, frame - 1] != -1 && story.SessionTable[i, frame + 1] != -1)
            {
                var t = StraightEnergy(x[0], position[i, frame - 1], position[i, frame + 1]);
                func    += LineStraightWeight * t.Item1;
                grad[0] += LineStraightWeight * t.Item2;
            }
            else
            {
                if (frame > 0 && story.SessionTable[i, frame - 1] != -1)
                {
                    var t = StraightEnergy(x[0], position[i, frame - 1]);
                    func    += LineStraightWeight * t.Item1;
                    grad[0] += LineStraightWeight * t.Item2;
                }
                else if (frame < story.FrameCount - 1 && story.SessionTable[i, frame + 1] != -1)
                {
                    var t = StraightEnergy(x[0], position[i, frame + 1]);
                    func    += LineStraightWeight * t.Item1;
                    grad[0] += LineStraightWeight * t.Item2;
                }
                else
                {
                }
            }
        }
Esempio n. 2
0
 static Log()
 {
     try
     {
         LoggingService = new LoggingService();
     }
     catch (Exception e)
     {
         Ultities.EventLogException("Tracer constructor", e);
     }
 }
Esempio n. 3
0
 public static void LogDebug(string message, params object[] parameters)
 {
     try
     {
         string fullName = Assembly.GetCallingAssembly().FullName;
         LogMessage(LogLevel.Debug, message + FormatParams(parameters), null, fullName, null, 0, false);
     }
     catch (Exception e)
     {
         Ultities.EventLogException("Tracer.TraceDebug", e);
     }
 }
Esempio n. 4
0
 public static void LogTrace(LogLevel severity, string message)
 {
     try
     {
         string fullName = Assembly.GetCallingAssembly().FullName;
         LogMessage(severity, message, null, fullName, null, 0, false);
     }
     catch (Exception e)
     {
         Ultities.EventLogException("Tracer.Trace", e);
     }
 }
Esempio n. 5
0
 public static void LogInfo(string message, params object[] parameters)
 {
     try
     {
         LogMessage(LogLevel.Info, message + FormatParams(parameters), null,
                    Assembly.GetCallingAssembly().FullName, null, 0, false);
     }
     catch (Exception e)
     {
         Ultities.EventLogException("Tracer.TraceInfo", e);
     }
 }
Esempio n. 6
0
 public static void LogError(string errorMessage, int errorNumber = 0)
 {
     try
     {
         string fullName = Assembly.GetCallingAssembly().FullName;
         LogMessage(LogLevel.Error, errorMessage, null, fullName, null, errorNumber, false);
     }
     catch (Exception e)
     {
         Ultities.EventLogException("Tracer.TraceError", e);
     }
 }
Esempio n. 7
0
        private LogLevel GetNLogLevelFromSeverity(int severity)
        {
            LogLevel info = LogLevel.Info;

            try
            {
                info = LogLevel.FromOrdinal(severity);
            }
            catch (Exception e)
            {
                Ultities.EventLogException("LoggingService.GetNLogLevelFromSeverity", e);
            }
            return(info);
        }
Esempio n. 8
0
 private static void LogException(LogLevel severity, Exception exception, string message = null)
 {
     try
     {
         string fullName = Assembly.GetCallingAssembly().FullName;
         if (!string.IsNullOrEmpty(message))
         {
             LogMessage(severity, message, null, fullName, null, 0, false);
         }
         LogMessage(severity, null, null, fullName, exception, 0, false);
     }
     catch (Exception e)
     {
         Ultities.EventLogException("Tracer.TraceException", e);
     }
 }
Esempio n. 9
0
        public static void LogDataSet(LogLevel severity, DataSet dataset)
        {
            string fullName = Assembly.GetCallingAssembly().FullName;

            try
            {
                if (dataset == null)
                {
                    LogMessage(LogLevel.Error, "TraceDataSet called with a null dataset", null, fullName, null, 0, false);
                }
                else
                {
                    string tempFileName = Path.GetTempFileName();
                    dataset.WriteXml(tempFileName);
                    LogMessage(severity, "The DataSet file was written to:" + tempFileName, null, fullName, null, 0, false);
                }
            }
            catch (Exception e)
            {
                Ultities.EventLogException("Logger.LogDataSet", e);
            }
        }
Esempio n. 10
0
        private static void LogFunc(string functionName, bool enterFunc = false, params object[] parameters)
        {
            string fullName = Assembly.GetCallingAssembly().FullName;

            string isEnterStr = enterFunc ? "LogEnterFunc" : "LogExitFunc";

            try
            {
                if (string.IsNullOrEmpty(functionName))
                {
                    LogMessage(LogLevel.Error, isEnterStr + " called with a null function name.", null, fullName, null,
                               0, false);
                }
                else
                {
                    LogMessage(LogLevel.Trace, isEnterStr + functionName + FormatParams(parameters), null, fullName,
                               null, 0, false);
                }
            }
            catch (Exception e)
            {
                Ultities.EventLogException("Logger.Logger." + isEnterStr, e);
            }
        }
Esempio n. 11
0
        public PositionTable <int> Calculate(Story story)
        {
            PositionTable <int> ans = null;

            int[] permutation        = Ultities.GetFeasiblePermutation(story, 0);
            PositionTable <int> perm = new PositionTable <int>(story.Characters.Count, story.TimeStamps.Length - 1);

            for (int i = 0; i < story.Characters.Count; ++i)
            {
                for (int j = 0; j < story.TimeStamps.Length - 1; ++j)
                {
                    perm[i, j] = -1;
                }
            }

            for (int id = 0; id < story.Characters.Count; ++id)
            {
                if (story.SessionTable[id, 0] != -1)
                {
                    perm[id, 0] = permutation[id];
                }
            }

            Sweep(story, ref ans, perm, true, true);

            EfficientBarycenterCalculator calc1 = new EfficientBarycenterCalculator();

            perm = calc1.Calculate(story);
            Update(story, ref ans, perm);

            Sweep(story, ref ans, perm, true, true);

            Sweep(story, ref ans, perm, false, true);

            return(ans);
        }
Esempio n. 12
0
        public Tuple <double, double, double>[][] Relax(Story story, PositionTable <double> position)
        {
            Tuple <double, double, double>[][] result = new Tuple <double, double, double> [story.Characters.Count][];
            for (int i = 0; i < story.Characters.Count; ++i)
            {
                result[i] = new Tuple <double, double, double> [story.FrameCount];
            }
            double xBaseline = 0;

            for (int frame = 0; frame < story.FrameCount; ++frame)
            {
                double xEnd = xBaseline + _app.Status.Config.Style.XFactor * (story.TimeStamps[frame + 1] - story.TimeStamps[frame]);
                for (int i = 0; i < story.Characters.Count; ++i)
                {
                    result[i][frame] = new Tuple <double, double, double>(xBaseline, xEnd, position[i, frame]);
                }
                xBaseline = xEnd + _app.Status.Config.Style.XGap;
            }

            if (!_app.Status.Config.Relaxing)
            {
                return(result);
            }
            // calculate x factor
            //double xfactor;
            {
                double min = int.MaxValue;
                double max = int.MinValue;
                for (int i = 0; i < story.Characters.Count; ++i)
                {
                    for (int frame = 0; frame < story.FrameCount; ++frame)
                    {
                        if (min > position[i, frame])
                        {
                            min = position[i, frame];
                        }
                        if (max < position[i, frame])
                        {
                            max = position[i, frame];
                        }
                    }
                }
                //xfactor = 3 * (max - min) / story.TimeStamps[story.TimeStamps.Length - 1];
            }

            for (int frame = 0; frame < story.FrameCount - 1; ++frame)
            {
                int left  = frame;
                int right = frame + 1;
                List <Tuple <int, List <int> > > list1 = Ultities.GetGroups(story, left);
                List <Tuple <int, List <int> > > list2 = Ultities.GetGroups(story, right);
                foreach (Tuple <int, List <int> > tuple1 in list1)
                {
                    foreach (Tuple <int, List <int> > tuple2 in list2)
                    {
                        List <int> intersection = tuple1.Item2.Intersect(tuple2.Item2).ToList();
                        if (intersection.Count == tuple1.Item2.Count || intersection.Count == tuple2.Item2.Count)
                        {
                            double bc1   = GetBarycenter(position, tuple1.Item2, left);
                            double bc2   = GetBarycenter(position, tuple2.Item2, right);
                            double delta = Math.Abs(bc1 - bc2) - _app.Status.Config.RelaxingGradient * _app.Status.Config.Style.XGap;
                            if (delta > 0)
                            {
                                if (intersection.Count == tuple1.Item2.Count && intersection.Count == tuple2.Item2.Count)
                                {
                                    foreach (int x in intersection)
                                    {
                                        result[x][left] = new Tuple <double, double, double>(result[x][left].Item1, result[x][left].Item2 - delta / 2, result[x][left].Item3);
                                    }
                                    foreach (int x in intersection)
                                    {
                                        result[x][right] = new Tuple <double, double, double>(result[x][right].Item1 + delta / 2, result[x][right].Item2, result[x][right].Item3);
                                    }
                                }
                                else
                                {
                                    if (intersection.Count == tuple1.Item2.Count)
                                    {
                                        foreach (int x in intersection)
                                        {
                                            result[x][left] = new Tuple <double, double, double>(result[x][left].Item1, result[x][left].Item2 - delta, result[x][left].Item3);
                                        }
                                    }
                                    else
                                    {
                                        foreach (int x in intersection)
                                        {
                                            result[x][right] = new Tuple <double, double, double>(result[x][right].Item1 + delta, result[x][right].Item2, result[x][right].Item3);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }
Esempio n. 13
0
        private void EnergyGrad(double[] x, ref double func, double[] grad, object obj)
        {
            Tuple <Story, PositionTable <double>, Dictionary <Tuple <int, int>, int> > tuple = obj as Tuple <Story, PositionTable <double>, Dictionary <Tuple <int, int>, int> >;
            Story story = tuple.Item1;
            PositionTable <double>             position = tuple.Item2;
            Dictionary <Tuple <int, int>, int> dict     = tuple.Item3;


            // calculate func
            func = 0;
            Tuple <double, double, double> tupleInnerDistance;
            Tuple <double, double, double> tupleOuterDistance;
            Tuple <double, double, double> tupleLineStraight;

            // calculate grad
            for (int frame = 0; frame < story.FrameCount; ++frame)
            {
                for (int i = 0; i < story.Characters.Count; ++i)
                {
                    if (story.SessionTable[i, frame] != -1)
                    {
                        int index = dict[new Tuple <int, int>(i, frame)];
                        grad[index] = 0;

                        for (int j = 0; j < story.Characters.Count; ++j)
                        {
                            if (i != j && story.SessionTable[j, frame] != -1)
                            {
                                if (story.SessionTable[i, frame] == story.SessionTable[j, frame])
                                {
                                    var t = InnerEnergy(x[index], x[dict[new Tuple <int, int>(j, frame)]], 1.0, 1.0 * (Ultities.GetGroupCount(story, i, frame) - 1));
                                    func        += InnerDistanceConstraintWeight * t.Item1;
                                    grad[index] += InnerDistanceConstraintWeight * t.Item2;
                                }
                                else
                                {
                                    var t = OuterEnergy(x[index], x[dict[new Tuple <int, int>(j, frame)]], 5.0);
                                    func        += OutterDistanceConstraintWeight * t.Item1;
                                    grad[index] += OutterDistanceConstraintWeight * t.Item2;
                                }
                            }
                        }

                        if (LineStraightFunction == 1 && frame > 0 && frame < story.FrameCount - 1 && story.SessionTable[i, frame - 1] != -1 && story.SessionTable[i, frame + 1] != -1)
                        {
                            var t = StraightEnergy(x[index], x[dict[new Tuple <int, int>(i, frame - 1)]], x[dict[new Tuple <int, int>(i, frame + 1)]]);
                            func        += LineStraightWeight * t.Item1;
                            grad[index] += LineStraightWeight * t.Item2;
                        }
                        else
                        {
                            if (frame > 0 && story.SessionTable[i, frame - 1] != -1)
                            {
                                var t = StraightEnergy(x[index], x[dict[new Tuple <int, int>(i, frame - 1)]]);
                                func        += LineStraightWeight * t.Item1;
                                grad[index] += LineStraightWeight * t.Item2;
                            }
                            else if (frame < story.FrameCount - 1 && story.SessionTable[i, frame + 1] != -1)
                            {
                                var t = StraightEnergy(x[index], x[dict[new Tuple <int, int>(i, frame + 1)]]);
                                func        += LineStraightWeight * t.Item1;
                                grad[index] += LineStraightWeight * t.Item2;
                            }
                            else
                            {
                            }
                        }
                    }
                }
            }
        }
Esempio n. 14
0
        public Dictionary <Tuple <int, int>, Tuple <double, double> > Relax(Story story, PositionTable <int> position)
        {
            // calculate a side free dictionary
            Dictionary <Tuple <int, int>, Tuple <bool, bool> > free = new Dictionary <Tuple <int, int>, Tuple <bool, bool> >();

            for (int frame = 0; frame < story.FrameCount; ++frame)
            {
                List <Tuple <int, List <int> > > list  = Ultities.GetGroups(story, frame);
                List <Tuple <int, List <int> > > list1 = frame == 0 ? null : Ultities.GetGroups(story, frame - 1);
                List <Tuple <int, List <int> > > list2 = frame == story.FrameCount - 1 ? null : Ultities.GetGroups(story, frame + 1);
                foreach (Tuple <int, List <int> > tuple in list)
                {
                    bool leftFree = false;
                    if (frame != 0)
                    {
                        foreach (Tuple <int, List <int> > tuple1 in list1)
                        {
                            List <int> intersection = tuple.Item2.Intersect(tuple1.Item2).ToList();
                            if (intersection.Count == tuple.Item2.Count)
                            {
                                leftFree = true;
                                break;
                            }
                        }
                    }
                    bool rightFree = false;
                    if (frame != story.FrameCount - 1)
                    {
                        foreach (Tuple <int, List <int> > tuple2 in list2)
                        {
                            List <int> intersection = tuple.Item2.Intersect(tuple2.Item2).ToList();
                            if (intersection.Count == tuple.Item2.Count)
                            {
                                rightFree = true;
                                break;
                            }
                        }
                    }
                    free.Add(new Tuple <int, int>(frame, tuple.Item1), new Tuple <bool, bool>(leftFree, rightFree));
                }
            }

            Dictionary <Tuple <int, int>, double> leftIndent  = new Dictionary <Tuple <int, int>, double>();
            Dictionary <Tuple <int, int>, double> rightIndent = new Dictionary <Tuple <int, int>, double>();

            for (int id = 0; id < story.Characters.Count; ++id)
            {
                for (int frame = 0; frame < story.FrameCount; ++frame)
                {
                    int session = story.SessionTable[id, frame];
                    if (session != -1)
                    {
                    }
                }
            }


            // <frame, session_id> -> <left, right>
            Dictionary <Tuple <int, int>, Tuple <double, double> > result = new Dictionary <Tuple <int, int>, Tuple <double, double> >();

            return(result);
        }
Esempio n. 15
0
        public PositionTable <int> Calculate(Story story)
        {
            int frameCount     = story.TimeStamps.Length - 1;
            int characterCount = story.Characters.Count;

            int[] permutation = Ultities.GetFeasiblePermutation(story, 0);
            //int[] permutation = Ultities.GetRandomFeasiblePermutation(story, 0);

            PositionTable <int> permutationTable = new PositionTable <int>(characterCount, frameCount);

            for (int i = 0; i < characterCount; ++i)
            {
                for (int j = 0; j < frameCount; ++j)
                {
                    permutationTable[i, j] = -1;
                }
            }

            for (int id = 0; id < characterCount; ++id)
            {
                if (story.SessionTable[id, 0] != -1)
                {
                    permutationTable[id, 0] = permutation[id];
                }
            }

            bool[] available = new bool[frameCount];
            available[0] = true;
            bool change = false;

            do
            {
                change = false;
                for (int frame = 0; frame < frameCount; ++frame)
                {
                    if (frame == 0 && (frameCount == 1 || !available[1]))
                    {
                        continue;
                    }
                    available[frame] = true;

                    int[] backup = new int[characterCount];
                    for (int i = 0; i < characterCount; ++i)
                    {
                        backup[i] = permutationTable[i, frame];
                    }

                    Dictionary <int, List <int> > dict = new Dictionary <int, List <int> >();
                    for (int id = 0; id < characterCount; ++id)
                    {
                        if (story.SessionTable[id, frame] != -1)
                        {
                            if (!dict.ContainsKey(story.SessionTable[id, frame]))
                            {
                                dict.Add(story.SessionTable[id, frame], new List <int>());
                            }
                            dict[story.SessionTable[id, frame]].Add(id);
                        }
                    }
                    List <Tuple <int, double> > list = new List <Tuple <int, double> >();
                    foreach (KeyValuePair <int, List <int> > pair in dict)
                    {
                        double barycenter1 = -1;
                        double barycenter2 = -1;
                        if (frame > 0 && available[frame - 1])
                        {
                            double sum   = 0;
                            int    count = 0;
                            foreach (int x in pair.Value)
                            {
                                if (story.SessionTable[x, frame - 1] != -1)
                                {
                                    sum += permutationTable[x, frame - 1];
                                    ++count;
                                }
                            }
                            if (count > 0)
                            {
                                barycenter1 = sum / count;
                            }
                        }
                        if (frame < frameCount - 1 && available[frame + 1])
                        {
                            double sum   = 0;
                            int    count = 0;
                            foreach (int x in pair.Value)
                            {
                                if (story.SessionTable[x, frame + 1] != -1)
                                {
                                    sum += permutationTable[x, frame + 1];
                                    ++count;
                                }
                            }
                            if (count > 0)
                            {
                                barycenter2 = sum / count;
                            }
                        }
                        double weight = -1;
                        if (barycenter1 == -1)
                        {
                            weight = barycenter2;
                        }
                        else if (barycenter2 == -1)
                        {
                            weight = barycenter1;
                        }
                        else
                        {
                            weight = 0.5 * barycenter1 + 0.5 * barycenter2;
                        }

                        list.Add(new Tuple <int, double>(pair.Key, weight));
                    }
                    list.Sort((a, b) =>
                    {
                        if (a.Item2 == b.Item2)
                        {
                            return(dict[a.Item1].First().CompareTo(dict[b.Item1].First()));
                        }
                        return(a.Item2.CompareTo(b.Item2));
                    });

                    int baseline = 0;
                    foreach (Tuple <int, double> tuple in list)
                    {
                        int        group = tuple.Item1;
                        List <int> items = dict[group];
                        if (frame == 0)
                        {
                            items.Sort((a, b) => permutationTable[a, frame + 1].CompareTo(permutationTable[b, frame + 1]));
                        }
                        else
                        {
                            items.Sort((a, b) => permutationTable[a, frame - 1].CompareTo(permutationTable[b, frame - 1]));
                        }
                        foreach (int x in items)
                        {
                            permutationTable[x, frame] = baseline++;
                        }
                    }

                    for (int i = 0; i < characterCount; ++i)
                    {
                        if (backup[i] != permutationTable[i, frame])
                        {
                            change = true;
                        }
                    }
                }
                Console.WriteLine("Crossing:{0}", Crossing.Count(story, permutationTable));
            }while (change);

            return(permutationTable);
        }