main
Jan-Niclas Loosen 8 months ago
parent b3ec1aa25c
commit 895855ea61

@ -5,8 +5,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="42ed9a51-9564-48c5-b5fa-035301b1578d" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/ext_01/control.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/ha_09/loosen_janniclas_1540907_10.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ext_01/control.py" beforeDir="false" afterPath="$PROJECT_DIR$/ext_01/control.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -40,7 +41,7 @@
<recent name="C:\Universitaet\UNI_Python\ueb_01" />
</key>
</component>
<component name="RunManager" selected="Python.control">
<component name="RunManager" selected="Python.loosen_janniclas_1540907_10">
<configuration name="control" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="UNI_Python" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -107,8 +108,31 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="loosen_janniclas_1540907_10" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="UNI_Python" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ha_09" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/ha_09/loosen_janniclas_1540907_10.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.loosen_janniclas_1540907_10" />
<item itemvalue="Python.control" />
<item itemvalue="Python.loosen_janniclas_1540907_09 (1)" />
<item itemvalue="Python.loosen_janniclas_1540907_09" />
@ -130,6 +154,11 @@
<workItem from="1705237047116" duration="41000" />
<workItem from="1705237636842" duration="2000" />
<workItem from="1705237645075" duration="7202000" />
<workItem from="1705245334895" duration="171000" />
<workItem from="1705398553017" duration="3666000" />
<workItem from="1705414770423" duration="3134000" />
<workItem from="1705419658987" duration="941000" />
<workItem from="1705431087533" duration="5323000" />
</task>
<servers />
</component>
@ -138,7 +167,8 @@
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/UNI_Python$loosen_janniclas_1540907_09__1_.coverage" NAME="loosen_janniclas_1540907_09 (1) Coverage Results" MODIFIED="1704717881190" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_08" />
<SUITE FILE_PATH="coverage/UNI_Python$control.coverage" NAME="control Coverage Results" MODIFIED="1705244854276" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ext_01" />
<SUITE FILE_PATH="coverage/UNI_Python$loosen_janniclas_1540907_10.coverage" NAME="loosen_janniclas_1540907_10 Coverage Results" MODIFIED="1705436313201" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_09" />
<SUITE FILE_PATH="coverage/UNI_Python$control.coverage" NAME="control Coverage Results" MODIFIED="1705421841538" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ext_01" />
<SUITE FILE_PATH="coverage/UNI_Python$loosen_janniclas_1540907_09.coverage" NAME="loosen_janniclas_1540907_09 Coverage Results" MODIFIED="1704711219437" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/ha_08" />
</component>
</project>

@ -48,3 +48,25 @@ for n in numbers:
counter += 1
print(f"|F| = {counter}")
# Ausgabe: |F| = 30240
def contains_twin_pair(combo):
"""Check if the combination contains at least one twin pair."""
for x in range(len(combo)):
for y in range(len(combo)):
if abs(combo[x] % 49 - combo[y] % 49) == 1:
return True
return False
# Generate all combinations of 6 numbers out of 49
numbers = list(range(1, 50))
all_combinations = list(itertools.combinations(numbers, 6))
# Filter combinations with and without twin pairs
combinations_with_twin = [combo for combo in all_combinations if contains_twin_pair(combo)]
combinations_without_twin = [combo for combo in all_combinations if not contains_twin_pair(combo)]
# Output the count of combinations
print(len(all_combinations))
print(f"Number of combinations with at least one twin pair: {len(combinations_with_twin)}")
print(f"Number of combinations without any twin pairs: {len(combinations_without_twin)}")
print(len(combinations_with_twin)/len(all_combinations))

