Пример #1
0
def knapsack_simulation(name):
    data = db.knapsack.find_one({"_id": ObjectId(name)})

    array1 = ast.literal_eval(data['knapsack_array1'])
    arr_size1 = 0

    array2 = ast.literal_eval(data['knapsack_array2'])
    arr_size2 = 0
    knapsack_size = int(data['knapsack_size'])

    knapsack_array1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0 ,0 ,0,0,0,0]
    knapsack_array2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0 ,0 ,0,0,0,0]

    for i in range(len(array1)):
        knapsack_array1[i+1] = int(array1[i])
        arr_size1+=1

    for i in range(len(array2)):
        knapsack_array2[i+1] = int(array2[i])
        arr_size2+=1

    print(knapsack_array1)
    print(knapsack_array2)

    simulation_strategy = knpsck.KnapSack(int(knapsack_size), int(arr_size2), knapsack_array1, knapsack_array2)
    simulation_context = SimulationContext(simulation_strategy)
    data = simulation_context.get_data()
    saved = 0
    if 'username' in session.keys():
        key = db.saved_simulation.find_one({'algo_id': name, "username": session['username'], "type": "idp",
                                            "algo": "knapsack"})
        if key is not None:
            saved = 1
    return render_template(
        '/simulation/knapsack_iterative_dp/knapsack_iterative_dp_simulation.html', **locals())
Пример #2
0
def coin_change_variant5_simulation(name):
    data = db.coin_change_variant5.find_one({"_id": ObjectId(name)})
    coin_array1 = ast.literal_eval(data['coin_array_variant5'])
    coin_array2 = []
    coin_array2.append(0)
    for i in range(len(coin_array1)):
        coin_array2.append(int(coin_array1[i]))
    k = len(coin_array1)
    payment = data['payment_variant5']

    dp_simulation_strategy = ccv5.CoinChangeVariant5(int(payment), k,
                                                     coin_array2)
    dp_simulation_context = SimulationContext(dp_simulation_strategy)
    data = dp_simulation_context.get_data()

    saved = 0
    if 'username' in session.keys():
        key = db.saved_simulation.find_one({
            'algo_id': name,
            "username": session['username'],
            "type": "idp",
            "algo": "coin_change_variant5"
        })
        if key is not None:
            saved = 1
    return data, saved
Пример #3
0
def eigen_value_entry_data(data):
    data = str(json.dumps(data['entry']))
    if eigen_util.validate_entry(data) is False:
        return False, data.replace('\"', '').replace('\\r\\n', '\n')
    else:

        simulation_strategy = eigen.EigenValue(eigen_util.get_matrix(data))
        simulation_context = SimulationContext(simulation_strategy)
        data = simulation_context.get_data()

        posts = db.eigen_value
        post_id = posts.insert_one(data).inserted_id
        return True, str(post_id)
Пример #4
0
def gauss_jordan_elimination_entry_data(data):
    data = str(json.dumps(data['entry']))
    if gj_util.validate_entry(data) is False:
        return False, data.replace('\"', '').replace('\\r\\n', '\n')
    else:
        mat = gj_util.get_matrix(data)

        simulation_strategy = gj.GaussJordanElimination(mat)
        simulation_context = SimulationContext(simulation_strategy)
        data = simulation_context.get_data()

        posts = db.gauss_jordan_elimination
        post_id = posts.insert_one(data).inserted_id
        return True, str(post_id)
Пример #5
0
def rock_climbing_simulation(name):
    data = db.rock_climbing.find_one({"_id": ObjectId(name)})

    dp_simulation_strategy = rc.RockClimbing(data['string1'], data['string2'])
    dp_simulation_context = SimulationContext(dp_simulation_strategy)
    data = dp_simulation_context.get_data()

    saved = 0
    if 'username' in session.keys():
        key = db.saved_simulation.find_one({'algo_id': name, "username": session['username'], "type": "idp",
                                            "algo": "lcs"})
        if key is not None:
            saved = 1
    return data, saved
