Skip to content

Commit

Permalink
Merge pull request #2 from antoniogi/cristina
Browse files Browse the repository at this point in the history
Changes for Cristina
  • Loading branch information
antoniogi authored Nov 15, 2024
2 parents 4eb5839 + ef151f1 commit 33f6e02
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 54 deletions.
20 changes: 20 additions & 0 deletions data/DABConfigFileCristina
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Bees]
#Number of employed bees
nemployed: 2
#Number of onlooker bees
nonlooker: 1
#Modification factor used by the onlooker bees
onlookerModFactor: 0.0001
#Number of iterations before a solution is abandoned
iterationsAbandoned: 5
#probability for an employed to change a parameter (defined as 1/value)
probEmployedChange: 4

[Algorithm]
#Execution time
time: 50000
#Size of the queue that stores the solutions that haven't been set yet
pendingSize: 50
objective: min

[General]
48 changes: 48 additions & 0 deletions data/param_cristina.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<CristinaParams>
<param>
<index>0</index>
<name>x0</name>
<value>0</value>
<type>int</type>
<gap>1</gap>
<min_value>-100</min_value>
<max_value>100</max_value>
</param>
<param>
<index>1</index>
<name>x1</name>
<value>0</value>
<type>int</type>
<gap>1</gap>
<min_value>-100</min_value>
<max_value>100</max_value>
</param>
<param>
<index>2</index>
<name>x2</name>
<value>0</value>
<type>int</type>
<gap>1</gap>
<min_value>-100</min_value>
<max_value>100</max_value>
</param>
<param>
<index>3</index>
<name>x3</name>
<value>0</value>
<type>int</type>
<gap>1</gap>
<min_value>-100</min_value>
<max_value>100</max_value>
</param>
<param>
<index>4</index>
<name>x4</name>
<value>0</value>
<type>int</type>
<gap>1</gap>
<min_value>-100</min_value>
<max_value>100</max_value>
</param>
</CristinaParams>
159 changes: 159 additions & 0 deletions src/CristinaData.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/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. #
#############################################################################

import os
import sys
from array import array
from xml.dom import minidom
import Utils as u
from Parameter import Parameter

__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.
"""


class CristinaData (object):
"""
This class stores all the data required by VMEC.
It also provides methods to read the input xml file that, for each
parameter, specifies the min/max/default values, the gap, the index,...
It can also create an XML output file with the data it contains
"""
def __init__(self):
self.__numParams = 0
self.__maxRange = 0
self.__fInput = None
self.__params = []
return

def __del__(self):
try:
del self.__params[:]
except:
pass

#returns the number of parameters that can be actually modified
def getNumParams(self):
return self.__numParams

def printData(self):
for i in range(self.__numParams):
u.logger.info("CristinaData. Param(" + str(i) +") - Value: " + str(self.__params[i].get_value()))

"""
Returns a list of doubles with the values of the modificable parameters
"""
def getValsOfParameters(self):
buff = array('f', [0]) * self.__numParams
for i in range(self.__numParams):
buff[i] = float(self.__params[i].get_value())
return buff

"""
Receives as parameter (buff) a list of values corresponding to
the values that the parameters that can be modified must take.
Goes through all of the parameters and changes the values of the
modificable parameters to the value specified in this list
"""

def setValsOfParameters(self, buff):
u.logger.debug("CristinaData. Setting parameters (number: "
+ str(len(buff)) + ")")
for i in range(len(buff)):
self.__params[i].set_value(buff[i])

def setParameters(self, parameters):
for param in parameters:
self.assign_parameter(param)
"""
Return a list with all the parameters (list of Parameter objects)
"""
def getParameters(self):
return self.__params

"""
Assign a parameter with a new value to an older version of the
same parameter
"""
def assign_parameter(self, parameter):
index = parameter.get_index()
if index >= len(self.__params):
self.__params.append(parameter)

def getMaxRange(self):
return self.__maxRange

"""
Method that reads the xml input file. Puts into memory all the data
contained in that file (min-max values, initial value, gap,...)
Argument:
- filepath: path to the XML input file
"""

def initialize(self, filepath):
try:
if not os.path.exists(filepath):
filepath = "../" + filepath
xmldoc = minidom.parse(filepath)
pNode = xmldoc.childNodes[0]
for node in pNode.childNodes:
if node.nodeType == node.ELEMENT_NODE:
c = Parameter()
for node_param in node.childNodes:
if node_param.nodeType == node_param.ELEMENT_NODE:
if node_param.localName == "type":
c.set_type(node_param.firstChild.data)
if node_param.localName == "value":
c.set_value(node_param.firstChild.data)
if node_param.localName == "min_value":
c.set_min_value(node_param.firstChild.data)
if node_param.localName == "max_value":
c.set_max_value(node_param.firstChild.data)
if node_param.localName == "index":
c.set_index(node_param.firstChild.data)
if node_param.localName == "name":
c.set_name(node_param.firstChild.data)
if node_param.localName == "gap":
c.set_gap(node_param.firstChild.data)
try:
self.assign_parameter(c)
self.__numParams += 1
values = 1 + int(round((c.get_max_value() -
c.get_min_value()) / c.get_gap()))
self.__maxRange = max(values, self.__maxRange)
except ValueError as e:
u.logger.warning("Problem calculating max range: " +
str(e) + " . Fileno: " + str(sys.exc_info()[2].tb_lineno))
pass
u.logger.debug("Number of parameters " +
str(self.__numParams) + "(" +
str(len(self.__params)) + ")")
except Exception as e:
u.logger.error("CristinaData (" +
str(sys.exc_info()[2].tb_lineno) +
"). Problem reading input xml file: " + str(e))
sys.exit(111)
return
48 changes: 48 additions & 0 deletions src/ProblemCristina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/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):
val = random.randint(0, 1000000)
solution.setValue(val)
# if val==0:
# print("ProblemCristina. Solution found with value: " + str(val))
return val

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

def finish(self):
raise NotImplementedError("Finish abstract problem")
11 changes: 8 additions & 3 deletions src/SolutionBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
class SolutionBase (object):
def __init__(self, infile):
#initial value of the solution is the min value represented by a float
if (util.objective == util.objectiveType.MINIMIZE):
if util.objective == util.objectiveType.MINIMIZE:
self.__value = util.infinity
else:
elif util.objective == util.objectiveType.MAXIMIZE:
self.__value = -util.infinity
self.__value = util.infinity #### REMOVE!!
else:
raise ValueError("Objective type not defined")
self.__isValid = True
return

Expand All @@ -53,6 +54,10 @@ def getNumberofParams(self):
def getMaxNumberofValues(self):
return 0

def print(self):
u.logger.debug("Print function not implemented")
return

def isValid(self):
return self.__isValid

Expand Down
75 changes: 75 additions & 0 deletions src/SolutionCristina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/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.
Version 1.1 (15-11-2024): Minor change
"""

from SolutionBase import SolutionBase
import Utils as u
from CristinaData import CristinaData


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

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

def prepare(self, filename):
return
# 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()

def setParametersValues(self, buff):
self.__data.setValsOfParameters(buff)

def setParameters(self, params):
self.__data.setParameters(params)

def getData(self):
return self.__data

def print(self):
self.__data.printData()
Loading

0 comments on commit 33f6e02

Please sign in to comment.