optimization

Scipy.optimize terminates successfully for infeasible NLP

假如想象 提交于 2020-01-23 11:26:59
问题 Tried solving an NLP using the scipy.optimize SLSQP. The problem is clearly infeasible but the minimize function in scipy.optimize seems to disagree. minimize X^2 + Y^2 subject to X + Y = 11 X, Y >= 6 The code: from scipy.optimize import minimize def obj(varx): return varx[1]**2 + varx[0]**2 def constr1(varx): constr1 = -varx[0]-varx[1]+11 return constr1 bnds = [(6,float('Inf')),(6,float('Inf'))] ops = ({'maxiter':100000, 'disp':'bool'}) cons = ({'type':'eq', 'fun':constr1}) res = minimize

Scipy.optimize terminates successfully for infeasible NLP

心不动则不痛 提交于 2020-01-23 11:26:12
问题 Tried solving an NLP using the scipy.optimize SLSQP. The problem is clearly infeasible but the minimize function in scipy.optimize seems to disagree. minimize X^2 + Y^2 subject to X + Y = 11 X, Y >= 6 The code: from scipy.optimize import minimize def obj(varx): return varx[1]**2 + varx[0]**2 def constr1(varx): constr1 = -varx[0]-varx[1]+11 return constr1 bnds = [(6,float('Inf')),(6,float('Inf'))] ops = ({'maxiter':100000, 'disp':'bool'}) cons = ({'type':'eq', 'fun':constr1}) res = minimize

Optimize Haskell code calculating the sum of all the primes below two million

倖福魔咒の 提交于 2020-01-23 09:20:22
问题 Problem 10 in Project Euler. I saw some discussion there but only for C. I used the following code to calculate: print . sum . sieve $ [2..2000000] where sieve [] = [] sieve (x:xs) = x : sieve (filter ((/= 0) . (`mod` x)) xs) It takes ages to calculate. I am wondering if there is any more efficient way to calculate it? 回答1: Many really fast ways of calculating prime numbers in haskell is described on the haskellwiki page for Prime numbers. Specifically the second one seems to be good enough,

Call method once to set multiple fields in Django Rest Framework serializer

别来无恙 提交于 2020-01-23 06:44:12
问题 How can I call the same method one time to set multiple fields with a Django Rest Framework serializer? This is what I do now but this clearly calls the method two times. How can I limit it to only be called once? class MyModel(models.Model): def GetTwoValues(self): foo = [] bar = [] # expensive operation return foo, bar class MyModelSerializer(serializers.HyperlinkedModelSerializer): foo = serializers.SerializerMethodField() bar = serializers.SerializerMethodField() def get_foo(self, obj):

Call method once to set multiple fields in Django Rest Framework serializer

偶尔善良 提交于 2020-01-23 06:40:27
问题 How can I call the same method one time to set multiple fields with a Django Rest Framework serializer? This is what I do now but this clearly calls the method two times. How can I limit it to only be called once? class MyModel(models.Model): def GetTwoValues(self): foo = [] bar = [] # expensive operation return foo, bar class MyModelSerializer(serializers.HyperlinkedModelSerializer): foo = serializers.SerializerMethodField() bar = serializers.SerializerMethodField() def get_foo(self, obj):

One function with different arguments to push certain bits of an input integer to the left

旧时模样 提交于 2020-01-23 03:59:08
问题 This question is related to this. Let x be an 8 bit integer. I will number bits from left to right because that's how we read. If I want to get the third and fifth bits and put them in the first and second bits, with everything else as zero, I can have f(x) = (5*x) & 0b11000000 . More concisely: 00a0b000 -> ab000000 | f_0b00101000(x) = (5*x) & 0b11000000 However if I want the fifth, sixth and eighth bits to be in the first three bits, f(x) is different: 000ab0cd -> abcd0000 | f_0b00011011(x)

Why am I getting a StackOverflow error in a long method?

匆匆过客 提交于 2020-01-23 01:05:09
问题 I have a small read-only database (based on data from services). The load time is critical so data can't be extracted from DB, XML and so on. I use the direct construction of data in the generated C# file: void Load(){ var aa = new A[ 100500 ]; aa[0] = new A( ... ); ... aa[100499] = new A( ... ); AA = aa; } There are no recursion here, stackallocks and so on. But I have got a StackOverflow error. Looking in Disassembler window I found that JIT converts this code into: var aa = new A[ 100500 ]

Why is the native String getBytes method slower than the custom implemented getBytesFast?

安稳与你 提交于 2020-01-22 20:18:56
问题 When running the following piece of code, the execution of the Java String's native method getBytes() seems to be slower than the custom getBytesFast() implementation. You can use the Arrays.equals(str.getBytes(), getBytesFast(str)) to verify that both byte arrays are equals. The getBytesFast implementation is a modified version of the implementation included in this programming tips article (1997): http://java.sun.com/developer/technicalArticles/Programming/Performance/ I'm looking for a

How can I speed this loop up? Is there a class for replacing multiple terms at at time?

时光总嘲笑我的痴心妄想 提交于 2020-01-22 19:50:27
问题 The loop: var pattern = _dict[key]; string before; do { before = pattern; foreach (var pair in _dict) if (key != pair.Key) pattern = pattern.Replace(string.Concat("{", pair.Key, "}"), string.Concat("(", pair.Value, ")")); } while (pattern != before); return pattern; It just does a repeated find-and-replace on a bunch of keys. The dictionary is just <string,string> . I can see 2 improvements to this. Every time we do pattern.Replace it searches from the beginning of the string again. It would

Why is C++ executable running so much faster when linked against newer libstdc++.so?

陌路散爱 提交于 2020-01-22 15:35:55
问题 I have a project (code here) in which I run benchmarks to compare the performance of different methods for computing dot product (Naive method, Eigen library, SIMD implementation, ect). I am testing on a fresh Centos 7.6 VM. I have noticed that when I use different versions of libstdc++.so.6 , I get significantly different performance. When I spin up a new Centos 7.6 instance, the default C++ standard library is libstdc++.so.6.0.19 . When I run my benchmark executable (linked against this