Пример #6
0
def shortest_common_supersequence_simulation(name):
    data = db.shortest_common_supersequence.find_one({"_id": ObjectId(name)})

    dp_simulation_strategy = scs.SCS(data['string1'], data['string2'])
    dp_simulation_context = SimulationContext(dp_simulation_strategy)
    data = dp_simulation_context.get_data()

    saved = 0
    if 'username' in session.keys():
        key = db.saved_simulation.find_one({'algo_id': name, "username": session['username'], "type": "idp",
                                            "algo": "lcs"})
        if key is not None:
            saved = 1
    return data, saved
def matrix_chain_multiplication_simulation(name):
    data = db.matrix_chain_multiplication.find_one({"_id": ObjectId(name)})
    dimensions = ast.literal_eval(data['dimension_array'])

    k = len(dimensions)

    dp_simulation_strategy = mcm.MatrixChainMultiplication(k, dimensions)
    dp_simulation_context = SimulationContext(dp_simulation_strategy)
    data = dp_simulation_context.get_data()

    saved = 0
    if 'username' in session.keys():
        key = db.saved_simulation.find_one({'algo_id': name, "username": session['username'], "type": "idp",
                                            "algo": "matrix_chain_multiplication"})
        if key is not None:
            saved = 1
    return data, saved
Пример #8
0
def longest_common_subsequence():
    name = '5bd118b776cc9d2f4663873f'
    data = db.longest_common_subsequence.find_one({"_id": ObjectId(name)})

    simulation_strategy = LCS(data['string1'], data['string2'])
    simulation_context = SimulationContext(simulation_strategy)

    data = simulation_context.get_data()
    saved = 0
    if 'username' in session.keys():
        key = db.saved_simulation.find_one({
            'algo_id': name,
            "username": session['username'],
            "type": "idp",
            "algo": "lcs"
        })
        if key is not None:
            saved = 1
    return data, saved
Пример #9
0
def longest_increasing_subsequence_simulation(name):
    data = db.longest_increasing_subsequence.find_one({"_id": ObjectId(name)})
    #arr = data['lis_array']
    arr = ast.literal_eval(data['lis_array'])
    arr_size = 0
    lis_array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0 ,0 ,0,0,0,0]
    for i in range(len(arr)):
        lis_array[i+1] = int(arr[i])
        arr_size+=1

    simulation_strategy = lis.LIS(int(arr_size), lis_array)
    simulation_context = SimulationContext(simulation_strategy)
    data = simulation_context.get_data()
    saved = 0
    if 'username' in session.keys():
        key = db.saved_simulation.find_one({'algo_id': name, "username": session['username'], "type": "idp",
                                            "algo": "lis"})
        if key is not None:
            saved = 1
    return render_template(
        '/simulation/lis_iterative_dp/lis_iterative_dp_simulation.html', **locals())
Пример #10
0
def logistic_regression_data_entry_data(data):
    iterations = int(data['iterations'])
    steps = int(data['steps'])
    alpha = float(data['alpha'])
    data = lgr_util.get_data(data['entry'])
    x1 = data[0]
    x2 = data[1]
    y = np.asarray(data[2])
    x = lgr_util.combine(x1, x2)
    simulation_strategy = lgr.LogisticRegression(x, y, iterations, steps,
                                                 alpha)
    simulation_context = SimulationContext(simulation_strategy)

    data = dict()
    data['data'] = simulation_context.get_data()
    data['x1'] = x1
    data['x2'] = x2
    data['y'] = list(y)
    data['alpha'] = alpha
    posts = db.logistic_regression
    post_id = posts.insert_one(data).inserted_id
    return post_id
Пример #11
0
def edit_distance_simulation(name):
    data = db.edit_distance.find_one({"_id": ObjectId(name)})

    print(data['string1'])

    simulation_strategy = edis.EditDistance(data['string1'], data['string2'])
    simulation_context = SimulationContext(simulation_strategy)
    data = simulation_context.get_data()

    saved = 0
    if 'username' in session.keys():
        key = db.saved_simulation.find_one({
            'algo_id': name,
            "username": session['username'],
            "type": "idp",
            "algo": "edit_distance"
        })
        if key is not None:
            saved = 1
    return render_template(
        '/simulation/edit_distance_iterative_dp/edit_distance_iterative_dp_simulation.html',
        **locals())