@ -0,0 +1,142 @@
import random
from io import BytesIO
import numpy as np
import multiprocessing
import requests
from mpi4py import MPI
from PIL import Image
def count_randomized_hits(iterations):
counter = 0
for i in range(iterations):
x = random.uniform(0, 1)
y = random.uniform(0, 1)
if ((x ** 2) + (y ** 2)) ** (1 / 2) <= 1:
counter += 1
return counter
FLAG_default = 0
FLAG_threaded = 1
FLAG_network = 2
def monte_carlo_methode(n, mode=0):
if mode == 1: # Multithreading mode
num_threads = 16
iterations_per_thread = n // num_threads
with multiprocessing.Pool(num_threads) as pool:
hits = pool.map(count_randomized_hits, [iterations_per_thread] * num_threads)
hit = sum(hits)
elif mode == 2: # MPI parallel mode
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
local_hit = count_randomized_hits(n // size)
hit = comm.reduce(local_hit, op=MPI.SUM, root=0)
else: # Default mode
hit = count_randomized_hits(n)
pi_approx = (hit / n) * 4
pi_diff = abs(np.pi - pi_approx)
return pi_approx, pi_diff
def uniform_kernel(n):
if n % 2 == 0:
print("Size needs to be odd")
exit(1)
K = 1 / n / n * np.ones([n, n])
return K
def gauss_kernel(s):
n = 3 * s
pos = np.arange(-n, n + 1)
x = np.meshgrid(pos, pos)
K = 1.0 / (2.0 * np.pi * s * s) * np.exp(-(x[0] ** 2 + x[1] ** 2) / (2.0 * s * s))
K = K / sum(sum(K))
return K
FLAG_gauss = 0
FLAG_uniform = 1
def process_image_part(data_part, kernel):
y_part_size, x_part_size, _ = data_part.shape
data_part_new = np.zeros_like(data_part)
for i in range((kernel.shape[0] - 1) // 2, y_part_size - (kernel.shape[0] - 1) // 2):
for j in range((kernel.shape[1] - 1) // 2, x_part_size - (kernel.shape[1] - 1) // 2):
for k in range(3):
new_value = 0.0
for ii in range(kernel.shape[0]):
for jj in range(kernel.shape[1]):
iii = ii - (kernel.shape[0] - 1) // 2
jjj = jj - (kernel.shape[1] - 1) // 2
new_value += kernel[ii, jj] * data_part[i + iii, j + jjj, k]
data_part_new[i, j, k] = new_value
return data_part_new
def process_image(img, func=0, mode=0):
if isinstance(img, str):
img = Image.open(img)
if img.mode == "P":
img = img.convert(mode="RGB")
data = np.asarray(img, dtype=np.float64) / 255.0
if func == 1:
kernel = uniform_kernel(3)
else:
kernel = gauss_kernel(3)
if mode == 1: # Multithreading mode
num_threads = 16
data_parts = np.array_split(data, num_threads, axis=0)
with multiprocessing.Pool(num_threads) as pool:
data_new_parts = pool.starmap(process_image_part, zip(data_parts, [kernel]*num_threads))
data_new = np.concatenate(data_new_parts, axis=0)
elif mode == 2: # MPI parallel mode
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
data_part = np.array_split(data, size, axis=0)[rank]
data_new_part = process_image_part(data_part, kernel)
data_new_parts = comm.gather(data_new_part, root=0)
if rank == 0:
data_new = np.concatenate(data_new_parts, axis=0)
else:
data_new = None
data_new = comm.bcast(data_new, root=0)
else: # Default mode
data_new = process_image_part(data, kernel)
data_new = data_new * 255.0
data_new = np.uint8(data_new)
return Image.fromarray(data_new, mode="RGB")
if __name__ == '__main__':
print(monte_carlo_methode(1000, FLAG_default))
print(monte_carlo_methode(1000, FLAG_threaded))
print(monte_carlo_methode(1000, FLAG_network))
url = "https://i.wfcdn.de/teaser/660/27020.jpg"
response = requests.get(url)
if response.status_code == 200:
image = Image.open(BytesIO(response.content))
image = process_image(image, FLAG_uniform, FLAG_threaded)
image.show()
Loading…
Cancel
Save