public static PiecewiseLinearFunction PenaltyFunction(int startTime, int endTime, int penalty)
        {
            var values = new List <Triplet>(3);

            values.Add(Triplet.Create(int.MinValue, startTime, penalty));
            values.Add(Triplet.Create(startTime, endTime, 0));
            values.Add(Triplet.Create(endTime, int.MaxValue, penalty));

            return(new PiecewiseLinearFunction(values));
        }
        public PiecewiseLinearFunction Shift(int value)
        {
            var v = new List <Triplet>(values.Count);

            for (int i = 0; i < values.Count; ++i)
            {
                v.Add(
                    Triplet.Create(
                        values[i].Item1 == int.MinValue ? int.MinValue : values[i].Item1 - value,
                        values[i].Item2 == int.MaxValue ? int.MaxValue : values[i].Item2 - value,
                        values[i].Item3)
                    );
            }

            return(new PiecewiseLinearFunction(v));
        }
        public PiecewiseLinearFunction AddPiece(Triplet piece)
        {
            var v = this.values.ToArray().ToList();

            if (piece.Item3 == 0 || piece.Item1 >= piece.Item2)
            {
                return(this.Clone());
            }

            for (int i = 0; i < v.Count; ++i)
            {
                if (piece.Item1 > v[i].Item1 && piece.Item1 < v[i].Item2)
                {
                    v.Insert(i, Triplet.Create(v[i].Item1, piece.Item1, v[i].Item3));
                    v[i + 1].Item1 = piece.Item1;
                    break;
                }
            }

            for (int i = 0; i < v.Count; ++i)
            {
                if (piece.Item2 > v[i].Item1 && piece.Item2 < v[i].Item2)
                {
                    v.Insert(i, Triplet.Create(v[i].Item1, piece.Item2, v[i].Item3));
                    v[i + 1].Item1 = piece.Item2;
                    break;
                }
            }

            for (int i = 0; i < v.Count; ++i)
            {
                if (v[i].Item1 >= piece.Item1)
                {
                    v[i].Item3 += piece.Item3;
                }

                if (v[i].Item2 == piece.Item2)
                {
                    break;
                }
            }

            return(new PiecewiseLinearFunction(v));
        }
        public PiecewiseLinearFunction Min()
        {
            var v = new List <Triplet>(values.Count);

            int currentMin = int.MaxValue;

            for (int i = 0; i < values.Count; ++i)
            {
                if (values[i].Item3 < currentMin)
                {
                    v.Add(Triplet.Create(values[i].Item1, values[i].Item2, values[i].Item3));
                    currentMin = values[i].Item3;
                }
                else
                {
                    v[v.Count - 1].Item2 = values[i].Item2;
                }
            }

            return(new PiecewiseLinearFunction(v));
        }
 public PiecewiseLinearFunction()
 {
     values = new List <Triplet>();
     values.Add(Triplet.Create(int.MinValue, int.MaxValue, 0));
 }