Expression evaluator without PEMDASM - c++

I have a question. I have these codes.
int x = 20 , y = 2 , z = 5 , a = 0, b = 2 ;
a = x*y-z/b
cout<<"a is "<<a;
done;
when performing this, I want it to be performed like a = 20 * 2 - 5 /2 and I want the result to be 17.5. c++ normally follows PEMDASM rules. How can I perform the expression without following the PEMDAS rule? thanks. I want it to perform according to the order in which they appear first, * appeared first, then - then / . thank you so much

You have to use parentheses:
a = (x*y - z)/b;

Related

How to iterate over objective function

I want to potentiate certain terms in my objective function.
model.addConstr(KW == quicksum(I[t] *(1.05**(-i)) for t in Tst + Z[t]
* (1.05**(-j)) for t in T)
model.setObjective(KW,GRB.MAXIMIZE)
model.optimize()
The variable i should run from 1 to the number of elements in Tst and T
respectively.
So if t in Tst is [2020,2021,2022], I[2020] gets multiplied by 1.05**
(-1)
I[2021] by 1.05**(-2) and I[2022] by 1.05**(-3).
Same with Z[t], only that the list of T is larger than Tst.
for i in range(1,len(Tst)+1):
model.addConstr(KW == quicksum(I[t] * (1.05**(-i)))
However KW is always 0 then, which it shouldnt be. What am i missing?
I just created a 2nd dictionary
Expo = {}
i=1
for t in T:
Expo[t] = i
i = i + 1
If I do:
model.addConstr(KW == quicksum(I[t] *(1.05**(Expo[t])) for t in Tst + Z[t]
* (1.05**(Expo[t])) for t in T)
model.setObjective(KW,GRB.MAXIMIZE)
model.optimize()
it does what i want. But I dont think its a very good solution :P

Increasing the index by 1 - Converting a code in C to MATLAB [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
I'm manually converting a code in C to MATLAB.
The code contains assignments like the following,
y[10] = p[32]+(p[31]-p[32])*pow(p_c[0],p[34])/(pow(p_c[0],p[34])+pow(p[33],p[34]));
To use it in MATLAB, I am manually increasing the value of all indexes by 1, since the index in MATLAB starts from 1.
y[10+1] = p[32+1]+(p[31+1]-p[32+1])*pow(p_c[0+1],p[34+1])/(pow(p_c[0+1],p[34+1])+pow(p[33+1],p[34+1]));
Is there an easy way of doing this task?
There are around 30 assignments like the above example and I am trying to avoid doing this manually.
Edit 1:
Would it be possible to use regular expression? I can copy all the lines of code that contain assignment in a text file. Using regular expression, if I can locate [35](any number) replace with [35+1]. I'm not sure how to implement this in a code.
Edit 2:A sample of other assignments in the code.
y[0] = ct[0]-x[12];
y[1] = ct[1]-1*x[10]-x[23];
y[3] = p_c[6]+p_c[5];
y[4] = p_c[2];
y[5] = x_c[23]+x_c[10]+y_c[1];
y[6] = y_c[0]+x_c[12];
p[0] = 30;
p[1] = 12;
p[2] = 2;
p[3] = 0;
p[4] = 90;
p[5] = 45
dx[0] = FunctionForD(p[67], p[64], p[66], p[65], p[23], x_c[0], x_c[3], p[49])*p[23]-FunctionForA(y[28], y[29], p[23], y[16])*p[23]+FunctionForD(y[30], y[31], p[23], y[16])*p[23]-FunctionForA(p[134], p[133], p[132], p[130], p[131], p_c[2], p[23], x_c[21], x_c[0], p[49])*p[23]; //
dx[1] = FunctionFor2(p[169], p[167], p[168], p[166], p[23], x_c[1], x_c[17], p[49])
If you were dealing with just one array, you could come up a way to do this. But here you are dealing with 8 different arrays (y, ct, x, p_c, x_c, y_c, p, dx). And the assignments too are in no particular order. They involve various combinations.
If you are using Linux/Unix, you can use the stream editor (sed) tool to accomplish this.
For windows, Notepad++ (which is free) supports regex search and replace. Have a look at this link.
If it is just 30 assignments better do it manually. You can ensure that each index is correctly incremented by 1 in MATLAB by doing something like this:
#define BMI 1 /* BMI is BASE_MATLAB_INDEX */
y[0 + BMI] = ct[0 + BMI] - x[12 + BMI];
and so on...

Fibonacci sequence multiplying

I tried to make the fibonacci sequence with the following code:
def fibonacci(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a = 0
b = 1
the_list = []
while n > len(the_list):
the_list.append(a)
#By saying a = b and b = a+b we define the
#fibonacci sequence, since this is how the
#fibonacci sequence works.
a = b
b = a+b
print the_list
# Now call the function we just defined:
fibonacci(10)
As far as I know this code should do it but instead of giving me the fibonacci sequence its giving the following output:
[0, 1, 2, 4, 8, 16, 32, 64, 128, 256]
So my fibonacci sequence is multiplying instead of working correcly. I have no idea why because i thought
a = b
b = a+b
should do the trick, if i look at my while loop the statements for this loop are also correct, so I just dont get it why i dont get the right output.
So if someone could explain me why this code is not working it would be highly appriciated
Your code is creating an exponential sequence because of a logic flaw. Based on your code:
Start:
a = 0
b = 1
1st iteration:
a = b = 1
b = a + 1 = 1 + 1 = 2
2nd iteration:
a = b = 2
b = a + 2 = 2 + 2 = 4
As you can see the fact that you set a before performing the b calculation causes your issue.
Instead you need would something like (to prove the point):
tmp = a
a = b
b = tmp + a
A little extra math would eliminate the need for the extra variable:
b += a
a = b - a
But the easiest (and most pythonic) way would be:
a, b = b, a + b

Adding constraints for a known solution causes out of bounds exception

I have a linear optimization goal to Maximize EE+FF, where EE and FF each consist of some C and D.
With code I've written, I can get solver to find:
EE_quantity: 0, FF_quantity: 7
...but I know there to be another solution:
EE_quantity: 1, FF_quantity: 6
In order to validate user input for other valid solutions, I added a constraint for both EE and FF. So I added the EE_quantity == 0, FF_quantity == 7 in the code below, which is a runnable example:
SolverContext c2 = SolverContext.GetContext();
Model m2 = c2.CreateModel();
p.elements = elements_multilevel_productmix();
Decision C_quantity = new Decision(Domain.IntegerNonnegative, "C_quantity");
Decision D_quantity = new Decision(Domain.IntegerNonnegative, "D_quantity");
Decision EE_quantity = new Decision(Domain.IntegerNonnegative, "EE_quantity");
Decision FF_quantity = new Decision(Domain.IntegerNonnegative, "FF_quantity");
m2.AddDecisions(C_quantity, D_quantity, EE_quantity, FF_quantity);
m2.AddConstraints("production",
6 * C_quantity + 4 * D_quantity <= 100,
1 * C_quantity + 2 * D_quantity <= 200,
2 * EE_quantity + 1 * FF_quantity <= C_quantity,
1 * EE_quantity + 2 * FF_quantity <= D_quantity,
EE_quantity == 0,
FF_quantity == 7
);
m2.AddGoal("fixed_EE_FF", GoalKind.Maximize, "EE_quantity + FF_quantity");
Solution sol = c2.Solve(new SimplexDirective());
foreach (var item in sol.Decisions)
{
System.Diagnostics.Debug.WriteLine(
item.Name + ": " + item.GetDouble().ToString()
);
}
It seems that Solver Foundation really doesn't like this specific combination. Using EE_quantity == 1, FF_quantity == 6 is fine, as is using just EE_quantity == 0 or FF_quantity == 7. But using both, AND having one of them being zero, throws an exception:
Index was outside the bounds of the array.
What is going on under the hood, here? And how do I specify that I want to find "all" solutions for a specific problem?
(Note: no new releases of Solver Foundation are forthcoming - it's essentially been dropped by Microsoft.)
The stack trace indicates that this is a bug in the simplex solver's presolve routine. Unfortunately the SimplexDirective does not have a way to disable presolve (unlike InteriorPointDirective). Therefore the way to get around this problem is to specify the fixed variables differently.
Remove the last two constraints that set EE_quantity and FF_quantity, and instead set both the upper and lower bounds to be 0 and 7 respectively when you create the Decision objects. This is equivalent to what you wanted to express, but appears to avoid the MSF bug:
Decision EE_quantity = new Decision(Domain.IntegerRange(0, 0), "EE_quantity");
Decision FF_quantity = new Decision(Domain.IntegerRange(7, 7), "FF_quantity");
The MSF simplex solver, like many mixed integer solvers, only returns the optimal solution. If you want MSF to return all solutions, change to the constraint programming solver (ConstraintProgrammingDirective). If you review the documentation for Solution.GetNext() you should figure out how to do this.
Of course the CP solver is not guaranteed to produce the globally optimal solution immediately. But if you iterate through solutions long enough, you'll get there.

Math question in regards to functions in the form (1) / ( b ^ c )

I've found functions which follow the pattern of 1 / bc produce nice curves which can be coupled with interpolation functions really nicely.
The way I use the function is by treating 'c' as the changing value, i.e. the interpolation value between 0 and 1, while varying b for 'sharpness'. I use it to work out an interpolation value between 0 and 1, so generelly the function I use is as such:
float interpolationvalue = 1 - 1/pow(100,c);
linearinterpolate( val1, val2, interpolationvalue);
Up to this point I've been using a hacked approach to make it 'work' since when interpolation value = 1 the value is very close to but not quite 0.
So I was wondering, is there a function in the form of or one which can reproduce similar curves to the ones produced by 1 / bc where at c = 0 result = 1 and c = 1 result = 0.
Or even C = 0, result = 0 and C = 1 result = 1.
Thanks for any help!
For interpolation the approach offering the most flexibility is using splines, in your case quadratic splines would seem sufficient. The wikipedia page is math heavy, but you can find adapted desciptions on google.
1 - c ^ b with small values for b? Another option would be to use a cubic polynomial and specifying the slope at 0 and 1.
You could use a similar curve of the form A - 1 / b^(c + a), choosing values of A and a to match your constraints. So, for c = 0, result = 1:
1 = A - 1/b^a => A = 1 + 1/b^a
and for c = 1, result = 0:
0 = A - 1/b^(1+a) => A = 1/b^(1+a)
Combining these, we can find a in terms of b:
1 + 1/b^a = 1/b^(1+a)
b^(1+a) + b = 1
b * (b^a - 1) = 1
b^a = 1/b - 1
So:
a = log_b(1/b - 1) = log(1/b - 1) / log(b)
A = 1 + 1/b^a = 1 / (1-b)
In real numbers, the ones that mathematician use, no function of the form you specify is ever going to return 0, division can't do that. (1/x)==0 has no real solutions. In floating point arithmetic, the poor relation of real arithmetic that computers use, you could write 1/(MAX_FP_VALUE^1) which will give you as close to 0 as you are ever going to get (actually, it might give you a NaN or one of the other odd returns that IEEE 754 allows).
And, as I'm sure you've noticed, 1/(b^0) always returns 1 since b^0 is, by definition of 0-th power, always 1.
So, no function with c = 0 will produce a result of 0.
For c = 1, result = 1, set b = 1
But I guess this is only a partial answer, I'm not terribly sure I understand what you are trying to do.
Regards
Mark