-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mandd/multipleknapsack models #17
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some comments
element1Val ,element2Val ,element3Val ,element4Val ,element5Val, | ||
element1Cost ,element2Cost ,element3Cost ,element4Cost,element5Cost, | ||
validity,totalValue,capacityID</variables> | ||
<capacity>capacityID</capacity> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, capacity is required input/variable from RAVEN perspective, and you do not allow user to directly provide this value. Right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct, the goal is to have knapsack parameters as raven generated variables
src/knapsack/BaseKnapsackModel.py
Outdated
@@ -108,16 +110,19 @@ def run(self, container, inputDict): | |||
@ In, inputDict, dict, dictionary of inputs from RAVEN | |||
""" | |||
totalValue = 0.0 | |||
capacity = inputDict[self.capacity][0] | |||
print(capacity) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove print?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
src/knapsack/BaseKnapsackModel.py
Outdated
totalValue = totalValue + inputDict[container.mapping[key][0]] | ||
else: | ||
self.capacity = self.capacity - inputDict[container.mapping[key][1]] | ||
print('======') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove print?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
#Internal Modules End----------------------------------------------------------- | ||
|
||
|
||
class MultipleKnapsackModel(ExternalModelPluginBase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you inherit directly from BaseKnapsackModel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a subset of elements that are in common between these two classes.
We could design a baseClass for all knapsack models.
I am open to suggestions.
mapping = InputData.parameterInputFactory('map', contentType=InputTypes.StringType) | ||
mapping.addParam('value', param_type=InputTypes.StringType, required=True) | ||
mapping.addParam('cost', param_type=InputTypes.StringType, required=True) | ||
inputSpecs.addSub(mapping) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you inherit directly from BaseKnapsackModel, most of these lines can be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, having a base class would simplify the structure of getInputSpecs and _readMoreXML methods.
container.mapping = {} | ||
self.knapsackSet = {} | ||
|
||
for child in xmlNode: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update this with
specs = self.getInputSpecs()()
specs.parseNode(xmlNode)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the overall _readMoreXML method
knapsackSetValues={} | ||
|
||
# knapsackSetValues is a dictionary in the form {knapsackID: knapsackValue} | ||
for knapsack in self.knapsackSet.keys(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if self.knapsackSet[knapsack] in inputDict first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check has been added
knapsackChosen = str(int(inputDict[key][0])) | ||
testValue = knapsackSetValues[knapsackChosen] - inputDict[container.mapping[key][1]] | ||
if testValue >= 0: | ||
knapsackSetValues[knapsackChosen] = knapsackSetValues[knapsackChosen] - inputDict[container.mapping[key][1]][0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line can be moved to the outside of this if condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code has been updated, similar issue was located in the base knapsack model which has been edited as well
else: | ||
container.__dict__[self.outcome] = 0. | ||
|
||
print(knapsackSetValues) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove print?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
numberUnsatConstraints = 0.0 | ||
for knapsack in knapsackSetValues.keys(): | ||
if knapsackSetValues[knapsack] < 0: | ||
numberUnsatConstraints = numberUnsatConstraints + 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop can be combined with previous for loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point: merged with previous loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment to reviewer
element1Val ,element2Val ,element3Val ,element4Val ,element5Val, | ||
element1Cost ,element2Cost ,element3Cost ,element4Cost,element5Cost, | ||
validity,totalValue,capacityID</variables> | ||
<capacity>capacityID</capacity> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct, the goal is to have knapsack parameters as raven generated variables
src/knapsack/BaseKnapsackModel.py
Outdated
@@ -108,16 +110,19 @@ def run(self, container, inputDict): | |||
@ In, inputDict, dict, dictionary of inputs from RAVEN | |||
""" | |||
totalValue = 0.0 | |||
capacity = inputDict[self.capacity][0] | |||
print(capacity) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
src/knapsack/BaseKnapsackModel.py
Outdated
totalValue = totalValue + inputDict[container.mapping[key][0]] | ||
else: | ||
self.capacity = self.capacity - inputDict[container.mapping[key][1]] | ||
print('======') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
#Internal Modules End----------------------------------------------------------- | ||
|
||
|
||
class MultipleKnapsackModel(ExternalModelPluginBase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a subset of elements that are in common between these two classes.
We could design a baseClass for all knapsack models.
I am open to suggestions.
mapping = InputData.parameterInputFactory('map', contentType=InputTypes.StringType) | ||
mapping.addParam('value', param_type=InputTypes.StringType, required=True) | ||
mapping.addParam('cost', param_type=InputTypes.StringType, required=True) | ||
inputSpecs.addSub(mapping) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, having a base class would simplify the structure of getInputSpecs and _readMoreXML methods.
container.mapping = {} | ||
self.knapsackSet = {} | ||
|
||
for child in xmlNode: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the overall _readMoreXML method
knapsackSetValues={} | ||
|
||
# knapsackSetValues is a dictionary in the form {knapsackID: knapsackValue} | ||
for knapsack in self.knapsackSet.keys(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check has been added
knapsackChosen = str(int(inputDict[key][0])) | ||
testValue = knapsackSetValues[knapsackChosen] - inputDict[container.mapping[key][1]] | ||
if testValue >= 0: | ||
knapsackSetValues[knapsackChosen] = knapsackSetValues[knapsackChosen] - inputDict[container.mapping[key][1]][0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code has been updated, similar issue was located in the base knapsack model which has been edited as well
numberUnsatConstraints = 0.0 | ||
for knapsack in knapsackSetValues.keys(): | ||
if knapsackSetValues[knapsack] < 0: | ||
numberUnsatConstraints = numberUnsatConstraints + 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point: merged with previous loop
else: | ||
container.__dict__[self.outcome] = 0. | ||
|
||
print(knapsackSetValues) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
Job Test linux pull on 465135e : invalidated by @joshua-cogliati-inl Civet recipe has been updated. |
Job Test linux pull on 465135e : invalidated by @wangcj05 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some comments.
|
||
alias = InputData.parameterInputFactory('alias', contentType=InputTypes.StringType) | ||
alias.addParam('variable', param_type=InputTypes.StringType, required=True) | ||
alias.addParam('type', param_type=InputTypes.StringType, required=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to keep alias, this can be useful. The alias is handled directly by Model.py inside RAVEN without using InputData. If you want to keep this capability, we need to keep these three lines to let knapsack external model aware of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed because I thought I merely did a copy and paste; restored
@@ -112,27 +101,4 @@ def run(self, container, inputDict): | |||
@ In, container, object, self-like object where all the variables can be stored | |||
@ In, inputDict, dict, dictionary of inputs from RAVEN | |||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can make the run class as abstractmethod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
src/knapsack/SimpleKnapsackModel.py
Outdated
#Internal Modules End----------------------------------------------------------- | ||
|
||
|
||
class SimpleKnapsackModel(ExternalModelPluginBase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class needs to be updated with the base class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I'll updated the two derived classes
src/knapsack/SimpleKnapsackModel.py
Outdated
else: | ||
raise IOError("BaseKnapsackModel: xml node " + name + " is not allowed") | ||
|
||
def initialize(self, container, runInfoDict, inputFiles): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one will be removed if you do not need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have two extra comments.
__init__.py
Outdated
from LOGOS.src.knapsack import MultipleKnapsackModel | ||
from LOGOS.src.knapsack import BaseKnapsackModel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You do not need to import BaseKnapsackModel since this model can not be directly used by RAVEN.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point fixed
doc/user_manual/include/Knapsack.tex
Outdated
@@ -9,12 +9,13 @@ \section{Knapsack Models} | |||
find the optimal solution. | |||
|
|||
|
|||
\subsection{BaseKnapsackModel} | |||
\subsection{BaseKnapsack Model} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseKnapsackModel --> SimpleKnapsackModel? If so, please update this section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, fixed all section
…holab/LOGOS into mandd/MultipleknapsackModels
Changes are good, and tests are green. This PR can be merged. |
Creation of MultipleKnapsack model
Issue #18