mathematical-optimization

Finding all divisors of a number optimization

拥有回忆 提交于 2019-11-27 15:25:49
问题 I have written the following function which finds all divisors of a given natural number and returns them as a list: def FindAllDivisors(x): divList = [] y = 1 while y <= math.sqrt(x): if x % y == 0: divList.append(y) divList.append(int(x / y)) y += 1 return divList It works really well with the exception that it's really slow when the input is say an 18-digit number. Do you have any suggestions for how I can speed it up? Update : I have the following method to check for primality based on

Which algorithm for assigning shifts (discrete optimization problem)

对着背影说爱祢 提交于 2019-11-27 14:46:48
问题 I'm developing an application that optimally assigns shifts to nurses in a hospital. I believe this is a linear programming problem with discrete variables, and therefore probably NP-hard: For each day, each nurse (ca. 15-20) is assigned a shift There is a small number (ca. 6) of different shifts There is a considerable number of constraints and optimization criteria, either concerning a day, or concerning an emplyoee, e.g.: There must be a minimum number of people assigned to each shift

Python constrained non-linear optimization

自作多情 提交于 2019-11-27 13:48:38
问题 What's the recommended package for constrained non-linear optimization in python ? The specific problem I'm trying to solve is this: I have an unknown X (Nx1), I have M (Nx1) u vectors and M (NxN) s matrices. max [5th percentile of (ui_T*X), i in 1 to M] st 0<=X<=1 and [95th percentile of (X_T*si*X), i in 1 to M]<= constant When I started out the problem I only had one point estimate for u and s and I was able to solve the problem above with cvxpy . I realized that instead of one estimate for

Find local maxima in grayscale image using OpenCV

大憨熊 提交于 2019-11-27 12:27:21
Does anybody know how to find the local maxima in a grayscale IPL_DEPTH_8U image using OpenCV? HarrisCorner mentions something like that but I'm actually not interested in corners ... Thanks! I think you want to use the MinMaxLoc(arr, mask=NULL)-> (minVal, maxVal, minLoc, maxLoc) Finds global minimum and maximum in array or subarray function on you image A pixel is considered a local maximum if it is equal to the maximum value in a 'local' neighborhood. The function below captures this property in two lines of code. To deal with pixels on 'plateaus' (value equal to their neighborhood) one can

Given a target sum and a set of integers, find the closest subset of numbers that add to that target

元气小坏坏 提交于 2019-11-27 12:22:38
问题 I have a set of integers M and a target sum k. I want to find the subset of M that when added together is the closest to k without going over. For example: M = {1, 3, 5, 5, 14} k = 12 answer = {1, 5, 5} because 1 + 5 + 5 = 11 and there is no way to make 12. I have the additional constraint that the subset can contain at most 4 elements. In my application, the size of |M| can be large (on the order of thousands of elements). If it is not possible to find the optimal answer in a reasonable time

scipy.optimize.leastsq with bound constraints

微笑、不失礼 提交于 2019-11-27 12:07:04
I am looking for an optimisation routine within scipy/numpy which could solve a non-linear least-squares type problem (e.g., fitting a parametric function to a large dataset) but including bounds and constraints (e.g. minima and maxima for the parameters to be optimised). At the moment I am using the python version of mpfit (translated from idl...): this is clearly not optimal although it works very well. An efficient routine in python/scipy/etc could be great to have ! Any input is very welcome here :-) thanks! scipy.optimize.least_squares in scipy 0.17 (January 2016) handles bounds; use that

Quadratic Program (QP) Solver that only depends on NumPy/SciPy?

天涯浪子 提交于 2019-11-27 09:51:36
问题 I would like students to solve a quadratic program in an assignment without them having to install extra software like cvxopt etc. Is there a python implementation available that only depends on NumPy/SciPy? 回答1: I ran across a good solution and wanted to get it out there. There is a python implementation of LOQO in the ELEFANT machine learning toolkit out of NICTA (http://elefant.forge.nicta.com.au as of this posting). Have a look at optimization.intpointsolver. This was coded by Alex Smola,

Solving an integer linear program: why are solvers claiming a solvable instance is infeasible?

半城伤御伤魂 提交于 2019-11-27 09:15:58
I'm trying to solve integer programming problems. I've tried both use SCIP and LPSolve For example, given the final values of A and B, I want to solve for valA in the following C# code: Int32 a = 0, b = 0; a = a*-6 + b + 0x74FA - valA; b = b/3 + a + 0x81BE - valA; a = a*-6 + b + 0x74FA - valA; b = b/3 + a + 0x81BE - valA; // a == -86561, b == -32299 Which I implemented as this integer program in lp format (the truncating division causes a few complications): min: ; +valA >= 0; +valA < 92; remAA_sign >= 0; remAA_sign <= 1; remAA <= 2; remAA >= -2; remAA +2 remAA_sign >= 0; remAA +2 remAA_sign <

mathematical optimization library for Java — free or open source recommendations? [closed]

自作多情 提交于 2019-11-27 07:29:05
Does anyone know of such a library that performs mathematical optimization (linear programming, convex optimization, or more general types of problems)? I'm looking for something like MATLAB, but with the ability to handle larger problems. Do I have to write my own implementations, or buy one of those commercial products (CPLEX and the like)? A good answer is dependent on what you mean by "convex" and "more general" If you are trying to solve large or challenging linear or convex-quadratic optimization problems (especially with a discrete component to them), then it's hard to beat the main

Which is better way to calculate nCr

爱⌒轻易说出口 提交于 2019-11-27 06:49:30
Approach 1: C(n,r) = n!/(n-r)!r! Approach 2: In the book Combinatorial Algorithms by wilf , i have found this: C(n,r) can be written as C(n-1,r) + C(n-1,r-1) . e.g. C(7,4) = C(6,4) + C(6,3) = C(5,4) + C(5,3) + C(5,3) + C(5,2) . . . . . . . . After solving = C(4,4) + C(4,1) + 3*C(3,3) + 3*C(3,1) + 6*C(2,1) + 6*C(2,2) As you can see, the final solution doesn't need any multiplication. In every form C(n,r), either n==r or r==1. Here is the sample code i have implemented: int foo(int n,int r) { if(n==r) return 1; if(r==1) return n; return foo(n-1,r) + foo(n-1,r-1); } See output here. In the