Beispiel #1
0
 static CpObjectiveProto NewMinimize1(int v1, long c1)
 {
     CpObjectiveProto obj = new CpObjectiveProto();
       obj.Vars.Add(v1);
       obj.Coeffs.Add(c1);
       return obj;
 }
Beispiel #2
0
 static CpObjectiveProto NewMaximize1(int v1, long c1)
 {
     CpObjectiveProto obj = new CpObjectiveProto();
       obj.Vars.Add(-v1 - 1);
       obj.Coeffs.Add(c1);
       obj.ScalingFactor = -1;
       return obj;
 }
Beispiel #3
0
        // Internal methods.

        void SetObjective(LinearExpr obj, bool minimize)
        {
            CpObjectiveProto objective = new CpObjectiveProto();

            if (obj is null)
            {
                objective.Offset        = 0L;
                objective.ScalingFactor = minimize ? 1L : -1;
            }
            else if (obj is IntVar intVar)
            {
                objective.Offset        = 0L;
                objective.Vars.Capacity = 1;
                objective.Vars.Add(intVar.Index);

                objective.Coeffs.Capacity = 1;
                if (minimize)
                {
                    objective.Coeffs.Add(1L);
                    objective.ScalingFactor = 1L;
                }
                else
                {
                    objective.Coeffs.Add(-1L);
                    objective.ScalingFactor = -1L;
                }
            }
            else
            {
                var dict = var_value_map_;
                dict.Clear();
                long constant  = LinearExpr.GetVarValueMap(obj, dict, terms_);
                var  dictCount = dict.Count;
                objective.Vars.Capacity = dictCount;
                objective.Vars.AddRange(dict.Keys);
                objective.Coeffs.Capacity = dictCount;
                if (minimize)
                {
                    objective.Coeffs.AddRange(dict.Values);
                    objective.ScalingFactor = 1L;
                    objective.Offset        = constant;
                }
                else
                {
                    foreach (var coeff in dict.Values)
                    {
                        objective.Coeffs.Add(-coeff);
                    }
                    objective.ScalingFactor = -1L;
                    objective.Offset        = -constant;
                }
            }
            model_.Objective = objective;
        }
Beispiel #4
0
        static CpObjectiveProto NewMaximize2(int v1, int v2, long c1, long c2)
        {
            CpObjectiveProto obj = new CpObjectiveProto();

            obj.Vars.Add(-v1 - 1);
            obj.Vars.Add(-v2 - 1);
            obj.Coeffs.Add(c1);
            obj.Coeffs.Add(c2);
            obj.ScalingFactor = -1;
            return(obj);
        }
Beispiel #5
0
        // Internal methods.

        void SetObjective(LinearExpr obj, bool minimize)
        {
            CpObjectiveProto objective = new CpObjectiveProto();

            if (obj is null)
            {
                objective.Offset        = 0L;
                objective.ScalingFactor = minimize ? 1L : -1;
            }
            else if (obj is IntVar intVar)
            {
                objective.Offset        = 0L;
                objective.Vars.Capacity = 1;
                objective.Vars.Add(intVar.Index);

                objective.Coeffs.Capacity = 1;
                if (minimize)
                {
                    objective.Coeffs.Add(1L);
                    objective.ScalingFactor = 1L;
                }
                else
                {
                    objective.Coeffs.Add(-1L);
                    objective.ScalingFactor = -1L;
                }
            }
            else
            {
                Dictionary <IntVar, long> dict = new Dictionary <IntVar, long>();
                long constant = LinearExpr.GetVarValueMap(obj, 1L, dict, terms_);
                if (minimize)
                {
                    objective.ScalingFactor = 1L;
                    objective.Offset        = constant;
                }
                else
                {
                    objective.ScalingFactor = -1L;
                    objective.Offset        = -constant;
                }

                var dictCount = dict.Count;
                objective.Vars.Capacity   = dictCount;
                objective.Coeffs.Capacity = dictCount;
                foreach (KeyValuePair <IntVar, long> it in dict)
                {
                    objective.Vars.Add(it.Key.Index);
                    objective.Coeffs.Add(minimize ? it.Value : -it.Value);
                }
            }
            model_.Objective = objective;
        }
Beispiel #6
0
        // Internal methods.

        void SetObjective(IntegerExpression obj, bool minimize)
        {
            CpObjectiveProto objective = new CpObjectiveProto();

            if (obj is IntVar)
            {
                objective.Coeffs.Add(1L);
                objective.Offset = 0L;
                if (minimize)
                {
                    objective.Vars.Add(obj.Index);
                    objective.ScalingFactor = 1L;
                }
                else
                {
                    objective.Vars.Add(Negated(obj.Index));
                    objective.ScalingFactor = -1L;
                }
            }
            else
            {
                Dictionary <IntVar, long> dict = new Dictionary <IntVar, long>();
                long constant = IntegerExpression.GetVarValueMap(obj, 1L, dict);
                if (minimize)
                {
                    objective.ScalingFactor = 1L;
                    objective.Offset        = constant;
                }
                else
                {
                    objective.ScalingFactor = -1L;
                    objective.Offset        = -constant;
                }
                foreach (KeyValuePair <IntVar, long> it in dict)
                {
                    objective.Coeffs.Add(it.Value);
                    if (minimize)
                    {
                        objective.Vars.Add(it.Key.Index);
                    }
                    else
                    {
                        objective.Vars.Add(Negated(it.Key.Index));
                    }
                }
            }
            model_.Objective = objective;
        }