OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Scipy Optimizing with Constraints

  • Thread starter Thread starter user25640830
  • Start date Start date
U

user25640830

Guest
I am trying to find the values for my x where the sum of the columns of my data_matrix matches the intial_sums.

However the results are: Variable 0: 1.0 Variable 1: 1.0 Final sums: [0. 4. 0. 2. 0.] Initial sums (should match final sums): [0 2 0 1 0]

Where the final sums are not correct at all.

Code:
import numpy as np
from scipy.optimize import minimize

Example data for optimization:

Code:
data_matrix = np.array([
[-1114.75839, 0.0, 2.0, 0.0, 1.0, 0.0],
[+325.115366, 0.0, 2.0, 0.0, 1.0, 0.0]
])

Initial sums for constraints:

Code:
initial_sum = np.array([0, 2, 0, 1, 0])

Code:
def objective(x):
    return np.dot(data_matrix[:, 0], x)

def constraint_1(x):
    return np.dot(data_matrix[:, 2], x) - initial_sum[1]
 
def constraint_2(x):
    return np.dot(data_matrix[:, 4], x) - initial_sum[3]
 
cons = [
    {'type': 'eq', 'fun': constraint_1},
    {'type': 'eq', 'fun': constraint_2}
]
 
bounds = [(0, None) for _ in range(len(data_matrix))]

Running the optimization:

Code:
result = minimize(objective, np.ones(len(data_matrix)), method='SLSQP', bounds=bounds, constraints=cons, tol=1e-6)
 
optimized_x = np.where(np.abs(result.x) < 1e-5, 0, result.x)
optimized_values = optimized_x.tolist()

Output results

Code:
for i in range(len(optimized_values)):
    print(f'Variable {i}: {optimized_values[i]}')

final_sums = np.dot(data_matrix[:, 1:6].T, optimized_x)
print(f'Final sums: {final_sums}')
print(f'Initial sums (should match final sums): {initial_sum}')`

I tried it with and without bounds and tried to play with the constraints.
<p>I am trying to find the values for my x where the sum of the columns of my data_matrix matches the intial_sums.</p>
<p>However the results are:<code> Variable 0: 1.0 Variable 1: 1.0 Final sums: [0. 4. 0. 2. 0.] Initial sums (should match final sums): [0 2 0 1 0]</code></p>
<p>Where the final sums are not correct at all.</p>
<pre><code>import numpy as np
from scipy.optimize import minimize
</code></pre>
<p>Example data for optimization:</p>
<pre><code>data_matrix = np.array([
[-1114.75839, 0.0, 2.0, 0.0, 1.0, 0.0],
[+325.115366, 0.0, 2.0, 0.0, 1.0, 0.0]
])
</code></pre>
<p>Initial sums for constraints:</p>
<pre><code>initial_sum = np.array([0, 2, 0, 1, 0])
</code></pre>
<pre><code>def objective(x):
return np.dot(data_matrix[:, 0], x)

def constraint_1(x):
return np.dot(data_matrix[:, 2], x) - initial_sum[1]

def constraint_2(x):
return np.dot(data_matrix[:, 4], x) - initial_sum[3]

cons = [
{'type': 'eq', 'fun': constraint_1},
{'type': 'eq', 'fun': constraint_2}
]

bounds = [(0, None) for _ in range(len(data_matrix))]
</code></pre>
<p>Running the optimization:</p>
<pre><code>result = minimize(objective, np.ones(len(data_matrix)), method='SLSQP', bounds=bounds, constraints=cons, tol=1e-6)

optimized_x = np.where(np.abs(result.x) < 1e-5, 0, result.x)
optimized_values = optimized_x.tolist()
</code></pre>
<p>Output results</p>
<pre><code>for i in range(len(optimized_values)):
print(f'Variable {i}: {optimized_values}')

final_sums = np.dot(data_matrix[:, 1:6].T, optimized_x)
print(f'Final sums: {final_sums}')
print(f'Initial sums (should match final sums): {initial_sum}')`
</code></pre>
<p>I tried it with and without bounds and tried to play with the constraints.</p>
 

Latest posts

H
Replies
0
Views
1
Hür Doğan ÜNLÜ
H
Top