Skip to content

Commit

Permalink
add changes with logs
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalTrip committed Sep 12, 2024
1 parent 8030f39 commit efe424f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
31 changes: 17 additions & 14 deletions fairpyx/algorithms/Optimization_Matching/FaSt.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,16 @@ def FaSt(alloc: AllocationBuilder)-> dict:
# Initialize the position array pos, F, college_values
pos= build_pos_array(initial_matching, V)
college_values=build_college_values(initial_matching,V)
logger.debug('Initial i:%d', i)
# logger.debug('Initial i:%d', i)
logger.debug('Number of Students - 1:%d', i)
# logger.debug('Initial j:%d', j)
logger.debug('Number of colleges:%d', j)

# Initialize F as a list of two lists: one for students, one for colleges
F_students = []
F_colleges = []
F_students.append(n) # Add sn to the student list in F
#logger.debug('Initialized F_students: %s, F_colleges: %s', F_stduents, F_colleges)
logger.debug('Initialized F_students: %s, F_colleges: %s', F_students, F_colleges)

logger.debug('\nInitial_matching %s', initial_matching)

Expand All @@ -277,21 +278,22 @@ def FaSt(alloc: AllocationBuilder)-> dict:
logger.debug('Current i:%d', i)
logger.debug('Current j:%d ', j)

logger.debug('V: %s', V)
logger.debug('V[i][j-1]: %d', V[i][j-1])
logger.debug('college_values:%s ', college_values)
logger.debug('college_values[j]: %d', college_values[j])
logger.debug('Valuations: %s', V)
# logger.debug('V[i][j-1]: %d', V[i][j-1])
logger.debug('Colleges values:%s ', college_values)
# logger.debug('college_values[j]: %d', college_values[j])
# IMPORTANT! in the variable college_values we use in j and not j-1 because it build like this: {1: 35, 2: 3, 3: 1}
# So this: college_values[j] indeed gave us the right index ! [i.e. different structure!]
if college_values[j] >= V[i][j-1]: # In the algo the college_values is actually v
j -= 1
else:
logger.info("index i:%s", i )
logger.info("index j: %s", j)
logger.debug('V[i][j]: %d', V[i][j])
#loggers for debugging DEMOTE
# logger.info("index i:%s", i )
# logger.info("index j: %s", j)
# logger.debug('V[i][j]: %d', V[i][j])
if V[i][j] > college_values[j]: #Raw 11 in the article- different indixes because of different structures.
initial_matching = Demote(initial_matching, i, j, 1)
logger.debug('initial_matching after demote: %s', initial_matching)
logger.debug('Matching after demote: %s', initial_matching)

else:
if V[i][j] < college_values[j]:#Raw 14
Expand Down Expand Up @@ -333,11 +335,11 @@ def FaSt(alloc: AllocationBuilder)-> dict:
college_values = build_college_values(initial_matching, V)
# Update leximin tuple
lex_tupl = get_leximin_tuple(initial_matching, V)
logger.debug('lex_tupl: %s', lex_tupl)
logger.debug('Leximin tuple for the current matching: %s', lex_tupl)

# Update position array
pos = build_pos_array(initial_matching, V)
logger.debug('pos: %s', pos)
logger.debug('Position array: %s', pos)

# Update F
# Insert all students from i to n
Expand All @@ -349,11 +351,12 @@ def FaSt(alloc: AllocationBuilder)-> dict:
if college not in F_colleges:
F_colleges.append(college)

logger.debug('Updated F_students: %s, F_colleges: %s', F_students, F_colleges)
logger.debug('Finished processing students (F_students): %s', F_students)
logger.debug('Finished processing colleges (F_colleges): %s', F_colleges)

i -= 1
iteration += 1
logger.debug('END while, i: %d, j: %d',i, j)
logger.debug('END of the loop, i: %d, j: %d',i, j)
logger.debug(f" final match: {initial_matching}")
return initial_matching

Expand Down
23 changes: 18 additions & 5 deletions matchingApp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@
from fairpyx.algorithms.Optimization_Matching.FaStGen import FaStGen
import logging


# Define a custom logging formatter to omit severity and logger name
# Configure logging to capture logs in a string buffer
log_stream = io.StringIO()
logging.basicConfig(stream=log_stream, level=logging.DEBUG)
logger = logging.getLogger(__name__)

class CustomFormatter(logging.Formatter):
def format(self, record):
# Remove DEBUG level and logger name from log output
message = record.getMessage()
return f"{message}" # Simply return the message content


# Set up logging configuration
handler = logging.StreamHandler(log_stream)
formatter = CustomFormatter()
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # Set log level to DEBUG
logger.addHandler(handler)
app = Flask(__name__)


Expand Down Expand Up @@ -47,8 +60,8 @@ def try_demo():
@app.route('/results', methods=['POST'])
def results_demo():
# Clear previous log contents
log_stream.truncate(0)
log_stream.seek(0)
log_stream.truncate(0)# Clear the log stream
log_stream.seek(0)# Move the stream pointer back to the start

# Get the algorithm type
algorithm = request.form['algorithm']
Expand Down

0 comments on commit efe424f

Please sign in to comment.