<commit_before>package com.network.topology.routing.constraints;

import com.lpapi.entities.LPConstraintGroup;
import com.lpapi.entities.LPExpression;
import com.lpapi.entities.LPOperator;
import com.lpapi.entities.group.LPGroupInitializer;
import com.lpapi.entities.group.LPNameGenerator;
import com.lpapi.entities.group.generators.LPEmptyNameGenratorImpl;
import com.lpapi.exception.LPModelException;
import com.lpapi.exception.LPNameException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Set;

public class RoutingContinuityConstrGroupInitializer extends LPGroupInitializer {

  private static final Logger log = LoggerFactory.getLogger(RoutingContinuityConstrGroupInitializer.class);

  private LPNameGenerator routingNameGenerator;

  private Set<String> vertexVars;

  public RoutingContinuityConstrGroupInitializer(Set<String> vertexVars, LPNameGenerator routingNameGenerator) {
    if (routingNameGenerator==null) {
      log.error("Initialized with empty routing variable name generator");
      this.routingNameGenerator = new LPEmptyNameGenratorImpl<>();
    } else {
      this.routingNameGenerator = routingNameGenerator;
    }
    if (vertexVars!=null) {
      this.vertexVars = Collections.unmodifiableSet(vertexVars);
    } else {
      log.error("Constraint generator initialized with empty set of vertices");
      this.vertexVars = Collections.EMPTY_SET;
    }
  }

  @Override
  public void run() throws LPModelException {
    try {
      LPConstraintGroup group = model().getLPConstraintGroup(this.getGroup().getIdentifier());
      for (String s : vertexVars) {
        for (String d : vertexVars) {
          //for all distinct pair of vertices, routing can only use a link that exists : r(s,d,i,j) <= LE (i,j)
          if (s.equals(d))
            continue;
          for (String k : vertexVars) {
            //Generate constraint
            LPExpression sum1 = new LPExpression(model());
            LPExpression sum2 = new LPExpression(model());
            for (String i: vertexVars) {
              if (i.equals(k))
                continue;
              if (!i.equals(s))
                sum1.addTerm(model().getLPVar(routingNameGenerator.getName(s,d,k,i)));
              if (!i.equals(d))
                sum2.addTerm(model().getLPVar(routingNameGenerator.getName(s, d, i, k)));
            }
            LPExpression constExpr = new LPExpression(model());
            constExpr.addTerm(1);
            LPExpression lhs, rhs;
            if (k.equals(s)) {
              //only look at outgoing links;
              lhs = sum1;
              rhs = constExpr;<commit_msg>Remove invalida variable considerations from routing continuity<commit_after>package com.network.topology.routing.constraints;

import com.lpapi.entities.LPConstraintGroup;
import com.lpapi.entities.LPExpression;
import com.lpapi.entities.LPOperator;
import com.lpapi.entities.group.LPGroupInitializer;
import com.lpapi.entities.group.LPNameGenerator;
import com.lpapi.entities.group.generators.LPEmptyNameGenratorImpl;
import com.lpapi.exception.LPModelException;
import com.lpapi.exception.LPNameException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Set;

public class RoutingContinuityConstrGroupInitializer extends LPGroupInitializer {

  private static final Logger log = LoggerFactory.getLogger(RoutingContinuityConstrGroupInitializer.class);

  private LPNameGenerator routingNameGenerator;

  private Set<String> vertexVars;

  public RoutingContinuityConstrGroupInitializer(Set<String> vertexVars, LPNameGenerator routingNameGenerator) {
    if (routingNameGenerator==null) {
      log.error("Initialized with empty routing variable name generator");
      this.routingNameGenerator = new LPEmptyNameGenratorImpl<>();
    } else {
      this.routingNameGenerator = routingNameGenerator;
    }
    if (vertexVars!=null) {
      this.vertexVars = Collections.unmodifiableSet(vertexVars);
    } else {
      log.error("Constraint generator initialized with empty set of vertices");
      this.vertexVars = Collections.EMPTY_SET;
    }
  }

  @Override
  public void run() throws LPModelException {
    try {
      LPConstraintGroup group = model().getLPConstraintGroup(this.getGroup().getIdentifier());
      for (String s : vertexVars) {
        for (String d : vertexVars) {
          //for all distinct pair of vertices, routing can only use a link that exists : r(s,d,i,j) <= LE (i,j)
          if (s.equals(d))
            continue;
          for (String k : vertexVars) {
            //Generate constraint
            LPExpression sum1 = new LPExpression(model());
            LPExpression sum2 = new LPExpression(model());
            for (String i: vertexVars) {
              if (i.equals(k))
                continue;
              if ((!i.equals(s)) || (k.equals(d))) //dont calculate sum1 when k equals d
                sum1.addTerm(model().getLPVar(routingNameGenerator.getName(s,d,k,i)));
              if ((!i.equals(d)) || (k.equals(s))) //dont calculate sum2 when k equals s
                sum2.addTerm(model().getLPVar(routingNameGenerator.getName(s, d, i, k)));
            }
            LPExpression constExpr = new LPExpression(model());
            constExpr.addTerm(1);
            LPExpression lhs, rhs;
            if (k.equals(s)) {
              //only look at outgoing links;
              lhs = sum1;
              rhs = constExpr;
{'fraction_non_alphanumeric': 0.10007278020378457, 'fraction_numerical': 0.0032751091703056767, 'mean_word_length': 3.080920564216778, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6725410', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1552, 'n_words': 351}