def test_calculate_performance_perfect_bridge(): density_goal = 100 products_goal = 5 particle_range = (1.01, 100.0) performance = Optimizer( bridge=theoretical_bridge(BridgeOption.PERMEABILITY, 500), max_products=products_goal, density_goal=density_goal, particle_range=particle_range, ).calculate_performance( theoretical_bridge(BridgeOption.PERMEABILITY, 500), products_result) print(performance)
def bridge_plot(products: dict, mode, value) -> str: bridge = theoretical_bridge(mode, value) ( fig, bridgeplot, ) = plt.subplots() bridgeplot.set_title("Bridge") bridgeplot.set_xscale("log") bridgeplot.set_xlabel("Particle Size μm") bridgeplot.set_ylabel("Accumulated volume %") bridgeplot.set_xticks([0.1, 1, 10, 100, 1000]) bridgeplot.xaxis.set_major_formatter(ScalarFormatter()) bridgeplot.plot(SIZE_STEPS, bridge, color="black", label="Ideal") bridgeplot.axes.set_ylim([0, 100]) bridgeplot.axes.set_xlim([0.01, 10000]) products_class = [] all_products = products_get() for id, values in products.items(): products_class.append( Product(product_id=id, share=(values["percentage"] / 100), cumulative=all_products[id]["cumulative"])) bridgeplot.plot(SIZE_STEPS, calculate_blend_cumulative(products_class), color="red", label="Blend") bridgeplot.legend() tmpfile = BytesIO() fig.savefig(tmpfile, format="png") encoded = base64.b64encode(tmpfile.getvalue()).decode("utf-8") return encoded
def optimizer_request_handler( value, blend_name, products, density_goal, volume, option="AVERAGE_PORESIZE", iterations: int = 500, max_products: int = 999, particle_range: Tuple[float, float] = (1.0, 100), weights: Dict = None, ): int_iterations = int(iterations) if int_iterations <= 0: return Response("Number of iterations must be a positiv integer", 400) if particle_range[0] >= particle_range[1]: return Response("Particle size 'from' must be smaller than 'to'", 400) if max_products == 0: max_products = 999 if not weights: weights = {"bridge": 5, "mass": 5, "products": 5} else: for i in weights.values(): if not 0 <= i <= 10: return Response("Weighting values must be between 1 and 10", 400) print( f"Started optimization request with {int_iterations} maximum iterations..." ) bridge = theoretical_bridge(option, value) selected_products = [ p for p in products_get().values() if p["id"] in products ] if len(selected_products) < 2: return Response( "Can not run the optimizer with less than two products", 400) optimizer = Optimizer( products=selected_products, bridge=bridge, density_goal=density_goal, volume=volume, max_iterations=int_iterations, max_products=max_products, particle_range=particle_range, weights=weights, ) optimizer_result = optimizer.optimize() combination = optimizer_result["combination"] products_result: List[Product] = [] for p in selected_products: if p["id"] in combination.keys(): products_result.append( Product( product_id=p["id"], share=combination[p["id"]] / sum(combination.values()), cumulative=p["cumulative"], sacks=combination[p["id"]], mass=(combination[p["id"]] * volume), )) return { "name": blend_name, "config": { "iterations": optimizer_result["iterations"], "value": value, "mode": option }, "products": {id: { "id": id, "value": combination[id] } for id in combination}, "performance": optimizer.calculate_performance( experimental_bridge=optimizer_result["cumulative_bridge"], products_result=products_result, ), "totalMass": round(sum([p.mass for p in products_result]), 1), "cumulative": optimizer_result["cumulative_bridge"], "executionTime": optimizer_result["execution_time"].seconds, "fitness": optimizer_result["score"], "weighting": { "bridge": weights["bridge"], "mass": weights["mass"], "products": weights["products"], }, "curve": optimizer_result["curve"], "bridgeScore": optimizer_result["bridge_score"], }
def test_calculate_performance_normal_values(): experimental_bridge = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.009285714285714286, 0.03285714285714286, 0.15642857142857144, 0.47235714285714286, 0.9794285714285714, 1.625857142857143, 2.3253571428571433, 3.0238571428571435, 3.7215000000000003, 4.456714285714286, 5.290285714285714, 6.270071428571429, 7.422285714285714, 8.748, 10.240142857142859, 11.903642857142856, 13.734857142857145, 15.728142857142858, 17.8685, 20.133357142857147, 22.502499999999998, 24.9545, 27.478357142857142, 30.059285714285714, 32.70528571428572, 35.417642857142866, 38.206714285714284, 41.09214285714286, 44.10049999999999, 47.26971428571427, 50.63585714285714, 54.247928571428574, 58.13192857142858, 62.28471428571429, 66.65507142857143, 71.11764285714285, 75.48807142857142, 79.53007142857142, 83.05835714285715, 85.92385714285714, 88.05585714285715, 89.70292857142857, 91.11307142857142, 92.30685714285715, 93.41014285714284, 94.41485714285716, 95.31214285714287, 96.10271428571428, 96.79014285714285, 97.3795, 97.87485714285714, 98.29599999999999, 98.65050000000001, 98.94685714285716, 99.19264285714286, 99.39528571428572, 99.56164285714286, 99.69457142857144, 99.79835714285714, 99.87564285714286, 99.93264285714285, 99.9715, 99.99249999999999, 99.99828571428571, 99.99892857142858, 99.99892857142858, 99.99892857142858, 99.99892857142858, 99.99892857142858, 99.99892857142858, 99.99892857142858, ] density_goal = 100 products_goal = 5 particle_range = (1.01, 100.0) performance = Optimizer( bridge=theoretical_bridge(BridgeOption.PERMEABILITY, 500), max_products=products_goal, density_goal=density_goal, particle_range=particle_range, ).calculate_performance(experimental_bridge, products_result) print(performance)
def bridgeRequestHandler(option: str, value: int): if not value or not option: return Response("No options or value given!", 400) bridge = theoretical_bridge(option, value) response_dict = {"bridge": [round(num, 1) for num in bridge]} return response_dict