Choco-solver: coefficients of the variables are real numbers - choco

I am looking for a way to encode mathematical equations on Choco Solver. I see there's a way to encode constraints like:
3x + 4y < 9
But I am trying to encode something like
3.5*x + 4.3*y < 9.3
where x and y are int vars and coefficients are real number.

If you need reals, you need to install Ibex solver (http://www.ibex-lib.org/) that is used as a library to handle reals in choco (see specific installation http://www.ibex-lib.org/doc/java-install.html)
Best,
Jean-Guillaume Fages
https://www.cosling.com/

Related

python hack converting floats to decimals

I've written a large program, with dependencies on libraries written in my lab. I'm getting wrong (and somewhat random) results, which are caused by floating-point errors.
I would like to do some python magic and change all floats to decimals, or some other more precise type.
I can't write the full code here, but following is the general flow -
def run(n):
...
x = 0.5 # initializes as float
for _ in range(n):
x = calc(x)
...
return x
What I'm trying to avoid is to go over all initialization in the code and add a manual cast to decimal.
Is there a trick I can do to make python initialize all floats in lines such as x = 0.5 as decimals? or perhaps use a custom interpreter which has more exact floats?
Thanks,
I can't post the full code, hope my edit makes it clearer.
I think you can use this:
from decimal import Decimal
Decimal(variable)

Is there a way in vyper to calculate e^x with x being a signed decimal?

For my thesis, I have been looking for an equation that calculates an exp(x) with Vyper smart contract. I choose Vyper over Solidity for its ability to handle fixed-point numbers. However, I couldn't find an efficient to do this since Vyper doesn't allow me to do exponentiation with a decimal base.
What I was trying to do is e**x
(e = constant Euler number = about 2.718281828. x is the variable of a decimal that can be negative or positive)
I tried looking for a Vyper math library that provides exponentiation function for decimals but with no luck. Someone somewhere suggested using look-up tables for exponential e to minimize computation time. However, I have no idea how can I implement that in Vyper.
I'm currently trying to develop an exponential function based on Taylor's series, https://en.wikipedia.org/wiki/Taylor_series.
Is this the only way to calculate this problem? I feel like there could be a better solution.
What I have is a solution, but it is not perfect, and I haven't been able to get it to run reliably in Vyper yet. It might be an avenue that you can explore for your thesis though. What I had was based on this Stackoverflow answer:
https://ethereum.stackexchange.com/a/65854
My answer involves multiplying e by a scale factor and doing the exponentiation on that. The scale factor is the number of decimals that you want for precision. After the exponentiation is done on the scaled number, you then divide the result by a divisor scaled by an exponent. The numbers get large rather quickly, but this is the code I have (NOTE: code is in Python rather than Vyper):
import math
SCALE = 10
EXP = 3
eScaled = math.e * (10 ** SCALE)
eScaleDiv = (10 ** SCALE) ** EXP
ePowExpScaled = eScaled ** EXP
print(ePowExpScaled / eScaleDiv)
print(math.e ** EXP)

Python Gekko: Increasing value of objective_convergence_tolerance for Solver=2 (BPOPT)

Is there a way to increase the value of objective_convergence_tolerance for Solver=2 (BPOPT)?
It seems to be working only for Solver = 1 and 2.
Thanks.
There are 4 parts that are checked for convergence including:
max(abs(g + a^T * lam - zL + zU))/s_d
max(abs(c))
max(abs(diag[x-xL s-sL]*zL - mu))/s_c
max(abs(diag[xU-x sU-s]*zU - mu))/s_c
The maximum of these 4 parts must be less than the convergence tolerance. Right now there is no separate objective function convergence criterion. Gekko has 3 solvers that are included with the publicly available version including:
APOPT
BPOPT
IPOPT
The BPOPT solver isn't typically one one of the best solvers if you'd like to try m.options.SOLVER=1 (APOPT) or m.options.SOLVER=3 (IPOPT).

(in excel) randomly generating a power law distribution

I am trying to simulate a number of different distribution types for a project using Excel. Right now, I have generated a normal distribution with a mean of 35 and a standard deviation of 3.33. So far so good.
I would like to also generate some other distribution types.
One I have tried is a lognormal. To get that, I am using the following code:
=(LOGNORM.INV(RAND(),LN(45^2/SQRT(45^2+3.33^2)),SQRT(LN((45^2+3.33^2)/4.5^2))
It produces some output, but I would welcome anyone's input on the syntax.
What I really want to try to do is a power law distribution. From what I can tell, Excel does not have a built-in function to randomly generate this data. Does anyone know of a way to do it, besides switching software packages?
Thanks for any help you can provide.
E
For the (type I) Pareto distribution, if the parameters are a min value xm and an exponent alpha then the cdf is given by
p = 1 - (xm/x)^alpha
This gives the probability, p, that the random variable takes on a value which is <= x. This is easy to invert, so you can use inverse sampling to generate random variables which follow that distribution:
x = xm/(1-p)^(1/alpha) = xm*(1-p)^(-1/alpha)
If p is uniform over [0,1] then so is 1-p, so in the above you can just use RAND() to simulate 1/p. Thus, in Excel if you wanted to e.g. simulate a type-1 Pareto distribution with xm = 2 and alpha = 3, you would use the formula:
= 2 * RAND()^(-1/3)
If you are going to be doing this sort of thing a lot with different distributions, you might want to consider using R, which can be called directly from Excel using the REXcel add-in. R has a very large number of built-in distributions that it can directly sample from (and it also uses a better underlying random number generator than Excel does).

Generating Normally distributed Random Numbers without decimal in excel

I am trying to get random numbers that are normally distributed with a mean of 20 and standard deviation of 2 for a sample size of 225 in Excel but I am getting numbers with decimals ( like 17.5642 , 16.337).
if I round it off, normal distribution cant be achieved. Please help me to get round figures that are normally distributed too....I used the Excel FORMULA "* =NORMINV(RAND(),20,2) *" for generating those numbers. Please suggest to get round figures.
As #circular-ruin has observed, what you are asking for strictly speaking doesn't make sense.
But -- perhaps you can run the Central Limit Theorem backwards. CLT is often used to approximate discrete distributions by normal distributions. You can use it to approximate a normal distribution by a discrete distribution.
If X is binomial with parameters p and n, then it is a standard result that the mean of X is np and the variance of X is np(1-p). Elementary algebra yields that such an X has mean 20 and variance 4 (hence standard deviation 2) if and only if n = 25 and p = 0.8. Thus -- if you simulate a bin(25,0.8) random variable you will get integer values which will be approximately N(20,4). This seems a little more principled then simulating N(20,4) directly and then just rounding. It still isn't normal -- but you really need to drop that requirement if you want your values to be integers.
To simulate a bin(25,0.8) random variable in Excel, just use the formula
=BINOM.INV(25,0.8,RAND())
with just 225 observations the results would probably pass a Chi-squared goodness of fit test for N(20,4) (though the right tail would be under-represented).

Resources