Пример #12
0
def k_means_clustering_entry_data(data):
    points = km_util.get_points(data['entry'])
    max_p = 0.0
    min_p = 0.0
    for point in points:
        for entry in point:
            if max_p < entry:
                max_p = entry
            if min_p > entry:
                min_p = entry
    centroids = [[random.uniform(min_p, max_p),
                  random.uniform(min_p, max_p)]
                 for i in range(int(data['clusters']))]

    simulation_strategy = km.KMeans(points, centroids)
    simulation_context = SimulationContext(simulation_strategy)
    data = dict()
    data['data'] = simulation_context.get_data()

    posts = db.k_means_clustering
    post_id = posts.insert_one(data).inserted_id
    return post_id
Пример #13
0
def linear_regression_data_entry_data(data):
    ret_data = dict()

    try:
        alpha = float(data['alpha'])
    except ValueError:
        ret_data['data'] = data['entry'].replace('\"', '').replace('\\r\\n', '\n')
        ret_data['msg'] = 'Wrong format for Alpha'
        return 0, ret_data

    try:
        iterations = int(data['iterations'])
    except ValueError:
        ret_data['data'] = data['entry'].replace('\"', '').replace('\\r\\n', '\n')
        ret_data['msg'] = 'Wrong format for Iterations'
        return 0, ret_data

    try:
        steps = int(data['steps'])
    except ValueError:
        ret_data['data'] = data['entry'].replace('\"', '').replace('\\r\\n', '\n')
        ret_data['msg'] = 'Wrong format for Steps'
        return 0, ret_data

    try:
        theta0 = float(data['theta0'])
        theta1 = float(data['theta1'])
        theta = np.array([theta0, theta1]);
    except ValueError:
        ret_data['data'] = data['entry'].replace('\"', '').replace('\\r\\n', '\n')
        ret_data['msg'] = 'Wrong format for Theta'
        return 0, ret_data

    if steps > iterations:
        ret_data['data'] = data['entry'].replace('\"', '').replace('\\r\\n', '\n')
        ret_data['msg'] = 'Steps should be less than iterations'
        return 0, ret_data

    if iterations % steps != 0:
        iterations -= iterations % steps

    if iterations / steps > 30:
        ret_data['data'] = data['entry'].replace('\"', '').replace('\\r\\n', '\n')
        ret_data['msg'] = 'Either decrease iterations or increase steps.'
        return 0, ret_data

    data = str(json.dumps(data['entry'])).replace("\"", "")
    if lr_util.validate_entry(data) is False:
        ret_data['data'] = data.replace('\"', '').replace('\\r\\n', '\n')
        ret_data['msg'] = 'Wrong Data Format'
        return 0, ret_data

    ret_data['data'] = data.replace('\"', '').replace('\\r\\n', '\n')
    ret_data['msg'] = 'Overshoot occurred. Try lower value of Alpha.'
    data = lr_util.get_matrix(data)

    x = data[0]
    y = data[1]
    x = np.asarray(x)
    y = np.asarray(y)

    simulation_strategy = lr.LinearRegression(theta, x, y, alpha, iterations, steps)
    simulation_context = SimulationContext(simulation_strategy)

    data = simulation_context.get_data()

    if data['overshoot']:
        return 0, ret_data
    posts = db.linear_regression
    post_id = posts.insert_one(data).inserted_id
    return 1, post_id
Пример #14
0
def fib():
    simulation_strategy = dpfib.Fibonacci(30)
    simulation_context = SimulationContext(simulation_strategy)
    data = simulation_context.get_data()
    return render_template('/simulation/fibonacci_iterative_dp/fibonacci.html',
                           **locals())