mathematical-optimization

how to find global minimum in python optimization with bounds?

妖精的绣舞 提交于 2019-12-17 15:37:25
问题 I have a Python function with 64 variables, and I tried to optimise it using L-BFGS-B method in the minimise function, however this method have quite a strong dependence on the initial guess, and failed to find the global minimum. But I liked its ability to set bounds for the variables. Is there a way/function to find the global minimum while having boundaries for the variables ? 回答1: This can be done with scipy.optimize.basinhopping . Basinhopping is a function designed to find the global

What is an NP-complete in computer science?

百般思念 提交于 2019-12-17 15:05:25
问题 What is an NP-complete problem? Why is it such an important topic in computer science? 回答1: NP stands for Non-deterministic Polynomial time. This means that the problem can be solved in Polynomial time using a Non-deterministic Turing machine (like a regular Turing machine but also including a non-deterministic "choice" function). Basically, a solution has to be testable in poly time. If that's the case, and a known NP problem can be solved using the given problem with modified input (an NP

scipy.optimize.leastsq with bound constraints

流过昼夜 提交于 2019-12-17 10:38:07
问题 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

How to concatenate two integers in C

人盡茶涼 提交于 2019-12-17 07:40:50
问题 Stack Overflow has this question answered in many other languages, but not C. So I thought I'd ask, since I have the same issue. How does one concatenate two integers in C? Example: x = 11; y = 11; I would like z as follows: z = 1111; Other examples attempt to do this with strings. What is a way to do this without strings? I'm looking for an efficient way to do this in C because in my particular usage, this is going into a time critical part of code. Thanks in Advance! 回答1: unsigned

Linear Programs using R

本秂侑毒 提交于 2019-12-14 02:29:06
问题 How can we solve a linear program using R? I want to solve the following example: min -a -2b +4c Constraints a + b + s1 = 5 a + 3c -s2 = 10 2b - 3c = 20 a >= 0, b >= 0, c >= 0, s1 >= 0, s2 >= 0 The equations might not make total sense. I just need to know the syntax of writing these equations in R. I might write something like this for the above equations require(lpSolve) R.obj <- c(-1,-2,4) R.con <- matrix(c(1,1,1,1,3,-1,2,-3),nrow=3,byrow=TRUE) R.dir <- c("=","=","=") R.rhs <- c(5,10,20) lp

Find threshold value where f(x)=0

核能气质少年 提交于 2019-12-14 02:17:45
问题 I have a function f(x) , that is positive and decreasing for x<c , and is zero for all x>=c . How can I find c , the threshold where the function hits zero (to within a tolerance)? Here's an example: zer = function(x){ ifelse(x>5, rep(0,length(x)), 5 - x) } > x=-5:15 > plot(x,zer(x)) You can use uniroot to find where a function crosses zero, but that relies on the function being negative and positive on either side of the crossing, so I can't use that here. In the above case it evaluates zer

How to improve the efficiency of c++ code (find the largest prime factor)? [closed]

*爱你&永不变心* 提交于 2019-12-13 23:51:23
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . How to improve the efficiency of this code? Suppose you have to deal with really big inputs. ‪#‎include‬ <iostream> using namespace std; int main() { //This program finds out the largest prime factor of given input long int n,big=1; cin>>n; for (long int i=2;i<n;i=i+2) { if (n%i==0) big=i; } cout<<big; return 0;

Error in using optim to maximise the likelihood in r

。_饼干妹妹 提交于 2019-12-13 21:14:54
问题 So, I have these functions: funk1 <- function(a,x,l,r) { x^2*exp(-(l*(1-exp(-r*a))/r))} funk2 <- function(x,l,r) { sapply(x, function (s) { integrate(funk1, lower = 0, upper = s, x=s, l=l, r=r)$value })} which are used to explain the data y in, z <- data.frame(ts = 1:100, y = funk2(1:100, l = 1, r = 1) + rpois(100, 1:100)) I wish to use optim to maximise the likelihood, so I defined a likelihood function: LL_funk <- function(l,r) { n=nrow(z) R = sum((funk2(ts,l,r) - y)^2) logl = -((n/2)*log(R

Generate all neighbors for 2OPT in python

不羁的心 提交于 2019-12-13 19:30:28
问题 I am trying to implement to the 2opt optimization algorithm for the undirected Traveling Salesman Problem. For given cities: cities = [[ 72.06557466, 5.73765812], [ 94.50272578, 68.95162393], [ 58.53952609, 15.12518299], [ 94.64599891, 34.65906808], [ 62.42311036, 45.8430048 ], [ 24.73697454, 4.4159545 ], [ 15.71071819, 81.27575127], [ 65.65743227, 54.90239983], [ 5.07828178, 47.34845887], [ 88.98592652, 48.14959719]] My understanding for the general algorithm is that starting from a random

AMPL variable size set iteration

て烟熏妆下的殇ゞ 提交于 2019-12-13 18:11:50
问题 I'm learning AMPL so that I can use it some time later in my programs. I have a small question though that I couldn't find its answer yet. Suppose I have a set , this set will contain some subsets, those subsets may differ in their dimensions. For example: set x:= (a,b,c) (a,c) (d,t,r,e,s); and so on .. Now I want to write a constraint which will have to deal with all elements in each subset (similar to a loop that will iterate between a , b , and c in the first subset, a and c in the second