コード例 #1
0
ファイル: Var.cs プロジェクト: codeflood/chel
        public CommandResult Execute()
        {
            _executionCultureName = Thread.CurrentThread.CurrentCulture.Name;

            // todo: temporary implementation. Variables should be output as a list once we have those in the type system.
            var output = new StringBuilder();

            if (string.IsNullOrEmpty(Value))
            {
                if (string.IsNullOrEmpty(Name))
                {
                    ListVariables(output);
                }
                else
                {
                    ShowVariable(output, Name);
                }
            }
            else
            {
                var variable = new ValueVariable(Name, Value);
                _variables.Set(variable);
                output.Append(Value);
            }

            return(new ValueResult(output.ToString()));
        }
コード例 #2
0
        public void Ctor_WhenCalled_SetsProperties()
        {
            // arrange, act
            var sut = new ValueVariable("name", "value");

            // assert
            Assert.Equal("value", sut.Value);
        }
コード例 #3
0
        public void Get_DifferentCasingForName_ReturnsVariable(string name)
        {
            // arrange
            var sut      = new VariableCollection();
            var variable = new ValueVariable("name", "alpha");

            sut.Set(variable);

            // act
            var result = sut.Get(name) as ValueVariable;

            // assert
            Assert.Equal(variable, result);
        }
コード例 #4
0
        public void Set_WhenCalled_StoresVariable()
        {
            // arrange
            var sut      = new VariableCollection();
            var variable = new ValueVariable("name", "alpha");

            // act
            sut.Set(variable);

            // assert
            var result = sut.Get("name") as ValueVariable;

            Assert.Equal("alpha", variable.Value);
        }
コード例 #5
0
        public void Remove_VariableNotSet_DoesNothing()
        {
            // arrange
            var sut      = new VariableCollection();
            var variable = new ValueVariable("name", "alpha");

            sut.Set(variable);

            // act
            sut.Remove("othername");

            // assert
            var names = sut.Names;

            Assert.Equal(new[] { "name" }, names);
        }
コード例 #6
0
        public void Remove_VariableWasSet_RemovesVariable(string name)
        {
            // arrange
            var sut      = new VariableCollection();
            var variable = new ValueVariable("name", "alpha");

            sut.Set(variable);

            // act
            sut.Remove(name);

            // assert
            var result = sut.Get("name");

            Assert.Null(result);
        }
コード例 #7
0
        public void SetGet_VariableAlreadySet_ReplacesVariable()
        {
            // arrange
            var sut       = new VariableCollection();
            var variable1 = new ValueVariable("name", "alpha");
            var variable2 = new ValueVariable("name", "beta");

            sut.Set(variable1);

            // act
            sut.Set(variable2);

            // assert
            var result = sut.Get("name") as ValueVariable;

            Assert.Equal("beta", result.Value);
        }
コード例 #8
0
ファイル: LinearParser.cs プロジェクト: spmadeira/SharpSolver
    //Parse string into a linear model.
    public static LinearModel Parse(string model)
    {
        LinearModel Model = new LinearModel();

        string[] cleanedInput = CleanInput(model);

        var declarativeLine = cleanedInput[0];

        if (declarativeLine.StartsWith("MAXZ="))
        {
            Model.Objective = Objective.MAX;
        }
        else if (declarativeLine.StartsWith("MINZ="))
        {
            Model.Objective = Objective.MIN;
        }
        else
        {
            throw new InvalidInputException("Invalid or undeclared objective.", 0, cleanedInput);
        }

        var decMatches = VarRegex.Matches(declarativeLine);

        Dictionary <string, Variable> declaredVariables = new Dictionary <string, Variable>();

        foreach (Match match in decMatches)
        {
            try
            {
                var         name       = match.Groups[2].Value;
                var         valueMatch = match.Groups[1].Value;
                BigRational value;
                if (valueMatch == "-")
                {
                    value = -1;
                }
                else
                {
                    var hasValue = decimal.TryParse(valueMatch, out var decimalValue);
                    if (!hasValue)
                    {
                        value = 1;
                    }
                    else
                    {
                        value = decimalValue;
                    }
                }

                Variable var = Variable.Input(name);
                declaredVariables[name] = var;
                ValueVariable valVar = var.As(value);
                Model.Variables.Add(var);
                Model.ObjectiveFunction.Add(valVar);
            }
            catch (Exception e)
            {
                throw new InvalidInputException("Invalid variable declaration.", 0, cleanedInput, e);
            }
        }


        for (int i = 1; i < cleanedInput.Length; i++)
        {
            var constraintLine = cleanedInput[i];

            var variables = new List <ValueVariable>();
            var vars      = VarRegex.Matches(constraintLine);

            foreach (Match match in vars)
            {
                try
                {
                    var         name       = match.Groups[2].Value;
                    var         valueMatch = match.Groups[1].Value;
                    BigRational value;
                    if (valueMatch == "-")
                    {
                        value = -1;
                    }
                    else
                    {
                        var hasValue = decimal.TryParse(valueMatch, out var decimalValue);
                        if (!hasValue)
                        {
                            value = 1;
                        }
                        else
                        {
                            value = decimalValue;
                        }
                    }

                    if (declaredVariables.TryGetValue(name, out var variable))
                    {
                        variables.Add(variable.As(value));
                    }
                    else
                    {
                        throw new InvalidInputException($"Undeclared variable {name}", i, cleanedInput);
                    }
                }
                catch (Exception e)
                {
                    throw new InvalidInputException($"Invalid variable declaration.", i, cleanedInput, e);
                }
            }

            Constraint ct;
            try
            {
                var constraint           = ConstraintRegex.Match(constraintLine);
                var constraintSign       = constraint.Groups[1].Value;
                var constraintValueMatch = constraint.Groups[2].Value;
                var constraintValue      = decimal.Parse(constraintValueMatch);

                switch (constraintSign)
                {
                case ">=":
                    ct = Constraint.GreaterThanOrEqualTo($"CT{i}", constraintValue, variables);
                    break;

                case "<=":
                    ct = Constraint.LessThanOrEqualTo($"CT{i}", constraintValue, variables);
                    break;

                case "=":
                case "==":
                    ct = Constraint.EqualTo($"CT{i}", constraintValue, variables);
                    break;

                default:
                    throw new InvalidInputException("Invalid constraint value", i, cleanedInput);
                }
            }
            catch (Exception e)
            {
                throw new InvalidInputException("Invalid constraint declaration", i, cleanedInput, e);
            }
            Model.Constraints.Add(ct);
        }

        return(Model);
    }