Skip to content
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

Merged
merged 21 commits into from
Feb 16, 2021
Merged

Mandd/multipleknapsack models #17

merged 21 commits into from
Feb 16, 2021

Conversation

mandd
Copy link
Collaborator

@mandd mandd commented Feb 8, 2021

Creation of MultipleKnapsack model
Issue #18

@mandd mandd requested a review from wangcj05 February 8, 2021 20:52
@mandd mandd added enhancement New feature or request priority normal labels Feb 8, 2021
Copy link
Collaborator

@wangcj05 wangcj05 left a 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>
Copy link
Collaborator

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?

Copy link
Collaborator Author

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

@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

totalValue = totalValue + inputDict[container.mapping[key][0]]
else:
self.capacity = self.capacity - inputDict[container.mapping[key][1]]
print('======')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print?

Copy link
Collaborator Author

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):
Copy link
Collaborator

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?

Copy link
Collaborator Author

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)
Copy link
Collaborator

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

Copy link
Collaborator Author

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:
Copy link
Collaborator

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) 

Copy link
Collaborator Author

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():
Copy link
Collaborator

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?

Copy link
Collaborator Author

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]
Copy link
Collaborator

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.

Copy link
Collaborator Author

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment on lines 135 to 138
numberUnsatConstraints = 0.0
for knapsack in knapsackSetValues.keys():
if knapsackSetValues[knapsack] < 0:
numberUnsatConstraints = numberUnsatConstraints + 1.
Copy link
Collaborator

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.

Copy link
Collaborator Author

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

Copy link
Collaborator Author

@mandd mandd left a 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>
Copy link
Collaborator Author

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

@@ -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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

totalValue = totalValue + inputDict[container.mapping[key][0]]
else:
self.capacity = self.capacity - inputDict[container.mapping[key][1]]
print('======')
Copy link
Collaborator Author

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):
Copy link
Collaborator Author

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)
Copy link
Collaborator Author

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:
Copy link
Collaborator Author

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():
Copy link
Collaborator Author

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]
Copy link
Collaborator Author

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

Comment on lines 135 to 138
numberUnsatConstraints = 0.0
for knapsack in knapsackSetValues.keys():
if knapsackSetValues[knapsack] < 0:
numberUnsatConstraints = numberUnsatConstraints + 1.
Copy link
Collaborator Author

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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@moosebuild
Copy link

Job Test linux pull on 465135e : invalidated by @joshua-cogliati-inl

Civet recipe has been updated.

@moosebuild
Copy link

Job Test linux pull on 465135e : invalidated by @wangcj05

Copy link
Collaborator

@wangcj05 wangcj05 left a 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)
Copy link
Collaborator

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.

Copy link
Collaborator Author

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
"""
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

#Internal Modules End-----------------------------------------------------------


class SimpleKnapsackModel(ExternalModelPluginBase):
Copy link
Collaborator

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.

Copy link
Collaborator Author

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

else:
raise IOError("BaseKnapsackModel: xml node " + name + " is not allowed")

def initialize(self, container, runInfoDict, inputFiles):
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

Copy link
Collaborator

@wangcj05 wangcj05 left a 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
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point fixed

@@ -9,12 +9,13 @@ \section{Knapsack Models}
find the optimal solution.


\subsection{BaseKnapsackModel}
\subsection{BaseKnapsack Model}
Copy link
Collaborator

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.

Copy link
Collaborator Author

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

@wangcj05
Copy link
Collaborator

Changes are good, and tests are green. This PR can be merged.

@wangcj05 wangcj05 merged commit 2cf41f9 into devel Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority normal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants