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

Urgent help regarding a problem #171

Closed
Amit-1984 opened this issue Apr 6, 2018 · 4 comments
Closed

Urgent help regarding a problem #171

Amit-1984 opened this issue Apr 6, 2018 · 4 comments
Labels

Comments

@Amit-1984
Copy link

Hello everyone,
i am trying to generate a custom population by overriding the newSolution() method in my problem class.
the code looks like:-
@OverRide
public Solution newSolution() {
Solution solution = new Solution(getNumberOfVariables(), getNumberOfObjectives());
for (int i = 0; i < totalClasses; i++)
{
RealVariable val = EncodingUtils.newInt(1, totalClasses);
solution.setVariable(i, val);
Variable value = solution.getVariable(i);
System.out.println(EncodingUtils.getInt(value));
}
for(int j = totalClasses; j < getNumberOfVariables(); j++)
solution.setVariable(j, EncodingUtils.newInt(1, tClass));
return solution;
}
Here, totalClasses is any number between 1 and getNumberOfVariables() value; Actually i want to initialize all values in solution before totalClasses randomly and rest of the values in solution based on already assigned values. in my above code i keep on getting output as zero always (of the statement:- System.out.println(EncodingUtils.getInt(value)))

kindly help me
and if i am doing something wrong with my implementation then kindly correct me.

Thanks in advance

@dhadka
Copy link
Member

dhadka commented Apr 6, 2018

Since the value of the variable hasn't been set yet, it defaults to 0.

The values are typically set in the Initialization step. For example, here I create 100 random solution for the DTLZ2 problem and print their variables:

		Problem problem = new DTLZ2(2);
		RandomInitialization initialization = new RandomInitialization(problem, 100);
		
		for (Solution solution : initialization.initialize()) {
			System.out.println(Arrays.toString(EncodingUtils.getReal(solution)));
		}

If you just want to see what a randomly created variable looks like, you can call randomize():

		Variable variable = EncodingUtils.newInt(0, 5);
		variable.randomize();
		System.out.println(EncodingUtils.getInt(variable));

@Amit-1984
Copy link
Author

@dhadka Thanks for the quick prompt.....
But i want to initialize the solutions myself and do not want just 100 random solutions. Actually i am representing the classes and interfaces in a solution and want to assign integer number to interfaces based on number assigned to classes in the solution.
like if i have 6 classes and 4 interfaces in a system then my solution is of size 10 containing integer values. the first six values are randomly initialized in newSolution() and rest 4 are based on previously assigned 6 integer values to the current solution.
And similar i am trying in my solution representation in newSolution(). Can you please guide me in this direction if i am able to make myself clear...

Thanks in advance.

@dhadka
Copy link
Member

dhadka commented Apr 9, 2018

I see. In that case, you would just need to use one of the set methods in EncodingUtils to set the value, such as EncodingUtils.setInt(variable, 5).

Please note though that RandomInitialization, which is used by default, will overwrite any values you assign in newSolution(). You would need to use a different initialization class, such as the one below, so it uses the solutions generated by newSolution() as is.

public void NoInitialization implements Initialization {

	protected final Problem problem;

	protected final int populationSize;

	public NoInitialization(Problem problem, int populationSize) {
		super();
		this.problem = problem;
		this.populationSize = populationSize;
	}

	public Solution[] initialize() {
		Solution[] initialPopulation = new Solution[populationSize];

		for (int i = 0; i < populationSize; i++) {
			Solution solution = problem.newSolution();

			// Assume that the solutions are already initialized.  Skip calling randomize().

			initialPopulation[i] = solution;
		}

		return initialPopulation;
	}
}

@github-actions
Copy link

This is an automated message. This issue is flagged as stale and will be closed in 7 days. If you feel this issue is still relevant, leave a comment to keep the issue open. Please also consider contributing a fix for the issue.

@github-actions github-actions bot added the Stale label Nov 18, 2022
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants