0

I have created two files. The first one called EA contains variables and calls to functions. The second file called EA_Functions contains all the functions used in EA.

This is EA:

from EA_Functions import *

HIGHEST_NUMBER = 10
LOWEST_NUMBER = 1
SET_SIZE = 5
NUMBER_OF_PARENTS = 2
NUMBER_OF_CHILDREN = 2
MUTATION_STEP_SIZE = 0.5

warms = {}

# Create an initial random sets of warms
for i in range(SET_SIZE):
    warms[i] = np.random.randint(low=LOWEST_NUMBER, high=HIGHEST_NUMBER + 1, size=SET_SIZE, dtype=int)

# Order the set of warms based on the error
warms_sorted = sort(warms)

This is EA_Functions:

import numpy as np
from random import randint


def calculate_error(lst):
    return (SET_SIZE - np.mean(lst)) ** 2


def sort(dictionary):
    return sorted(dictionary.items(), key=lambda item: calculate_error(item[1]))

I want to use SET_SIZE variable contained in the first file, EA.

If in EA_Functions I try to import EA:

from EA import *

I get errors during the execution of the "main" code, the file EA

6
  • You need to make it a function parameter. Commented Jun 26, 2020 at 0:27
  • Why do you need to have it on EA? If you are not going to change it's value via code, you can have it on EA_Functions and the code will do fine Commented Jun 26, 2020 at 0:36
  • You should include the error message in the question. Commented Jun 26, 2020 at 0:36
  • Some variables are also used on EA Commented Jun 26, 2020 at 0:57
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. Commented Jun 26, 2020 at 1:03

2 Answers 2

0

you can add a parameter in the function like this:

def calculate_error(lst, size):
    return (size - np.mean(lst)) ** 2

then, when you call it pass SET_SIZE to calculate_error()

Sign up to request clarification or add additional context in comments.

1 Comment

Very smart! Thank you ;) I didn't think about that.
0

You are getting errors because you have a circular import. If you are from EA_Functions.py in EA.py, you shouldn't import from EA.py in EA_Functions.py.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.