Skip to content

Commit

Permalink
Initial commit to add a new problem/solution type (Cristina)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniogi committed Nov 1, 2024
1 parent 4eb5839 commit 6031914
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 4 deletions.
44 changes: 44 additions & 0 deletions src/ProblemCristina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

#############################################################################
# Copyright 2013 by Antonio Gomez and Miguel Cardenas #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#
# See the License for the specific language governing permissions and #
# limitations under the License. #
#############################################################################

__author__ = ' AUTHORS: Antonio Gomez ([email protected])'


__version__ = ' REVISION: 1.0 - 15-01-2014'

"""
HISTORY
Version 0.1 (12-04-2013): Creation of the file.
Version 1.0 (15-01-2014): Fist stable version.
"""

import random

class ProblemCristina(object):
def __init__(self):
return

def solve(self, solution):
return random.random()

def extractSolution(self):
raise NotImplementedError("Extract solution abstract problem")

def finish(self):
raise NotImplementedError("Finish abstract problem")
59 changes: 59 additions & 0 deletions src/SolutionCristina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

#############################################################################
# Copyright 2013 by Antonio Gomez and Miguel Cardenas #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#
# See the License for the specific language governing permissions and #
# limitations under the License. #
#############################################################################

__author__ = ' AUTHORS: Antonio Gomez ([email protected])'


__version__ = ' REVISION: 1.0 - 15-01-2014'

"""
HISTORY
Version 0.1 (17-04-2013): Creation of the file.
Version 1.0 (15-01-2014): Fist stable version.
"""

from SolutionBase import SolutionBase
import Utils as u


class SolutionCristina (SolutionBase):
def __init__(self, infile):
SolutionBase.__init__(self, infile)
#TODO: Implemement how data is initialized
self.__data = None
return

def initialize(self, data):
self.__data = data
return

def prepare(self, filename):
self.__data.create_input_file(filename)

def getNumberofParams(self):
return self.__data.getNumParams()

def getMaxNumberofValues(self):
return self.__data.getMaxRange()

def getParameters(self):
return self.__data.getParameters()

def getParametersValues(self):
return self.__data.getValsOfParameters()
5 changes: 5 additions & 0 deletions src/SolutionsQueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os

from SolutionFusion import SolutionFusion
from SolutionCristina import SolutionCristina
from SolutionNonSeparable import SolutionNonSeparable
import Utils as util
from Parameter import Parameter
Expand Down Expand Up @@ -57,6 +58,10 @@ def __init__(self, filename, solution_type, infile, writeToFile,
self.__solutionBase = SolutionFusion(self.__infile)
util.logger.info("QUEUE: Initialised fusion queue (" +
filename + ")" + self.__infile)
if self.__solType == util.solution_type.CRISTINA:
self.__solutionBase = SolutionCristina(self.__infile)
util.logger.info("QUEUE: Initialised Cristina queue (" +
filename + ")" + self.__infile)
else:
self.__solutionBase = SolutionNonSeparable(self.__infile)
util.logger.info("QUEUE: Initialised non separable queue (" +
Expand Down
23 changes: 23 additions & 0 deletions src/SolverDAB.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import time
from array import array
from SolverBase import SolverBase
from ProblemCristina import ProblemCristina
from ProblemFusion import ProblemFusion
from ProblemNonSeparable import ProblemNonSeparable
from SolutionCristina import SolutionCristina
from SolutionFusion import SolutionFusion
from SolutionNonSeparable import SolutionNonSeparable
import Utils as u
Expand Down Expand Up @@ -85,6 +87,12 @@ def __init__(self, ProblemType, infile):
self.__solution_type = u.solution_type.NONSEPARABLE
self.__bestLocalSolution = SolutionNonSeparable(infile)
self.__bestGlobalSolution = SolutionNonSeparable(infile)
elif ProblemType == u.problem_type.CRISTINA:
self.__problem = ProblemCristina()
self.__solution_type = u.solution_type.CRISTINA
self.__bestLocalSolution = SolutionCristina(infile)
self.__bestGlobalSolution = SolutionCristina(infile)

if u.objective == u.objectiveType.MAXIMIZE:
self.__bestLocalSolution.setValue(-u.infinity)
self.__bestGlobalSolution.setValue(-u.infinity)
Expand Down Expand Up @@ -453,6 +461,10 @@ def __init__(self, problem_type, infile, configfile):
self.__problem = ProblemNonSeparable()
self.__bestSolution = SolutionNonSeparable(self.__infile)
self.__bestGlobalSolution = SolutionNonSeparable(self.__infile)
elif self.__problem_type == u.problem_type.CRISTINA:
self.__problem = ProblemCristina()
self.__bestSolution = SolutionCristina(self.__infile)
self.__bestGlobalSolution = SolutionCristina(self.__infile)
else:
u.logger.critical("SolverDAB (" + str(sys.exc_info()[2].tb_lineno) +
"). Unknown problem type " + str(problem_type))
Expand Down Expand Up @@ -491,6 +503,13 @@ def __init__(self, problem_type, infile, configfile):
"pending.queue", u.solution_type.FUSION, infile, False)
self.__topSolutions = solQueue.SolutionsQueue(
"top.queue", u.solution_type.FUSION, infile, False, True)
if problem_type == u.problem_type.CRISTINA:
self.__finishedSolutions = solQueue.SolutionsQueue(
"finishedNonSep.queue", u.solution_type.CRISTINA, infile, True, True)
self.__pendingSolutions = solQueue.SolutionsQueue(
"pendingNonSep.queue", u.solution_type.CRISTINA, infile, False)
self.__topSolutions = solQueue.SolutionsQueue(
"top.queue", u.solution_type.CRISTINA, infile, False, True)
if problem_type == u.problem_type.NONSEPARABLE:
self.__finishedSolutions = solQueue.SolutionsQueue(
"finishedNonSep.queue", u.solution_type.NONSEPARABLE, infile, True, True)
Expand Down Expand Up @@ -747,6 +766,8 @@ def receiveSolutions(self):
solutionTemp = SolutionFusion(self.__infile)
elif self.__problem_type == u.problem_type.NONSEPARABLE:
solutionTemp = SolutionNonSeparable(self.__infile)
elif self.__problem_type == u.problem_type.CRISTINA:
solutionTemp = SolutionCristina(self.__infile)

if solutionTemp is None:
u.logger.error("Solution is None after creation (type " + str(self.__problem_type) + ")")
Expand Down Expand Up @@ -886,6 +907,8 @@ def runDistributed(self):
self.__problem = ProblemFusion()
elif (self.__problem_type == u.problem_type.NONSEPARABLE):
self.__problem = ProblemNonSeparable()
elif (self.__problem_type == u.problem_type.CRISTINA):
self.__problem = ProblemCristina()

numParams = self.__bestSolution.getNumberofParams()
buff = array('f', [0]) * numParams
Expand Down
4 changes: 2 additions & 2 deletions src/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def enum(**enums):
return type('Enum', (), enums)

#problem type
problem_type = enum(NONE=0, FUSION=1, NONSEPARABLE=2)
problem_type = enum(NONE=0, FUSION=1, NONSEPARABLE=2, CRISTINA=3)

#solution type
solution_type = enum(FUSION=1, NONSEPARABLE=2)
solution_type = enum(FUSION=1, NONSEPARABLE=2, CRISTINA=3)

#solver type
solver_type = enum(NONE=0, DAB=1, SA=2)
Expand Down
6 changes: 6 additions & 0 deletions src/Worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import sys
from mpi4py import MPI
import Utils as u
from SolutionCristina import SolutionCristina
from SolutionFusion import SolutionFusion
from SolutionNonSeparable import SolutionNonSeparable
from ProblemCristina import ProblemCristina
from ProblemFusion import ProblemFusion
from ProblemNonSeparable import ProblemNonSeparable

Expand Down Expand Up @@ -59,6 +61,8 @@ def __init__(self, comm, problem_type):
self.__problem = ProblemFusion()
elif self.__problem_type == u.problem_type.NONSEPARABLE:
self.__problem = ProblemNonSeparable()
elif self.__problem_type == u.problem_type.CRISTINA:
self.__problem = ProblemCristina()
except Exception as e:
print("Worker " + str(sys.exc_info()[2].tb_lineno) + " " + str(e))

Expand Down Expand Up @@ -93,6 +97,8 @@ def run(self, infile, cfile):
solution = SolutionFusion(infile)
elif self.__problem_type == u.problem_type.NONSEPARABLE:
solution = SolutionNonSeparable(infile)
elif self.__problem_type == u.problem_type.CRISTINA:
solution = SolutionCristina(infile)
else:
u.logger.error("WORKER (" + str(self.__rank) +
") Problem type not supported")
Expand Down
6 changes: 4 additions & 2 deletions src/disop.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def main():
try:
inputfile = "../data/param_config.xml"
configfile = "../data/DABConfigFile"
problem_type = util.problem_type.FUSION
problem_type = util.problem_type.CRISTINA
solver_type = util.solver_type.DAB

#process the arguments if the argparse module has been found
Expand All @@ -112,7 +112,7 @@ def main():

parser.add_argument('-p', '--problem', required=True, type=str,
default='FUSION',
choices=['FUSION', 'NONSEPARABLE'],
choices=['FUSION', 'NONSEPARABLE', 'CRISTINA'],
help='Problem type')
parser.add_argument('-v', '--verbose', required=False, type=int,
default=3, choices=[1, 2, 3],
Expand All @@ -135,6 +135,8 @@ def main():
#extract the problem type
if args.problem == 'FUSION':
problem_type = util.problem_type.FUSION
if args.problem == 'CRISTINA':
problem_type = util.problem_type.CRISTINA
if args.problem == 'NONSEPARABLE':
problem_type = util.problem_type.NONSEPARABLE

Expand Down

0 comments on commit 6031914

Please sign in to comment.